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;}


为什么会出现此错误?

错误:“标点” |的类型冲突

最佳答案

以下代码:


包含问题评论
干净地编译
演示如何在数组中分配结构的许多实例
演示如何处理错误
运算符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

关于c - 标点冲突类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41212734/

10-11 19:53