简体   繁体   English

编译时错误C编程数组

[英]Compile time error C programming Arrays

I wrote a small C program which takes some user input and prints it, 我写了一个小的C程序,需要一些用户输入并打印出来,

#include<stdio.h>
int main()
{
    char name[], char add[];
    short int height;
    unsigned char buffer[1024];
    int i,j,n,m;

    printf("enter your name_length num: n is:-\n");
    scanf("%d",&n);

    printf("enter your name\n");

    for(i=0;i<n;i++)
    {
        scanf("%c",&name[i]);
    }

    printf("enter your address: m is :-\n");
    scanf("%d",&m);

    for(j=0;j<n;j++)
    {
        scanf("%c",&add[j]);
    }

    printf("enter your height\n");
    scanf("%d",&height);


    for(i=0;i<n;i++)
    {
        printf("your entered name is:\t",name[i]);
    }

    for(j=0;j<n;j++)
    {
        printf("your entered address is:\t",add[i]);
    }

    printf("your entered height is:\n",height);

 return;
}

but while running I am getting an error - 但是在运行时出现错误-

在此处输入图片说明

So I am at my wits end on why the array size missing is coming, is there anything that I have missed ??? 所以我不知所措地想知道为什么缺少数组大小,有什么我错过的吗?

There are few problems in declaration syntax. 声明语法几乎没有问题。 Change this line 更改此行

from

char name[], char add[];

to

char name[256],  add[256];

After this you get run-time errors ( welcome to C programming ) 此后,您将获得运行时错误(欢迎使用C编程)

In C you must declare array with a constant integer like, 在C语言中,您必须使用常数整数声明数组,例如:

char name[1000],  add[1000];

OR

char name[1000];
char add[1000];

OR

int size=1000;
char name[size];
char add[size];

And your main function should return integer at the end, use 并且您的主函数应该在最后返回整数,使用

return 0;

As others have pointed out, the line char name[], char add[]; 正如其他人指出的那样,行char name[], char add[]; is invalid. 是无效的。 This is because there is no way of knowing how much memory to assign to the array. 这是因为无法知道要分配给阵列多少内存。 There are two solutions, depending on your problem. 有两种解决方案,具体取决于您的问题。

The best solution in this instance would be to move the declaration to below where you know the length of the user's name (assuming you're not using a strict format requiring declarations at the top): 在这种情况下,最好的解决方案是将声明移到您知道用户名长度的下面(假设您使用的格式不是严格要求顶部的声明):

printf("enter your name_length num: n is:-\n");
scanf("%d",&n);
char name[n];

This creates a fixed length array (of length n ), and given a user's name isn't likely to change length over the course of this program this is more than acceptable. 这将创建一个固定长度的数组(长度为n ),并且鉴于用户名在此程序的过程中不太可能更改长度,这是可以接受的。

The alternate solution is to use memory allocation, which allows you to increase or decrease the size of the array during runtime. 另一种解决方案是使用内存分配,这使您可以在运行时增大或减小阵列的大小。

char *name = null;
...
printf("enter your name_length num: n is:-\n");
scanf("%d",&n);
name = malloc( n * sizeof(char));

This assigns n * sizeof(char) bytes to the array, which can later be extended, reassigned or freed. 这为数组分配了n * sizeof(char)个字节,以后可以对其进行扩展,重新分配或释放。 This is the more flexible option. 这是更灵活的选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM