简体   繁体   中英

c - save then print out a string in 2d array in C

I'm trying to make a simple program from C to store a data then save it to 2d array there's 2 data stored in the 2d array. I think the saving code is work well, but when I try to print out the data stored in 2d array it crashes.

Here's my code :

#include <stdio.h>
int main(){
 char data[2][3];

 //input the data then save it in 2d array
 printf("enter the first word: ");
 scanf("%s", &data[0][0]);
 printf("enter the second word: ");
 scanf("%s", &data[1][0]);

 //here is the problem when iam trying to print out the stored data in 2d array
 printf("first word: %s\nsecond word: %s", data[0][0],data[1][0]);
}

You have 3 issues with your code.

First, and the one causing your code to crash is your final printf() . You passed in data[0][0] which is of type char to %s which takes a null terminated string . You have also need an extra new line at the end. Change it to:

printf("first word: %s\nsecond word: %s\n", data[0], data[1]);

Second, your second dimension of your array is only 3. This means you can only store strings of size 3 or less. Accounting for the null terminator at the end, it's just a 2 character array. Change the second dimension to a bigger number (ie data[2][50]).

Third, &data[0][0] is redundant. You are dereferencing a pointer then making it a pointer again. Just do data[0] and data[1] in your scanf() .


About your confusion of 2d char arrays

I think you are confused about what char arrays are and what c-strings are. A char array is an array of char . "Bill", for example, would be a char array where 'B' will be the index 0, 'l' will be index 3, and a null ternimator '\\0' will be the last index to tell the machine that this is the end of the string.

A 2d char array would be an array of those strings (eg "Bill") like "Bill", "Bob", "Alice" etc. If you want to also keep track of not only people's names, but also their addresses, phones etc, you might need a 3d array.

The 3d array will look something like data[2][4][50] . Where the first level are the people you want to keep track of. The second level are the attribute of a specific person (name, address etc.). Third level will be an array of characters ( char ) that makes up an attribute about a person.

Clearing one thing...

Pass the address of a character array not the address of a char .

scanf("%s", data[1]);// same as &data[1][0]

Here you will see that the passing the address of the char variable also works because it is basically coincides with the address of the char array.

Error lies somewhere else...

Also you should pass the address of the char array to the printf .

printf("first word: %s\nsecond word: %s\n", data[0], data[1]);

Or as it is pointed out in the line before..yes you got it right..this also works

printf("first word: %s\nsecond word: %s\n", &data[0][0], &data[1][0]);

And as you know there will be 2 characters use scanf("%2s",data[1]) .

So where did I do wrong?

"%s" expects a char* and what you passed to it is char . That's why it ran into error. Yes, it considered that char value to be an address and tried to access it and it invoked undefined behavior.

If you want to print characters then you will use different format specifier:

printf("%c",data[1][0]);

Few things to point out:-

  • You are only considering 2 length char arrays or 2 length strings.

  • To remind you once more %c deals with arguments of type char and %s deals with arguments of type char* . (That's why the distinction in printf ).

  • scanf() is like a parser. In case it fails to parse something it skips it. There are better ways to get input fgets() etc.

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