'->'的无效类型参数(具有'struct arr')

问题描述 投票:-1回答:1
#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)->...与var[count]->一起显示错误:“'->'(具有'struct arr')的类型参数无效”但是使用temp[i]temp+i都没有问题。vartemp都是指针。那为什么我会得到这个错误呢?

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

c arrays pointers structure
1个回答
1
投票

(var+count) !=

© www.soinside.com 2019 - 2024. All rights reserved.