#printf acting weird

1 messages · Page 1 of 1 (latest)

rustic locust
#

ok so when i run this code:

for (size_t i = 0; i < arrayLength; i++)
{       
  if(gifts[i].ID[0]=='\0'){
    return;
  }
  printf("%s | %s | %s | %s \n", gifts[i].ID, gifts[i].Gift, gifts[i].Name, gifts[i].Bought);
}

i get this output:

0 | TestGift | TestPerson | 01 
1 | Random | RandomPerson | 1

which is almost correct, except of the 1 at the end of the first line, the gifts[0].ID is just '0', anyone has any idea why is this happening?
this is my Gift structure:

typedef struct{
    char ID[2];
    char Gift[100];
    char Name[100];
    char Bought[1];
}Gift;
obtuse perchBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

karmic tendon
#

@rustic locust char Bought[1]; can't possibly be properly null-terminated 😉

rustic locust
#

Yaiks makes sense

#

So Bought[2] should be fine right?

karmic tendon
#

Once you set Bought[1] to \0 yes

#

But why are you using a char here at all? Why not an int?

rustic locust
#

im getting it from file and its easyer than casting imo

#

anyways, thanks

#

!solved

obtuse perchBOT
#

Thank you and let us know if you have any more questions!

#

[SOLVED] printf acting weird