以下代码:
- 包含问题的评论
- 完全编译
- 演示如何分配一个结构的许多实例中的阵列
- 说明如何处理错误
- 运营商
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'。消息很明确。 –
谢谢你天才 –
那我该怎么定义呢? –