3

I am trying to create an array of strings, and print it. Sounds easy, but nothing works for me.

First try was:

int main(){

char ls[5][3]={"aba","bab","dad","cac","lal"};
printf("Das hier: %s", ls[1]);


return 0;
}

But the output is:

Das hier: babdadcaclal a

Even if it should only be:

bab

Next try was after some searches, using:

char* ls[5][3]=.....

instead. This prints:

Das hier: pP@

I was searching for this problem about one day, and I guess, that the problem could be my compiler, but I am not sure about that...
Hope that someone knows what to do, because I am out of ideas. I am using the gcc compiler, if this somehow matters.

4
  • 10
    You aren't leaving room for the NULL terminators in your strings. Commented Aug 23, 2018 at 20:25
  • 2
    char ls[][4]={"aba",... Then for the number of rows (in the scope where ls is defined), size_t n = sizeof ls / sizeof *ls;. Commented Aug 23, 2018 at 20:29
  • 9
    One remark. The problem is (almost) never a compiler. Commented Aug 23, 2018 at 20:30
  • the method of declaring the array leaves a lot to be desired. A (IMO) much better declaration would be: char *ls[] = {"aba", "bab", "dad", "cac", "lal" }; (extra spaces for readability) That makes the array very easy to modify and an element can be displayed via: printf( "Das hier: %s\n", ls[1] ); Please note the trailing '\n' in the format string, so the text is immediately output to the terminal rather than sitting in the stdout buffer until the program exits Commented Aug 23, 2018 at 23:58

2 Answers 2

6

This "bab" is a string with an invisible termination '\0' at the end, i.e. of size 4.
To fix, change to

char ls[5][4]={"aba","bab","dad","cac","lal"};

Then, to improve maintainability, use the proposal from David C. Rankins comment, to make an implicitly sized array of TLAs:

char ls[][4]={"aba","bab","dad","cac","lal"};

To use that, use this variable for setting up loops:

size_t n;
n = sizeof ls / sizeof *ls;
Sign up to request clarification or add additional context in comments.

1 Comment

regarding: int n; n = sizeof ls / sizeof *ls; This proposed code is error prone. Because the returned value from sizeof() has type size_t which is an unsigned long int. The proposed code is trying to force a unsigned long int into a int. This is likely to result in a modification of the value. Suggest changing: int n; to size_t n;
3

Strings in C are null-terminated it means that you need one more byte at the end of the character array to mark it's termination. otherwise you cannot find where the string actually ends.

For example, If I have string "aba" it would be like that at the memory:

'a','b','a','\0'

So you should define you're array as:

char ls[5][4]={"aba","bab","dad","cac","lal"};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.