“->”的无效类型参数(有 'struct arr')
invalid type argument of '->' (have 'struct arr')
#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
时没有问题。
var
和 temp
都是指针。那么为什么我会收到此错误?
另一个不相关的问题,我必须在哪里或何时释放动态分配的指针temp
。 temp
是在函数 void inputArray(int);
中动态分配的,该函数在 main.
的循环中调用
(var+count)
!= var[count]
但
*(var+count)
== var[count]
并且因为
(*(var+count)).temp
或 (var+count)-> temp
然后
var[count].temp
或 (&var[count]) -> temp
确保 var
已正确初始化并且在使用前引用了有效对象!!
具有静态存储持续时间的声明指针
struct arr{
int *temp;
int size;
}*var;
是零初始化的,不指向任何分配的内存。
所以你的程序有未定义的行为。
至少你需要在调用函数之前分配一个 struct arr
类型的数组,大小写 elements
。
例如
var = malloc( cases * sizeof( struct arr ) );
至于问题
In the above code, whenever I replace (var+count)->... with
var[count]-> it shows the error :" 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
#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
时没有问题。
var
和 temp
都是指针。那么为什么我会收到此错误?
另一个不相关的问题,我必须在哪里或何时释放动态分配的指针temp
。 temp
是在函数 void inputArray(int);
中动态分配的,该函数在 main.
(var+count)
!= var[count]
但
*(var+count)
== var[count]
并且因为
(*(var+count)).temp
或 (var+count)-> temp
然后
var[count].temp
或 (&var[count]) -> temp
确保 var
已正确初始化并且在使用前引用了有效对象!!
具有静态存储持续时间的声明指针
struct arr{
int *temp;
int size;
}*var;
是零初始化的,不指向任何分配的内存。
所以你的程序有未定义的行为。
至少你需要在调用函数之前分配一个 struct arr
类型的数组,大小写 elements
。
例如
var = malloc( cases * sizeof( struct arr ) );
至于问题
In the above code, whenever I replace (var+count)->... with var[count]-> it shows the error :" 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