我不能输入的名称到使用scanf函数的字符数组 [英] I can not input the names into the char array using scanf

查看:107
本文介绍了我不能输入的名称到使用scanf函数的字符数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能用scanf函数,进入stdnames阵列的名称。
     编译时,它没有错误,但是当我输入一个名称,
    那么preSS输入写它给出了一个错误,并关闭该程序的其他名称。
    我应该怎么做呢?

  INT主(INT ARGC,CHAR *的argv [])
{
  浮标志[50];
  / *字符* stdnames [100] = {阿文德Thillainathan,罗伯特郎}; * /
 //我要象上面one会将名字
  字符* stdnames [100];
  INT totalNames = 0;
  INT I = 0,W = 0中,h = 0; 的printf(你要进入?? \\ n有多少名办);
 scanf函数(%d个,&安培; totalNames);
 断言(totalNames!= 0); 对于(诠释计数= 0; COUNT< totalNames;计数++)
 {
     的printf(请输入学生的\\ n名称);
     scanf函数(%S,stdnames [计数]);
 //从这里开始的问题
 }
 getres(标记,totalNames); 对于(i = 0; I< totalNames;我++)
      {
        INT V = 1;
        的printf(\\ n);
        的printf(%S \\ n \\ n个IELTS标志,stdnames [I]);
        对于(H = W; H< W + 5; H ++)
           {
            如果(ⅴ== 1)
            {
                的printf(听:0.1F%\\ N标记[H]);
            }
            否则如果(ⅴ== 2)
            {
                的printf(阅读:0.1F%\\ N标记[H]);
            }
            否则,如果(V == 3)
            {
                的printf(写作:0.1F%\\ N标记[H]);
            }
            否则,如果(V == 4)
            {
                的printf(说起:0.1F%\\ N标记[H]);
            }
            其他
            {
                的printf(总体来说:0.1F%\\ n \\ n,标志[H]);
            }
            v ++;
            //如果(H == 10)
            // {
            //突破;
            //}
        }
        W + = 5;
}
返回0;
}


解决方案

通过

 的char * stdnames [100];

你有(指向char)数组。结果

接下来的大问题是

谁将为这些指针分配内存?

一个小型的答案是 - 你必须做你自己象下面这样:结果

  stdnames [计数] =的malloc(100 * sizeof的(炭)); //您可以用所需的大小替换100

  stdnames [计数] =的malloc(100); //的sizeof(char)的几乎总是1

您需要的 scanf函数语句之前把此行。结果

注意:不要忘了在分配的内存一旦这些变量变得无关紧要。不喜欢它:

 免费(stdnames [计数]);

I can not use scanf to enter a name in stdnames array. when compiled it had no error , but as soon as i enter a name and then press enter to write the other name it gives an error and shuts the program. How should I go about it ?

int main(int argc, char* argv[])
{
  float marks[50];
  /*char *stdnames[100]={"Arvind Thillainathan","Robert Lang"};*/
 //I want to stores names like the above one
  char *stdnames[100];
  int totalNames = 0;
  int i = 0, w=0,h=0;

 printf("How many names do you want to enter ??\n");
 scanf("%d",&totalNames);
 assert(totalNames != 0);

 for(int count = 0; count < totalNames; count++)
 {
     printf("Enter name of student\n");
     scanf("%s",stdnames[count]);
 //From here the problem starts
 }
 getres(marks,totalNames);

 for(i = 0; i < totalNames; i++) 
      {
        int v = 1;
        printf("\n");
        printf("IELTS Marks of %s\n\n",stdnames[i]);
        for(h = w; h < w+5; h++)
           {
            if(v==1)
            {
                printf("Listening : %0.1f\n", marks[h]);
            }
            else if(v==2)
            {
                printf("Reading   : %0.1f\n", marks[h]);    
            }
            else if(v==3)
            {
                printf("Writing   : %0.1f\n", marks[h]);    
            }
            else if(v==4)
            {
                printf("Speaking  : %0.1f\n", marks[h]);    
            }
            else
            {
                printf("Overall   : %0.1f\n\n", marks[h]);
            }
            v++;
            //if(h==10)
            //{
            //  break;
            //}
        }
        w+=5;
}
return 0;
}

解决方案

By

char *stdnames[100];

you got an array of (pointers to char).

The NEXT BIG QUESTION is

Who will allocate memory for each of these pointers?

A small answer would be - You have to do it yourself like below :

stdnames[count]=malloc(100*sizeof(char)); // You may replace 100 with desired size

or

stdnames[count]=malloc(100); // sizeof(char) is almost always 1

You need to put this line before the scanf statement.

Note: Don't forget to free the allocated memory once these variables become irrelevant. Do it like :

free(stdnames[count]);

这篇关于我不能输入的名称到使用scanf函数的字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