Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

more struct problems

Status
Not open for further replies.

troye

Technical User
May 16, 2003
21
MY
I'm having some trouble figuring out how to declare structs.

In "struct dep toys", rather then actually initializing the value to "Roger", i am trying to point the value to the "struct emp manager.empname" value. If I leave the value in "dep toys" to "Roger", it works fine, but it's not what the question in the book is asking me to do. I need to reference it to "emp manager.empname".

Should I be using a strcpy somewhere along the line?

secondly, I need to be able to have a function recieve a struct and print out the values. The functions "print_emp" has to take in a type "struct emp" value and us it's contents as values to be displayed.


Thanks.


======================================



#include <stdio.h>
#include <string.h>

struct date
{
int yy,
mm,
dd;
};

struct emp
{
char empname[10];
float salary;
struct date hired;
};

struct dep
{
struct emp manager;
struct emp worker[15];
float profits;
};

void print_emp(struct emp toys);

void main(void)
{
struct date hired;
struct emp manager ={&quot;Roger&quot;, 30000,
{hired.yy =1998,
hired.mm = 02 ,
hired.dd =28}
};

struct emp worker[15] = { {&quot;Mojax&quot;, 10000,
{hired.yy =1987,
hired.mm = 04 ,
hired.dd =12}},

{&quot;Kojax&quot;,8000,
{hired.yy =1980,
hired.mm = 04 ,
hired.dd =02}}
};

struct date date1 = {1934, 02, 23};
struct emp person1 = {&quot;Roger&quot;, 30000,
{hired.yy =1998,
hired.mm = 02 ,
hired.dd =28}
};

struct dep toys[2]= {
//******------>problem here {{manager.empname,30000,
{hired.yy =1998,
hired.mm = 02 ,
hired.dd =28}
},
{&quot;Mojax&quot;, 10000,
{hired.yy =1987,
hired.mm = 04 ,
hired.dd =12}},
80000},

{{&quot;Roger&quot;,30000,
{hired.yy =1998,
hired.mm = 02 ,
hired.dd =28}
},
{&quot;Kojax&quot;,8000,
{hired.yy =1980,
hired.mm = 04 ,
hired.dd =02}},
80000}
};


printf(&quot;\ndate1: %d/%d/%d\n&quot;, date1.mm, date1.dd, date1.yy);

printf(&quot;\nPerson1\n---------\nName: %s\nSalary: %.2f\nHired: %d/%d/%d\n&quot;,
person1.empname,person1.salary,person1.hired.mm, person1.hired.dd, person1.hired.yy);


printf(&quot;\nmanager******%s %.2f %d/%d/%d\n&quot;, manager.empname, manager.salary, manager.hired.mm, manager.hired.dd, manager.hired.yy);

printf(&quot;\nworker[0]******%s %.2f %d/%d/%d&quot;, worker[0].empname, worker[0].salary, worker[0].hired.mm, worker[0].hired.dd, worker[0].hired.yy);
printf(&quot;\nworker[1]******%s %.2f %d/%d/%d\n&quot;, worker[1].empname, worker[1].salary, worker[1].hired.mm, worker[1].hired.dd, worker[1].hired.yy);

printf(&quot;\ntoy[0]******%s %.2f %d/%d/%d&quot;, toys[0].manager.empname, toys[0].manager.salary, toys[0].manager.hired.mm, toys[0].manager.hired.dd, toys[0].manager.hired.yy);
printf(&quot;\ntoy[0]******%s %.2f %d/%d/%d\n&quot;, toys[0].worker[0].empname, worker[0].salary, worker[0].hired.mm, worker[0].hired.dd, worker[0].hired.yy);

printf(&quot;\ntoy[1]******%s %.2f %d/%d/%d&quot;, toys[1].manager.empname, toys[1].manager.salary, toys[1].manager.hired.mm, toys[1].manager.hired.dd, toys[1].manager.hired.yy);
printf(&quot;\ntoy[1]******%s %.2f %d/%d/%d\n&quot;, toys[1].worker[1].empname, worker[1].salary, worker[1].hired.mm, worker[1].hired.dd, worker[1].hired.yy);

// print_emp(toys[0]);
// print_emp(toys[1]);

return 0;

}


void print_emp(struct emp toys)
{
printf(&quot;\nPersonX\n---------\nName: %s\nSalary: %.2f\nHired: \n&quot;,
toys.empname,toys.salary);

}
 
For question #2:
Pass the function a pointer.
Code:
Proto:
void print_emp(struct emp *toys) {
 printf(&quot;\nPersonX\n---------\nName: %s\nSalary: %.2f\nHired:  \n&quot;,
                    toys->empname,toys->salary);
}

Called like:
struct emp stuff;
print_emp(&stuff);
 
i think you are facing a problem because you trying to initialize the char array empname (in toys[0].manager.empname) with a non-constant expression (manager.empname). when you use &quot;Roger&quot; you are initializing toys[0].manager.empname with a constant expression and that is why it works.

you could either use strcpy or change the empname in struct emp from
char empname[10];
to
char *empname;
since pointers can be freely assigned values - constant or not - unlike arrays.

using strcpy will break the uniformity in the code. it will also require you to manage the array sizes so that the biggest name can be accomodated in any instance of the struct emp. no such problem with a pointer, but you should remember to initialize each such pointer to NULL and check the value before using it (or simply avoid using any before initializing to a nice value).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top