0

Could someone please help me with this? Why is yearofstart value not printed out in the first argument ? I knw there is something wrong, but I don't knw what is it :)
Thanks so much.

#include <stdio.h>

struct date{
int year;
}yearofstart={1950};

struct record{
char *name;
char acct_type;
struct date yearofstart;
}customer={"Jones",'A'};

void adjust(struct record *ptr);
int main(){

printf("%s %c %d \n",customer.name,customer.acct_type,customer.yearofstart.year);

adjust(&customer);

printf("%s %c %d \n",customer.name,customer.acct_type,customer.yearofstart.year);
return 0;
}

void adjust(struct record *ptr){

ptr->name="Mike";
ptr->acct_type='B';
ptr->yearofstart.year=1999;
}
3
Contributors
4
Replies
5
Views
9 Years
Discussion Span
Last Post by SarahYan
0

The yearofstart member of struct customer variable does not bear a relation to struct yearofstart variable. You don't initialize this last (of struct date type) member of customer. It's a global variable so all its members initialized with 0.

0

It's because of the scope of yearofstart declared inside the struct record lies only within that structure. If you want your program to print 1950, do this:

struct record{
char *name;
char acct_type;
struct date yearofstart;
}customer={"Jones",'A',1950};

Or do this:

printf("%s %c %d \n",customer.name,customer.acct_type,yearofstart.year);
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.