문제

I couldn't find answer of the exact question, since the condition is a bit more specific: How can i allocate array of struct pointers.

typedef struct COORDS
{
  int xp;
  int yp;
} coord;

coord** xy;

i want to allocate it like: xy[500][460] But it returns invalid memory error when accessing them.

도움이 되었습니까?

해결책 2

Static allocation:

coord xy[500][460];

Dynamic allocation:

coord** xy = (coord**)malloc(sizeof(coord*)*500);
for (int i=0; i<500; i++)
    xy[i] = (coord*)malloc(sizeof(coord)*460);

// and at a later point in the execution of your program
for (int i=0; i<500; i++)
    free(xy[i]);
free(xy);

다른 팁

coord** new = malloc (500 * sizeof (coord*));
int idx = 0;

for(; idx < 500; ++idx)
    new [idx] = malloc (460 * sizeof (coord));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top