Home > Blockchain >  Struct array string merge with another string in c
Struct array string merge with another string in c

Time:07-01

So i have a problem where the string in struct merge with another array. See the code and output for more explanation. Code:

#include <stdio.h>
#include <stdlib.h>

struct print
{
    char code[3];
    char name[10];
}test[2]={"001","Alen","101","Paul"};

int main()
{

    int x;
    for(x=0;x<2;x  )
    {
        printf("%s %s\n",test[x].code,test[x].name);
    }
    return 0;
}

Output:

001Alen Alen
101Paul Paul

Process returned 0 (0x0)   execution time : 0.017 s
Press any key to continue.

The output is wrong, it should be like this:

001 Alen
101 Paul

So why the "name" merge in "code" variables? It shouldn't be like that. So how do i solve this? Thank you.

CodePudding user response:

C strings need to be NUL terminated. Your code array field is one character too small. Change char code[3]; to be char code[4];

CodePudding user response:

You declared a character array within the structure with exactly 3 characters

char code[3];

Strings literals like this "001" have 4 characters. The last character of such string literals is the terminating zero character '\0'.

So in this declaration

struct print
{
    char code[3];
    char name[10];
}test[2]={"001","Alen","101","Paul"};

the character array code is initialized by three characters of corresponding string literals. The last character, the terminating zero character '\0', of the string literals is not stored in the array.

Thus the character array code does not contain a string.

To output it using the conversion specifier s you should write

printf("%.3s %s\n",test[x].code,test[x].name);

Another approach is to enlarge the array as for example

char code[4];

In this case it can store the terminating zero character '\0' of the string literals and you may write

printf("%s %s\n",test[x].code,test[x].name);

CodePudding user response:

To perhaps put this in more visual terms, judging from the output you're receiving your program is laying out a struct print in memory like so:

 ----------- --------------------------------------- 
| code      | name                                  |
 --- --- --- --- --- --- --- --- --- --- --- --- --- 
|   |   |   |   |   |   |   |   |   |   |   |   |   |
 --- --- --- --- --- --- --- --- --- --- --- --- --- 

If you initialize with {"001", "Alen"}, this looks like:

 ----------- --------------------------------------- 
| code      | name                                  |
 --- --- --- --- --- --- --- --- --- --- --- --- --- 
| 0 | 0 | 1 | A | l | e | n | \0|   |   |   |   |   |
 --- --- --- --- --- --- --- --- --- --- --- --- --- 

Because the code field only provides three characters, there isn't room for the null terminator. Thus if we point at the beginning of code and print that as a string, the program will print until it hits a null terminator.

We can see this demonstrated by a simple program:

#include <stdio.h>

struct print
{
    char code[3];
    char name[10];
};

int main() {
    struct print x = {"001", "Alen"};

    for (char *ch = x.code; *ch != '\0'; ch  ) {
        printf("> %c\n", *ch);
    }

    return 0;
}

When we run this:

> 0
> 0
> 1
> A
> l
> e
> n

But if we initialize code with a two digit code:

#include <stdio.h>

struct print
{
    char code[3];
    char name[10];
};

int main() {
    struct print x = {"01", "Alen"};

    for (char *ch = x.code; *ch != '\0'; ch  ) {
        printf("> %c\n", *ch);
    }

    return 0;
}

Which when run:

> 0
> 1

As stated previously, the most obvious solution is to increase the size of the code field to leave room for the null terminator.

  • Related