Two Dimensional Array of Characters in C

安稳与你 提交于 2019-12-05 07:43:26

问题


Help me to get out of this problem. I'm using GCC on ubuntu12.04. While I write this program to get 5 strings from keyboard n then print these strings on screen. Program is compiled but during execution it takes strings from keyboard but print only last string. The program which I have written is below:

void main()    
{  
    char names[10];  
    int i,j;

    for(i=0;i<5;i++)  
    {  
        printf(" Enter a name which you want to register\n");  
        scanf("%s",names);  
    }  
    for(i=0;i<5;i++)    
        printf(" the names you enter are %s\n", names);  

}

回答1:


1) you can use 2D char array in this way

char names[5][100];

each line in the 2D array is an array of char with size = 100

for(i=0;i<5;i++)  
{
    printf(" Enter a name which you want to register\n");  
    scanf("%99s",names[i]);
}

2) You can use array of pointers in this way

char *names[5];

each element in the array is a pointer to a string (char array). you have to assign each pointer in the array to a memory space before you call scanf()

for(i=0;i<5;i++)  
{
    names[i]=malloc(100);  
    printf(" Enter a name which you want to register\n");  
    scanf("%99s",names[i]);
}

3) if you compile with gcc version >2.7 then your scanf() can allocate memory by using "%ms" instead of "%s"

char *names[5];
for(i=0;i<5;i++)  
{
    printf(" Enter a name which you want to register\n");  
    scanf("%ms",&names[i]);
}



回答2:


There is a simple example about reading and keeping string in the char array.

#include <stdio.h>

const int MACRO = 6;

int main() {
  printf("Hello Admin Please Enter the  Items:\n");

  char items[MACRO][20];

  for (int i = 0; i < MACRO; ++i) {
    scanf("%19s", items[i]);

  }

  for (int i = 0; i < MACRO; ++i) {
    printf("%s ", items[i]);
  }

  return 0;
}



回答3:


Here is the code I wrote using pointer.

#include <stdio.h>
void main()
{
   char *string[100];
   int ln;

   printf("Enter numbar of lines: ");
   scanf("%d",&ln);
   printf("\n");
   for(int x=0;x<ln;x++)
   {    
     printf("Enter line no - %d ",(x+1));
     scanf("%ms",&string[x]);  // I am using gcc to compile file, that's why using %ms to allocate memory.              
   }
   printf("\n\n");
   for(int x=0;x<ln;x++)
   {            
     printf("Line No %d - %s \n",(x+1),string[x]);  
   }

}   

Another code using two dimensional Array

    #include <stdio.h>

    void main()
    {

    int ln;

    printf("Enter numbar of lines: ");
    scanf("%d",&ln);
    printf("\n");

    char string[ln][10];

    for(int x=0;x<ln;x++){
        printf("Enter line no - %d ",(x+1));
        scanf("%s",&string[x][0]);
    }


    for(int x=0;x<ln;x++)
    {           
        printf("Line No %d - %s \n",(x+1),string[x]);   
    }



}   


来源:https://stackoverflow.com/questions/17466563/two-dimensional-array-of-characters-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!