gpt4 book ai didi

c - 数组类型的元素类型不完整,无法将结构数组传递给函数

转载 作者:行者123 更新时间:2023-11-30 16:42:06 26 4
gpt4 key购买 nike

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

int main()
{
int n;
printf("how many students:\n");
scanf("%d",&n);
struct std
{
int rollNo;
char FirstName[15];
char SecondName[15];
char fatherName[20];
char motherName[20];
char institute [50];
char deptt[20];
int sem;
float cgpa;
float sgpa;
float height;
float weight;
char citizenship[10];
char lang1[15];
char lang2[15];
int no_siblings;
char sub1[15];
char sub2[15];
char sub3[15];
float marks_12;

}Array[n],*s;
writes(&s,n,Array);
return 0;
}

void writes(struct std *s,int n,struct std Array[n])
{ int i;

for(i=0;i<n;i++)
{
printf("serially enter \n roll-no,\n first name,\n second name,\n father's name,\n mother's name,\n institute name,\n department,\n semester,\n cgpa,\n sgpa,\n height,\n weight,\n citizenship,\n lang1,\n lang2,\n no of siblings,\n sub1,\n sub2,\n sub3,\n class 12 board's %age marks scored\n");
scanf("%d",Array[i].rollNo);
gets(Array[i].FirstName);
gets(Array[i].SecondName);
gets(Array[i].fatherName);
gets(Array[i].motherName);
gets(Array[i].institute);
gets(Array[i].deptt);
}

}

我开始编写一个代码,我可以将结构 std 的数组传递给函数以获取输入,但我收到错误消息“数组类型具有不完整的元素类型”,

我是初学者,我尝试了很多但无法解决。请帮忙。

最佳答案

以下建议代码

  1. 干净地编译
  2. 使用当前可用的函数,而不是已删除的函数
  3. 尊重终端的宽度
  4. 在调用 printf() 时正确使用格式参数
  5. 正确检查系统函数(fgets()、scanf())的错误指示
  6. 正确定义结构,以便定义在整个代码中可见
  7. 消除“神奇”数字
  8. 使用适当的水平和垂直间距
  9. 在设置值之前清除数组
  10. 演示如何将长字符串分解为短字符串
  11. 从调用 writes() 中删除未使用的参数
  12. 正确地为 writes() 函数提供原型(prototype)
  13. 将数据保存在使用位置的本地

现在,建议的代码示例

#include <stdio.h>   // printf(), scanf(), perror()
#include <stdlib.h> // exit(), EXIT_FAILURE
#include <string.h> // memset()

#define LEN_FIRSTNAME 15
#define LEN_SECONDNAME 15
#define LEN_FATHERNAME 20
#define LEN_MOTHERNAME 20
#define LEN_INSTITUTE 50
#define LEN_DEPTT 20
#define LEN_CITIZENSHIP 10
#define LEN_LANG1 15
#define LEN_LANG2 15
#define LEN_SUB1 15
#define LEN_SUB2 15
#define LEN_SUB3 15

struct std
{
int rollNo;
char FirstName[ LEN_FIRSTNAME ];
char SecondName[ LEN_SECONDNAME ];
char fatherName[ LEN_FATHERNAME ];
char motherName[ LEN_MOTHERNAME ];
char institute [ LEN_INSTITUTE ];
char deptt[ LEN_DEPTT ];
int sem;
float cgpa;
float sgpa;
float height;
float weight;
char citizenship[ LEN_CITIZENSHIP ];
char lang1[ LEN_LANG1 ];
char lang2[ LEN_LANG2 ];
int no_siblings;
char sub1[ LEN_SUB1 ];
char sub2[ LEN_SUB2 ];
char sub3[ LEN_SUB3 ];
float marks_12;
};


// prototypes
void writes( int n, struct std Array[n] );


int main( void )
{

int n;
printf("how many students:\n");
if( 1 != scanf("%d",&n) )
{
perror( "scanf for number of students failed" );
exit( EXIT_FAILURE );
}


// implied else, scanf successful

struct std Array[n];
memset( Array, '\0', sizeof(Array) );

writes( n, Array );
return 0;
}


void writes( int n, struct std Array[n] )
{
for( int i=0; i<n; i++ )
{
printf( "%s",
"serially enter \n"
"roll-no,\n"
"first name,\n"
"second name,\n"
"father's name,\n"
"mother's name,\n"
"institute name,\n"
"department,\n" );
// semester,\n cgpa,\n sgpa,\n height,\n weight,\n citizenship,\n lang1,\n lang2,\n no of siblings,\n sub1,\n sub2,\n sub3,\n class 12 board's %age marks scored\n");

if( 1 != scanf("%d", &(Array[i].rollNo)) )
{
perror( "scanf for roll-no failed" );
exit( EXIT_FAILURE );
}

// implied else, scanf for roll-no successful

if( !fgets( Array[i].FirstName, sizeof( Array[i].FirstName), stdin ) )
{
perror( "fgets for FirstName failed" );
exit( EXIT_FAILURE );
}

// implied else, fgets for FirstName successful

// remove possible trailing newline
char *newline = NULL;
if( (newline = strchr( Array[i].FirstName, '\n' ) ) )
{
*newline = '\0';
}

//---etc---
}
}

关于c - 数组类型的元素类型不完整,无法将结构数组传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46012693/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com