I created a 2d array of structures and now I want to assign values to x, y, and z. Any ideas where the segmentation fault is coming from?
struct xyz
{
int x;
int y;
int z;
};
char buf[80];
struct xyz array[width][height];
for(row=1;row<=width;row++)
{
for(col=1;col<=height;col++)
{
fgets(buf,80,file);
array[row][col].x = strtol(buf, NULL, 10);
fgets(buf,80,file);
array[row][col].y = strtol(buf, NULL, 10);
fgets(buf,80,file);
array[row][col].z = strtol(buf, NULL, 10);
}
}
SI there a reason you’re running from 1 to hight/col? In C all array begin in 0 and end in length-1.
In other word the for loops should look like: