Array vector has "n" elements, but lenght returns "n+1" in C

Asked

Viewed 61 times

1

Hello,

I have the following question: I have a vector and I’m sure it is passed to him only one element through the following loop:

char uid[1];
while(indice1 < indice2){
    printf("Valor de indice 1 %d, indice 2 %d\n", indice1, indice2);
    uid[indice0] = recvline[indice1];
    ++indice1; ++indice0;
}

Printing the values of the indices they are respectively 11 and 12.

However, when I concatenate this string with another one via the strcat function or return its size using (int)strlen(uid), the size is 2.

I believe that the element ' 0' should be suppressed in concatenation or element counting.

Some solution?

Thank you!

  • just do lenght - 1

  • 1

    Removing 0 from the end will cause your string to display the contents of the rest of the empty fields. Ex: If your string has size 11 and the array 30 will appear any symbols at the other 19 positions until the end of the array.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.