# Problems with Nested Structures and Arrays of Structures

• 03-01-2010
Ignoramus
Problems with Nested Structures and Arrays of Structures
This is an assignment, but I've been making an honest jab at it.

We're supposed to make a contact list program, in which we use an array of structures to hold up to three contacts. The contacts are required to be held in a structure with the fields phone and ID.

-ID has to be a union type with fields of SSN and StudentID
-SSN has to be a structure with fields of prefix, number, and suffix
-phone has to be a structure with fields of area, prefix, suffix

We have to ask the user for the input, of course. If requested, the contact information is printed for each contact.

So here's some of my code of my current try (which is still mostly uncompleted):

Nested Structures
Code:

```typedef struct {                 int prefix,                     number,                     suffix;         } social_secnum;                 typedef union{                 int SchoolID;                 social_secnum SSN;         } identify;                         typedef struct {                 int area,                     prefix,                     suffix;         } phone_number;                 typedef struct contact_struct {                 identify ID;                 phone_number phone;         } contacts[3];```
Function to populate structures?
Code:

```void newContact (contact_struct contacts[3]) {         char contact_menu, dash;         int i;         for (i = 0; i < 3 ; i++) {                 printf ("I) Enter the Student ID of this person\nS) Enter the SSN of this person\n");                 scanf ("%c" &contact_menu);                 switch (contact_menu) {                         case 'I':                         case 'i':                                 printf ("900");                                 scanf ("%d" , &identify.SchoolID);                                 contact_struct contacts[i].ID = &identify.SchoolID;                                 break;                         case 'S'                         case 's':                                 printf ("SSN: " );                                 scanf ("%d%c%d%c%d" , &social_secnum.prefix, &hyphen, &social_secnum.number, &hyphen, &social_secnum.suffix);                                 contact_struct contacts[i].ID = &identify.SSN;                                 break;                         default:                                 printf ("Invalid input. Please try again.");                                 i = 0;                                 break;                 }                 printf ("Phone #: ");                 scanf ("%d%c%d%c%d" , &phone_number.area, &hyphen, &phone_number.prefix, &hyphen, &phone_number.suffix);                 contact_struct contacts[i].phone = &phone_number         } }```
Function to print contact info?
Code:

```void printContact () {         int i;         for (i = 1; i < 4; i++) {                 printf ("Contact #%d\n" , i);                 if (contacts[i] ID == &identify.SchoolID) {                         printf ("900%d\n" , contacts[i-1].ID.SchoolID);                 }                 else if (contacts[i] ID == &identify.SSN) {                         printf ("%d-%d-%d" , contacts[i-1].ID.SSN.prexix, contacts[i-1].ID.SSN.number, contacts[i-1].ID.SSN.suffix);                 }         printf ("(%d)-%d-%d" , contacts [i-1].phone); }```
I know it's not commented, which is really really bad practice... but I wanted to get things figured out before I started commenting... because I'm not quite sure what I'm doing right now. The code is still for the most part unfinished, so yeah. I understand structures, but we've been set out on our own to figure out nested structures, and arrays of structures.

When I compile, these are the errors I get

Code:

```contactlist.c:26: error: expected ‘)’ before ‘contacts’ contactlist.c: In function ‘main’: contactlist.c:39: warning: implicit declaration of function ‘newContact’ contactlist.c:39: error: expected expression before ‘contacts’ contactlist.c:42: warning: implicit declaration of function ‘printContact’ contactlist.c:57: error: expected ‘)’ before ‘contacts’ contactlist.c: In function ‘printContact’: contactlist.c:91: error: expected expression before ‘contacts’ contactlist.c:92: error: expected expression before ‘contacts’ contactlist.c:94: error: expected expression before ‘contacts’ contactlist.c:95: error: expected expression before ‘contacts’ contactlist.c:95: warning: too few arguments for format contactlist.c:97: error: expected expression before ‘contacts’ contactlist.c:97: warning: too few arguments for format contactlist.c:98: error: expected declaration or statement at end of input contactlist.c: In function ‘main’: contactlist.c:98: error: expected declaration or statement at end of input```
I'm not expecting anyone to count the lines on the code provided here to find where each of the errors/warnings are at, but I have some questions based on the errors/warnings.

