以下代碼:
- 包含問題的評論
- 完全編譯
- 演示如何分配一個結構的許多實例中的陣列
- 說明如何處理錯誤
- 運營商
sizeof()
返回size_t
而不是int
,因此所有參考都相應修改
現在的代碼
#include <stdio.h> // scanf(), perror()
#include <stdlib.h> // exit(), EXIT_FAILURE, malloc()
struct varfuri
{
float x;
float y;
};
int main(void)
{
size_t numPoints;
if(1 != scanf("%lu",&numPoints))
{
perror("scanf failed");
exit(EXIT_FAILURE);
}
// implied else, scanf successful
struct varfuri *puncte = malloc(numPoints * sizeof(struct varfuri));
if(NULL == puncte)
{
perror("malloc failed");
exit(EXIT_FAILURE);
}
// implied else, malloc successful
// the following, on a 32 bit architecture, will return 4
printf("\nSIZEOF PUNCTE: \n%lu",sizeof(puncte));
//return 0; not needed when the returned value from `main()` is 0
} // end function: main
你得首先定義爲'float',然後作爲'struct'。消息很明確。 –
謝謝你天才 –
那我該怎麼定義呢? –