2016-12-18 71 views
-2
int main() 
{scanf("%d",&n); 
float *puncte; 

puncte=(float*)malloc(n*sizeof(float)); 
printf("\nSIZEOF PUNCTE: \n%d",sizeof(puncte)); 

struct varfuri{ 
float x; float y; 
}puncte[sizeof(puncte)-1]; 

return 0;} 

为什么会出现此错误?puncte的冲突类型

错误:'puncte'的冲突类型|

+3

你得首先定义为'float',然后作为'struct'。消息很明确。 –

+0

谢谢你天才 –

+0

那我该怎么定义呢? –

回答

0

以下代码:

  1. 包含问题的评论
  2. 完全编译
  3. 演示如何分配一个结构的许多实例中的阵列
  4. 说明如何处理错误
  5. 运营商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