简体   繁体   中英

Two Dimensional Array of Characters in C

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) 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]);
}

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;
}

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]);   
    }



}   

In your program the mistake is that you have not putted '&'address of operator int the first for loop . names in your case is an array if you store %s string in names and not &names[0] or &names[1] or so on then as array itself acts as a pointer therefore the array "names" is pointing to the address of its first elements ie names[0] . so if you are writing scanf("%s",names); that is similar to scanf("%s",&names[0]); so as you are storing the names in one element only and that too for 5 iterations for only the last string you have entered will be stored and previous strings will be gone . so onlye last string is printed in your program .

in your code, you only declare char data type to be one dimensional and thus it will always overwrite the previous input,that's why the result is the last input printed 5 times.

char names[10];

the above declaration means that you declare a char type variable only with 10 character size without an extra array,it means you only declare a single variable for 5 input.

to make a two dimensional char, you will need to declare it like this :

char names[5][10];

in the code above, it means that you declare a char type variable with 10 character size in an array of 5.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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