【问题标题】:invalid type argument of '->' (have 'struct arr')'->' 的无效类型参数(有 'struct arr')
【发布时间】:2020-06-30 13:44:14
【问题描述】:
#include<stdio.h>
#include<stdlib.h>

struct arr{
    int *temp;
    int size;
}*var;

void inputArray(int);
void displayArray(int);

int main()
{

    int cases,i;
    printf("Enter the no of test cases\n");
    scanf("%d",&cases);
    for(i=0;i<cases;++i)
    {
        printf("Entering test case %d:\n\n",i+1);
        inputArray(i);
    }
    printf("You have entered the following\n");
    for(i=0;i<cases;++i)
    {
        printf("Test case %d\n\n",i+1);
        displayArray(i);
    }
    return 0;
}

void inputArray(int count)
{
    int i;
    printf("Enter the size of the array\n");
    scanf("%d",&(var+count)->size);
    (var+count)->temp=(int*)malloc(sizeof(int)*(var+count)->size);
    if((var+count)->temp==NULL)
    {
        printf("NOT ENOUGH MEMORY IN HEAP");
        exit(1);
    }
    printf("Enter the array\n");
    for(i=0;i<(var+count)->size;++i)
    {
        scanf("%d", &(var+count)->temp[i] );
    }

}

void displayArray(int count)
{
    int i;
    printf("\n");
    for(i=0;i<(var+count)->size;++i)
    {
        printf(" %d ",(var+count)->temp[i]);
    }
    printf("\n");
}

在上面的代码中,每当我替换 (var+count)-&gt;... 和 var[count]-&gt; 显示错误:" invalid type argument of '->' (have 'struct arr') " 但是当我使用temp[i]temp+i 时都没有问题。 vartemp 都是指针。那么为什么会出现这个错误呢?

另一个不相关的问题,我必须在何处或何时释放动态分配的指针temptemp 是在函数 void inputArray(int); 内动态分配的,该函数在 main 的循环中调用。

【问题讨论】:

  • 除了编译错误之外,在任何一种情况下,您都不会为var 分配任何值,因此您不能像这样做那样取消引用它。您的程序有未定义的行为。
  • 首先,全局指针变量varNULL,因为它没有被显式初始化,所以已经“暂时定义”了一个默认初始值NULL。跨度>
  • 抱歉我忘了加var=(struct arr*)malloc(sizeof(struct arr)*cases);
  • 在实际分配内存之前,您不能将数据存储在分配的内存中...

标签: c arrays pointers memory-management structure


【解决方案1】:

具有静态存储持续时间的声明指针

struct arr{
    int *temp;
    int size;
}*var;

是零初始化的,不指向任何分配的内存。

所以你的程序有未定义的行为。

在调用函数之前,您至少需要分配struct arr 类型的数组和elements

例如

var = malloc( cases * sizeof( struct arr ) );

至于问题

在上面的代码中,每当我将 (var+count)->... 替换为 var[count]-> 它显示错误:" invalid type argument of '->

那么表达式var[count] 不是指针。它的类型为struct arr

这是一个演示程序,展示了如何实现分配。

#include <stdio.h>
#include <stdlib.h>

struct arr
{
    int *temp;
    size_t size;
};

void inputArray( struct arr *var, size_t count )
{
    printf( "Enter the size of the array: " );

    size_t n = 0;

    scanf( "%zu", &n );

    ( var + count )->temp = malloc( n * sizeof( int ) );

    if ( ( var + count )->temp == NULL )
    {
        ( var + count )->size = 0;
    }
    else
    {
        ( var + count )->size = n;

        if ( n != 0 )
        {
            printf( "Enter %zu element(s) of the array: ", n );
            for ( size_t i = 0; i < n; ++i )
            {
                scanf( "%d", &( var + count)->temp[i] );
            }
        }           
    }

    putchar( '\n' );
}

void displayArray( const struct arr *var, size_t count )
{
    for ( size_t i = 0; i < ( var + count )->size; ++i )
    {
        printf( "%d ", ( var+count )->temp[i] );
    }
    putchar( '\n' );
}

int main(void) 
{
    printf( "Enter the no of test cases: " );

    size_t n = 0;

    scanf( "%zu", &n );

    struct arr *var = malloc( n * sizeof( struct arr ) );

    if ( var == NULL ) 
    {
        puts( "Error. Not enough memory." );
        n = 0;
    }

    for ( size_t i = 0; i < n; i++ )
    {
        printf( "Entering test case %zu:\n", i+1 );
        inputArray( var, i );
    }

    if ( n != 0 )
    {
        puts( "You have entered the following" );

        for ( size_t i = 0; i < n; ++i )
        {
            printf("Test case %zu:" ,i + 1 );
            displayArray( var, i );
        }

        for ( size_t i = 0; i < n; ++i )
        {
            free( ( var + i )->temp );
        }
    }

    free( var );

    return 0;
}

程序输出可能看起来像

Enter the no of test cases: 5
Entering test case 1:
Enter the size of the array: 1
Enter 1 element(s) of the array: 1
Entering test case 2:
Enter the size of the array: 2
Enter 2 element(s) of the array: 1 2
Entering test case 3:
Enter the size of the array: 3
Enter 3 element(s) of the array: 1 2 3
Entering test case 4:
Enter the size of the array: 4
Enter 4 element(s) of the array: 1 2 3 4
Entering test case 5:
Enter the size of the array: 5
Enter 5 element(s) of the array: 1 2 3 4 5
You have entered the following
Test case 1:1 
Test case 2:1 2 
Test case 3:1 2 3 
Test case 4:1 2 3 4 
Test case 5:1 2 3 4 5 

【讨论】:

  • 我的错,我现在明白了.. 你能帮我解决这个问题吗? “另一个不相关的问题,我必须在何处或何时释放动态分配的指针 temp。temp 是在函数 void inputArray(int) 内动态分配的;它在 main 的循环中调用。”来自莫斯科的@Vlad
  • 他不是在问这件事。 In the above code, whenever I replace (var+count)-&gt;... with var[count]-&gt; it shows the error :" invalid type argument of '-&gt;' (have 'struct arr') "
【解决方案2】:

(var+count) != var[count] 但是

*(var+count) == var[count]

因为

(*(var+count)).temp(var+count)-&gt; temp 然后

var[count].temp(&amp;var[count]) -&gt; temp

确保 var 已正确初始化并在使用前引用了一个有效对象!!

【讨论】:

  • 还值得一提的是,OP 的 var 没有指向任何东西。它将是NULL
  • 我只是从这段代码中抽象出来,因为 OP 不了解指针和数组的工作原理。所以这个非常简单的例子。
  • 抱歉我忘了加var=(struct arr*)malloc(sizeof(struct arr)*cases);
  • @P__J__ 你的回答没有意义。这无关紧要。
  • @VladfromMoscow 你的评论没有任何意义In the above code, whenever I replace (var+count)-&gt;... with var[count]-&gt; it shows the error :" invalid type argument of '-&gt;' (have 'struct arr') "
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-22
  • 1970-01-01
  • 1970-01-01
  • 2016-03-21
  • 2021-10-15
  • 2011-12-10
  • 2016-06-01
相关资源
最近更新 更多