-When it's warning me about the implicit declaration of functions, this obviously has to do with the arguments of the function prototype. What am I supposed to pass into the function? Should I not pass in anything since there's no input from the user to put into the argument that goes into the function?

-In the newContact function, do I have things going the right directions, as far as placing values and storing them into each of the structures? With all of the nesting going on, I'm not sure how one is read from the other.

-In the printContact function, I'm racking my brain on how to print the correct info. We use a union for either SSN, or StudentID because the contact will be defined by one, but not the other... so how can I get it to "know" which one to print out (I tried using the if and else if statements to do this)?

-Also in the printContact function, how do I get it to print out the correct data from all the nested structures? Do I have to try and access each field from the contacts structure? It keeps telling me that I don't have enough arguments to print out the data.

Any comments/criticism is definitely welcome. I can't find any good examples of nested structures and/or printing from them, so I'm having a hard time with this program.
• 03-01-2010
vivekraj
Post your full program.Because,I don't know how you're calling those functions defined.
• 03-01-2010
sganesh
Re: Problems with Nested Structures and Arrays of Structures
Do you know Debugger?....
If you know that I think you could debug your code very easily.
I tried some what. Still it contains some of the problem you can solve that using debugger.

Code:

```#include <stdio.h> void printContact (); typedef struct {                 int prefix,                     number,                     suffix;         } social;         typedef union{                 int SchoolID;                 social SSN;         } ident;         typedef struct {                 int area,                     prefix,                     suffix;         } phone_no;         struct contact_struct {                 ident ID;                 phone_no phone;         } contacts[3]; void newContact(struct contact_struct contacts[]); ident identify; social social_secnum; phone_no phone_number; int main (void) {         int start_menu;         int looping_val = 1;         while (looping_val == 1) {                 printf ("1) Enter a new contact\n2)Display the contacts\n3) Quit\n");                 scanf ("%d" , &start_menu);                 switch (start_menu)                 {                         case 1:                                 newContact(contacts);                                 break;                         case 2:                                 printContact ();                                 break;                         case 3:                                 looping_val = 0;                                 break;                         default :                                 printf ("Invalid input. Please try again.\n");                 }         return (0); } } void newContact(struct contact_struct contacts[]) {         char contact_menu, hyphen;         int i;         for (i = 0; i < 3 ; i++) {                 printf ("I) Enter the Student ID of this person\nS) Enter the SSN of this person\n");                 scanf ("%c" ,&contact_menu);                 switch (contact_menu) {                         case 'I':                         case 'i':                                 printf ("900\n");                                 scanf ("%d" , &identify.SchoolID);                                 contacts[i].ID.SchoolID = identify.SchoolID;                                 break;                         case 'S':                         case 's':                                 printf ("SSN: " );                                 scanf ("%d%c%d%c%d" , &social_secnum.prefix, &hyphen, &social_secnum.number, &hyphen, &social_secnum.suffix);                                 contacts[i].ID = identify;                                 break;                         default:                                 printf ("Invalid input. Please try again.");                                 i = 0;                                 break;                 }                 printf ("Phone #: ");                 scanf ("%d%c%d%c%d" , &phone_number.area, &hyphen, &phone_number.prefix, &hyphen, &phone_number.suffix);                 contacts[i].phone = phone_number;         } } void printContact () {         int i;         for (i = 1; i < 4; i++) {                 printf ("Contact #%d\n" , i);                 if (contacts[i].ID.SchoolID == identify.SchoolID) {                         printf ("900%d\n" , contacts[i-1].ID.SchoolID);                 }                 else if (contacts[i].ID.SSN.prefix == identify.SSN.prefix &&                         contacts[i].ID.SSN.number == identify.SSN.number &&                         contacts[i].ID.SSN.suffix == identify.SSN.suffix )                 {                         printf ("%d-%d-%d" , contacts[i-1].ID.SSN.prefix, contacts[i-1].ID.SSN.number, contacts[i-1].ID.SSN.suffix);                 }         printf ("(%d)-%d-%d" , contacts [i-1].phone); } }```
As a Programmer you should use Debugger.
Try to use Debugger in the future.
• 03-02-2010
Ignoramus
I took out parts of my full program, and just posted the target parts. I'd rather not post the whole program since it is an assignment...

