2014-05-13 4 views
0

조건이 조금 더 구체적이기 때문에 정확한 질문의 답을 찾을 수 없습니다. 어떻게 struct 포인터 배열을 할당 할 수 있습니까?구조체 포인터의 배열을 할당 하시겠습니까?

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

coord** xy; 

내가 좋아하는 그것을 할당 할 : xy[500][460] 그러나 그들에 액세스 할 때 그것은 잘못된 메모리 오류를 반환합니다.

+1

은'new' 예약어이다. 힙을 할당하거나 스택으로 선언하려고합니까? – Amarghosh

+1

컴파일러가 오래되었고 실제로 오래된 것입니다. new는 내 경우 예약어가 아니지만 예제를 편집합니다. – Lively

+1

'new'는 C++의 예약어입니다. –

답변

0

정적 분배 :

coord xy[500][460]; 

동적 할당은 : 시동기

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); 
+0

고맙습니다. – Lively

+0

@Lively : 환영합니다 :) –

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

for(; idx < 500; ++idx) 
    new [idx] = malloc (460 * sizeof (coord)); 
+0

죄송합니다. 그러나이 코드를 사용하면 다시 같은 오류가 발생합니다. 액세스 권한은 다음과 같습니다.'xy [1] [2] .xp = 20;' – Lively

+0

@Lively : 코드가 경고없이 컴파일됩니까? – alk

+0

게임 엔진에서 꽤 오래된 내장형 컴파일러입니다. 코딩 된 @ barak manos ..work'd 그러나. – Lively

관련 문제