数组类型具有不完整的元素类型,无法将struct数组传递给函数

时间:2017-09-02 10:13:14

标签: c arrays file structure

#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的数组传递给函数来获取输入但是我收到一个错误说&#34;数组类型的元素类型不完整&#34;,

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

1 个答案:

答案 0 :(得分:0)

以下提议的代码

  1. 干净地编译
  2. 使用目前可用的功能,而不是删除功能
  3. 尊重终端的宽度
  4. writes()
  5. 的调用中正确使用格式参数
  6. 从系统函数(fgets(),scanf())
  7. 中正确检查错误指示
  8. 正确定义结构,以便在整个代码中可以看到定义
  9. 消除'魔术'数字
  10. 使用适当的水平和垂直间距
  11. 在设置值
  12. 之前清除数组
  13. 演示了字符串可以分解成短字符串的时间
  14. 从调用writes()
  15. 中删除未使用的参数
  16. 正确提供#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--- } } 函数
  17. 的原型
  18. 将数据保持在本地使用位置
  19. 现在,建议的代码示例

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.eilamshapira.routeburntracker">
    
    <uses-permission android:name="android.permission.INTERNET" />
    
    <compatible-screens>
        <!-- all small size screens -->
        <screen android:screenSize="small" android:screenDensity="ldpi" />
        <screen android:screenSize="small" android:screenDensity="mdpi" />
        <screen android:screenSize="small" android:screenDensity="hdpi" />
        <screen android:screenSize="small" android:screenDensity="xhdpi" />
        <screen android:screenSize="small" android:screenDensity="xxhdpi" />
        <screen android:screenSize="small" android:screenDensity="xxxhdpi" />
        <!-- all normal size screens -->
        <screen android:screenSize="normal" android:screenDensity="ldpi" />
        <screen android:screenSize="normal" android:screenDensity="mdpi" />
        <screen android:screenSize="normal" android:screenDensity="hdpi" />
        <screen android:screenSize="normal" android:screenDensity="xhdpi" />
        <screen android:screenSize="normal" android:screenDensity="xxhdpi" />
        <screen android:screenSize="normal" android:screenDensity="xxxhdpi" />
        <!-- all large size screens -->
        <screen android:screenSize="large" android:screenDensity="ldpi" />
        <screen android:screenSize="large" android:screenDensity="mdpi" />
        <screen android:screenSize="large" android:screenDensity="hdpi" />
        <screen android:screenSize="large" android:screenDensity="xhdpi" />
        <screen android:screenSize="large" android:screenDensity="xxhdpi" />
        <screen android:screenSize="large" android:screenDensity="xxxhdpi" />
        <!-- all xlarge size screens -->
        <screen android:screenSize="xlarge" android:screenDensity="ldpi" />
        <screen android:screenSize="xlarge" android:screenDensity="mdpi" />
        <screen android:screenSize="xlarge" android:screenDensity="hdpi" />
        <screen android:screenSize="xlarge" android:screenDensity="xhdpi" />
        <screen android:screenSize="xlarge" android:screenDensity="xxhdpi" />
        <screen android:screenSize="xlarge" android:screenDensity="xxxhdpi" />
    </compatible-screens>
    
    
    
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
    
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
    
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <intent-filter>
                <action android:name="MAINACTIVITY"></action>
                <category android:name="android.intent.category.DEFAULT"></category>
            </intent-filter>
        </activity>
    
    
        <activity
            android:name=".DisplayMessageActivity"
            android:parentActivityName=".MainActivity">
    
            <!-- The meta-data tag is required if you support API level 15 and lower -->
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>
    
        <activity
            android:name=".ReferralPage"
            android:parentActivityName=".MainActivity">
    
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
    
            <intent-filter>
                <action android:name="OPENMILFORD"></action>
                <category android:name="android.intent.category.DEFAULT"></category>
            </intent-filter>
        </activity>
    
    
        <service
            android:name=".FirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>
    
        <service
            android:name=".MyFirebaseInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>
    
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/colorAccent" />
    </application>