Those functions should stand on their own, though. But here's the basic layout of the program

Code:

```#include <stdio.h> /*structures are here before main*/ void newContact(contact_struct contacts[3]);  /*prototype of function*/ void printContact();  /*prototype of function*/ int main(void) {               /*user menu here*/             switch (var)                           case '1':                                       newContact(contact_struct contacts[3]);                                       break;                         case '2':                                       printContact();                                       break;                         case '3':                                       /*quits program*/```
• 03-02-2010
vivekraj
You try the following code.It will do the requirement.

Code:

```#include <stdio.h> void printContact (); typedef struct {         int prefix,             number,             suffix; } social; typedef union{         int SchoolID;         social SSN; } ident; typedef struct {         int area,             prefix,             suffix; } phone_no; struct contact_struct {         ident ID;         phone_no phone; } contacts[3]; void newContact(struct contact_struct contacts[]); ident identify; social social_secnum; phone_no phone_number; int main (void) {         int start_menu;         int looping_val = 1;         while (looping_val==1) {                 printf ("1) Enter a new contact\n2) Display the contacts\n3) Quit \n");                 scanf ("%d" , &start_menu);                 switch (start_menu)                 {                         case 1:                                 newContact(contacts);                                 break;                         case 2:                                 printContact ();                                 break;                         case 3:                                 looping_val = 0;                                 break;                         default :                                 printf ("Invalid input. Please try again.\n");                 }         }                 return (0); } void newContact(struct contact_struct contacts[]) {         char contact_menu, hyphen;         int i;         getchar();         for (i = 0; i < 3 ; i++) {                 printf ("I) Enter the Student ID of this person\nS) Enter the SSN of this person\n");                 scanf ("%c" ,&contact_menu);                 switch (contact_menu) {                         case 'I':                         case 'i':                                 printf ("900");                                 scanf ("%d" , &identify.SchoolID);                                 contacts[i].ID.SchoolID = identify.SchoolID;                                 break;                         case 'S':                         case 's':                                 printf ("SSN: " );                                 scanf ("%d%c%d%c%d" , &social_secnum.prefix, &hyphen, &social_secnum.number, &hyphen, &social_secnum.suffix);                                 contacts[i].ID = identify;                                 break;                         default:                                 printf ("Invalid input. Please try again.");                                 i = 0;                                 break;                 }                 printf ("Phone#:");                 scanf ("%d%c%d%c%d" , &phone_number.area, &hyphen, &phone_number.prefix, &hyphen, &phone_number.suffix);                 getchar();                 contacts[i].phone = phone_number;         } } void printContact () {         int i;         for (i = 1; i < 4; i++) {                 printf ("Contact #%d\n" , i);                 if (contacts[i].ID.SchoolID == identify.SchoolID) {                         printf ("900%d\n" , contacts[i-1].ID.SchoolID);                 }                 else if (contacts[i].ID.SSN.prefix == identify.SSN.prefix &&                                 contacts[i].ID.SSN.number == identify.SSN.number &&                                 contacts[i].ID.SSN.suffix == identify.SSN.suffix )                 {                         printf ("%d-%d-%d\n" , contacts[i-1].ID.SSN.prefix, contacts[i-1].ID.SSN.number, contacts[i-1].ID.SSN.suffix);                 }                 printf ("(%d)-%d-%d\n" , contacts [i-1].phone);         } }```