3

I am trying to concatenate arrays from a structure using strcat. My code looks as follows:

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 the output instead of giving me

ABCCABACBAACCCB

gives me just

ABC

Could somebody tell me what I am doing wrong?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Goldengirl
  • 957
  • 4
  • 10
  • 30

5 Answers5

6

Write instead

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 explicitly initialized only the first two data members of each element of the array that is first and first_id. All other data members were zero-initialized.

If you want to use exactly your statements 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 the following way

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
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
4

The problem here is, while initializing at definition time, you're only initializing the first two member variables of each input[n] element (Check the number of initializer supplied). So, later, while doing

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

all the second, third, fourth and fifth member variables for input[n] elements are initialized to 0 or null-pointer, as the array definition is global.

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


Having said that, the recommended signature of main() is int main(int argc, char *argv[]) or at least, int main(void).

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
3

You are declared the structure with ten members

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;
}

While assigning the value you are mentioning the two values, So first two values will be assigned. Rest of the variables will be null.

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

If you do like this, you will get this output, as like you 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);
Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31
2

Your initializer does not cover all members of the struct searchTable, just first two members for each element of the input array. The others are initialized with zeros implicitely. That way:

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

concatenates with empty strings, as all of these are treated as zero-length, because they are starting with '\0' (or NUL in ASCII) null character. In other words:

strlen(input[1].second) == 0
strlen(input[2].third) == 0
...
Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
2
input[5]= {
        {"ABC", 101},
        {"CAB",102},
        {"ACB",103},
        {"AAC",104},
        {"CCB",105}
};

You are always initializing the first two members . i.e first and first_id

so do replace all with input[0].first. that will work for sure .

Happy Coding !!!!!!!

Sohil Omer
  • 1,171
  • 7
  • 14