J
jvax
Hi,
I'm new to C programming and am having the hardest time trying to
figure out how to allocate memory for a structure array. Can somebody
please see if I'm doing things correctly? My code is as follows.
typedef struct s_city{
double x, y;
} CITY;
....
CITY *cities;
cities = (struct s_city *)malloc(numberOfCities * sizeof(struct
s_city));
....
later in the code, when I try to do
for(i=0; i<numberOfCities; i++) {
cities.x = someX;
cities.y = someY;
}
I get a segmentation fault.
I really don't know what I am doing wrong. Help me please.
Thank you in advance.
-- jvax
I'm new to C programming and am having the hardest time trying to
figure out how to allocate memory for a structure array. Can somebody
please see if I'm doing things correctly? My code is as follows.
typedef struct s_city{
double x, y;
} CITY;
....
CITY *cities;
cities = (struct s_city *)malloc(numberOfCities * sizeof(struct
s_city));
....
later in the code, when I try to do
for(i=0; i<numberOfCities; i++) {
cities.x = someX;
cities.y = someY;
}
I get a segmentation fault.
I really don't know what I am doing wrong. Help me please.
Thank you in advance.
-- jvax