delclaring and using struct

SGDK only sub forum

Moderator: Stef

Post Reply
matteus
Very interested
Posts: 336
Joined: Mon Feb 04, 2008 1:41 pm

delclaring and using struct

Post by matteus » Wed Oct 28, 2015 9:00 pm

Code: Select all

typedef struct {
    char Name[24];
    char Job[25];
    char Specialism[18];
} Expert;

struct Expert Experts[6];
strcpy(Experts[0].Name, "David Barby");
strcpy(Experts[0].Job, "Expert Independent Valuer");
strcpy(Experts[0].Specialism, "Pottery");
I wanted to check I was doing this correctly? There seems to be several errors though

Grind
Very interested
Posts: 69
Joined: Fri Jun 13, 2014 1:26 pm
Location: US
Contact:

Re: delclaring and using struct

Post by Grind » Wed Oct 28, 2015 9:45 pm

You don't need to put "struct" in front of the type if you used a typedef:

Expert Experts[6]; // Would be fine

Everything else looks valid. Are you running into a problem with this? I think SGDK has a strcpy so that should work. Be careful though as C standard libraries use null terminated strings. You are actually writing a 26 byte array into Job[25]. Likely with your code an attempt to draw/print Experts[0].Job would print "Expert Independent ValuerPottery" due to the null char '\0' being overridden by the P, but I didn't test it myself.

matteus
Very interested
Posts: 336
Joined: Mon Feb 04, 2008 1:41 pm

Re: delclaring and using struct

Post by matteus » Wed Oct 28, 2015 10:29 pm

Cheers, I realised I'd not made the char lengths long enough :)

Post Reply