简体   繁体   中英

How to fill an array of strings in C

I am currently learning C and am trying to fill an array of strings with a loop as a simple exercise. I am just trying to fill all 4 elements with the word "Hello". When I print my array out I get output like this:

messages[0] = HelloHelloHelloHello
messages[1] = HelloHelloHello
messages[2] = HelloHello
messages[3] = Hello

For an odd reason, the first 3 elements have the word hello repeating mulitple times. I do not understand why this is happening.

Here is my code:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(){

char messages[4][5];

for(int i = 0; i < 4; i++){
    //&messages[i] =  (char *)malloc(5);
    strcpy(messages[i],"Hello");
}

for(int i = 0; i < 4; i++){

    printf("messages[%d] = %s\n", i, messages[i]);
}

return 0;
 }

I also tried looking up way to do this, and I found something about using malloc, but I just kept getting compiler errors.

You have not allocated memory for the null terminator. char messages[4][5]; should be char messages[4][6];

在此处输入图像描述

Since there is no null terminator. printf prints the character until null terminator is found.And also the last strcpy copies the \0 beyond the memory allocated by you. This is undefined behavior.

As mentioned in the other replies, the problem is in the statement

strcpy(messages[i], "Hello");

The array messages is declared as an array of four arrays of length five but the size of the string "Hello" is actually six characters; the terminating null character is an invisible part of the string. If you print the value of sizeof "Hello" you will get the value six.

If you just need an array of strings which you don't intend to change, you can declare your array as

const char *messages[] = {"Hello", "Hello", "Hello", "Hello"};

Here the length of the array is inferred from the initializer.

It is also a good idea to not hard code the length of the array in more then one place. In my own code I always use the array length macro

#define LEN(array) ((int) (sizeof (array) / sizeof (array)[0]))

With this you can write

for (int i = 0; i < LEN(messages); i++) {
    ...
}

If you then want to change the length of messages you only need to change the declaration.

Strings in C are terminated by a zero byte - that is, a byte containing zero. Thus, to hold a five character string you need to reserve six characters for it - five for the data and one for the terminator. If you increase the size of the second dimension of your array of characters to six instead of five it will work as you expected:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(){

char messages[4][6];

for(int i = 0; i < 4; i++){
    //&messages[i] =  (char *)malloc(5);
    strcpy(messages[i],"Hello");
}

for(int i = 0; i < 4; i++){

    printf("messages[%d] = %s\n", i, messages[i]);
}

return 0;
 }

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