random random - 8 months ago 36
C Question

Numbers and letters in char array c

Let me preface with my

background is about as deep as a puddle in the desert.

I'm trying to create a
that has this format:

struct Event {
char time[20];
char name[20];
char userId[20];

Then assign the values like this:

int main(int argc, const char * argv[]) {

struct Event event1;

strcpy( event1.time, "2007-03-01T13:00:00Z");
strcpy( event1.name, "VS");
strcpy( event1.userId, "2d97f036a1T13G21Jm0Z");

printf("%s", event1.time);

return 0;

However, I'm getting
on this line:

strcpy( event1.time, "2007-03-01T13:00:00Z");

Which makes sense because of the number values in the timestamp. I can't seem to find anything on how to store both numbers and characters in string.


The first thing I see is that 2007-03-01T13:00:00Z and 2d97f036a1T13G21Jm0Z requires char [21] to allow for the additional \0 terminating character.

Not sure why you are getting the failure on event1.time as it should overflow into the next field, but try fixing the field sizes and retest.

FYI: I've mostly given up on strcpy() for safety purposes and use strncpy() instead. In paranoid mode this looks like:

strncpy( target, source, sizeof target );
target[sizeof target - 1]] = '\0';   /* assure \0 terminator */