Why is the "strcat" function not working?

I am trying to concatenate arrays from a struct using strcat

. My code looks like this:

int main(){

//Implementation of Search table Tree

struct searchTable
{
    char first[10];
    int first_id;
    char second[10];
    int second_id;
    char third[10];
    int third_id;
    char fourth[10];
    int fourth_id;
    char fifth[10];
    int fifth_id;

}input[5]= {
        {"ABC", 101},
        {"CAB",102},
        {"ACB",103},
        {"AAC",104},
        {"CCB",105}
};
char join[100]={0};

strcat(join, input[0].first);
strcat(join, input[1].second);
strcat(join, input[2].third);
strcat(join, input[3].fourth);
strcat(join, input[4].fifth);

printf("%s", join);

      

Here's the output instead of giving me

ABCCABACBAACCCB

gives me just

ABC

Can anyone tell me what I am doing wrong?

+3


source to share


5 answers


Instead, write

strcat(join, input[0].first);
strcat(join, input[1].first);
strcat(join, input[2].first);
strcat(join, input[3].first);
strcat(join, input[4].first);

      

Using these initializers

//...
}input[5]= {
        {"ABC", 101},
        {"CAB",102},
        {"ACB",103},
        {"AAC",104},
        {"CCB",105}
};

      

you have explicitly initialized only the first two data elements of each array element, which is first

and first_id

. All other data items were initialized to zero.

If you want to use your assertions exactly with strcat



strcat(join, input[0].first);
strcat(join, input[1].second);
strcat(join, input[2].third);
strcat(join, input[3].fourth);
strcat(join, input[4].fifth);

      

then initialize the array like this

struct searchTable
{
    char first[10];
    int first_id;
    char second[10];
    int second_id;
    char third[10];
    int third_id;
    char fourth[10];
    int fourth_id;
    char fifth[10];
    int fifth_id;

}input[5]= {
        { .first = "ABC", .first_id = 101 },
        { .second = "CAB", .second_id = 102 },
        { .third = "ACB", .third_id = 103 },
        { .fourth = "AAC", .fourth_id = 104 },
        { .fifth = "CCB", .fifth_id = 105 }
};

      

In this case, the output will look like

ABCCABACBAACCCB

      

+6


source


The problem is that on initialization during definition, you only initialize the first two member variables of each element input[n]

(check the number of initializers provided). So later, doing

strcat(join, input[1].second);
strcat(join, input[2].third);
strcat(join, input[3].fourth);
strcat(join, input[4].fifth);

      

all member variables second

, third

, fourth

and fifth

elements input[n]

are initialized to 0

, or a null pointer, since the definition of the array is global.



The output is correct and this is the expected output for this code.


Having said that, the recommended signature main()

is equal to int main(int argc, char *argv[])

or at least int main(void)

.

+4


source


You are declared a ten-member structure

struct searchTable
{
    char first[10];
    int first_id;
    char second[10];
    int second_id;
    char third[10];
    int third_id;
    char fourth[10];
    int fourth_id;
    char fifth[10];
    int fifth_id;
}

      

When assigning a value, you are specifying two values, so the first two values โ€‹โ€‹will be assigned. The rest of the variables will be empty.

strcat(join, input[1].second);
strcat(join, input[2].third);
strcat(join, input[3].fourth);
strcat(join, input[4].fifth);

      

If you do this, you will receive this output as expected.

    strcat(join, input[0].first);
    strcat(join, input[1].first);
    strcat(join, input[2].first);
    strcat(join, input[3].first);
    strcat(join, input[4].first);

      

+3


source


Your initializer doesn't cover all members struct searchTable

, only the first two elements for each element in the array input

. The rest are zero-initialized implicitly. Thus:

strcat(join, input[1].second);
strcat(join, input[2].third);
...

      

is concatenated with empty strings, as they are all treated as zero-length, because they start with a null character '\0'

(or NUL

in ASCII). In other words:

strlen(input[1].second) == 0
strlen(input[2].third) == 0
...

      

+2


source


input[5]= {
        {"ABC", 101},
        {"CAB",102},
        {"ACB",103},
        {"AAC",104},
        {"CCB",105}
};

      

You always initialize the first two members. i.e first and first_id

so replace everything with input [0] .first. this will work for sure.

Happy coding !!!!!!!

+2


source







All Articles