내 프로젝트에서을 행동 기능 초기화, 내가 만든 3 개 가지 typedef struct
유형 :구조체가 이상하게
typedef struct Point
{
float x;
float y;
float z;
} Point;
typedef struct Triangle
{
Point A;
Point B;
Point C;
unsigned char color[3];//RGB
} Triangle;
typedef struct Structure
{
Triangle* triangles;
unsigned int nt; //number of triangles in array
} Structure;
당신이 눈치 챘을 수도 바와 같이, 유형 Structure
는 Triangle
s의 동적 크기의 배열을 가지고, 그래서 난거야 또한 여기에 메모리 할당 및 해제 기능을 게시 :
Structure newStructure(unsigned int nt)
{
Structure S;
Triangle* tri = malloc ((nt) * sizeof(Triangle));
if (tri!=NULL)
{
S.triangles = tri;
S.nt = nt;
}
else S.nt = 0;
return S;
}
void delStructure (Structure S)
{
if (S.triangles != NULL) free (S.triangles);
}
그럼 내가 다음 구문을 사용하여 현재
Structure
에
Triangle
를 추가하는 기능을하고 싶었 :,616를. 컴파일 할 때이 오류 메시지를 보여줍니다
Structure addTriangle(Triangle T, Structure S)
{
Structure R = newStructure(S.nt+1);
int i=0;
while(i++<S.nt) R[0].triangles[i] = S.triangles[i];
R[0].triangles[S.nt] = T;
delStructure(S); //Is this necessary?
return R[0];
}
, 난이 기능을 사용할 때, 배열의 첫 번째 Triangle
임의의 값을 가져옵니다이 내가 가진 것입니다. 내가 배열에 삼각형 T1
와 Structure S
및 T2
이있는 경우 은 더 명확하게하기 위해, 다음 나는 결과가 Triangle
의 경우 다음과 같은 배열을 가진 Structure
될 것 S = addTriangle(T3,S)
를 사용 {T?,T2,T3}
을 T?
분명히 임의의 값을 가지는 경우.
왜 이런 일이 발생합니까?