구조체의 객체 테이블을 main 앞에 c로 초기화 할 수 있습니까? 나는 그것이 IFI는 main
내부 구조체를 초기화 작동하지 않는 것으로 나타났습니다 거리 함수를 만들고 싶어로서구조체 초기화
typedef struct customer{
int x, y;// coordinates
int quantity;
} customer;
customer *table1;
int main(){
table1 = (customer *)malloc(n * sizeof(customer));
table1[0].quantity = 0; table1[0].x = 0; table1[0].y = 0; //afetiria
table1[1].quantity = 1000; table1[1].x = 0; table1[1].y = 12; // 1st
table1[2].quantity = 1500; table1[2].x = 6; table1[2].y = 5; // 2nd
table1[3].quantity = 800; table1[3].x = 7; table1[3].y = 15; // 3rd
distance(1,2) //calculate the distance bet 1st and 2d object
}
:이 구조체를 가지고있다. 전 세계적으로 초기화 할 수있는 방법에 대한 아이디어가 있습니까 table1
?
C 프로그램에서'malloc()'의 반환 값을 변환하지 마십시오. –
정확히 무엇을 의미합니까? 내가 사용하는 타입을 캐스트하지 않고 어떻게 malloc을 사용할 수 있습니까? –
malloc의 반환 유형은 void *입니다. * 캐스트 할 필요가 없습니다. 캐스트를 제거하면 정상적으로 작동합니다. – Perry