Thread: Nested structs

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    17

    Nested structs

    I am having trouble getting my nested structs to print. I want the phone struct to print, but I'm not quite sure how to format it. I'm thinking it's what I did when I actually created the struct, but I could be wrong. Another problem I'm having is when I'm actually calling the print functions. More specifically, the pointer print function prEmps2. I just don't know how to get it to call properly. I've highlighted the areas that I've been struggling with.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAXNAME     40
    #define MAXDIGITS   27
    
    struct phone
       {
         char areacode[MAXDIGITS];
         char localprefix[MAXDIGITS];
         char finalfour[MAXDIGITS];
       } ;
    
    struct employee
    {
       long number;
       char name[MAXNAME];
       struct phone areacode, localprefix, finalfour;
       int age;
    } ;
    
    int main(void)
    {
      void printEmps(struct employee table[], int);
      void peEmps2(struct employee *ptr, int);
      
      struct employee emptab[] =
      {
         {1986, "mario, super",   "758", "748", "2634", 64},
         {2012, "man, super",     "423", "436", "2353", 98},
      };
      
      printEmps(emptab,sizeof(emptab)/sizeof(emptab[0]));
      prEmps2()
      
      system("PAUSE");	
      return 0;
    }
    
    void printEmps(struct employee table[], int n)
    {
       int i;
       
       for (i = 0; i < n; i++)
          printf("Employee Number: %s\n Name: %s\n Phone: (%s)%s-%s\n Age: %i\n",
          table[i].number, table[i].name, table[i].phone areacode,
          table[i].phone localprefix, table[i].phone finalfour, table[i].age);
    }
    
    void prEmps2(struct employee *ptr, int n)
    {
       struct employee *endptr = ptr + n;
       
       for (; ptr < endptr; ptr++)
          printf("Name: %s\n", ptr->name);
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Start by fixing the basic syntax errors so your code compiles. Consider upping the warning levels on your compiler.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    7

    guess

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAXNAME     40
    #define MAXDIGITS   27
    
    struct phone
       {
         char areacode[MAXDIGITS];
         char localprefix[MAXDIGITS];
         char finalfour[MAXDIGITS];
       } ;
    
    struct employee
    {
       long number;
       char name[MAXNAME];
       struct phone phone1;
       int age;
    } ;
    
    int main(void)
    {
      void printEmps(struct employee table[], int);
      void prEmps2(struct employee *ptr, int);
      
      struct employee emptab[] =
      {
         {1986, "mario, super",   "758", "748", "2634", 64},
         {2012, "man, super",     "423", "436", "2353", 98},
      };
      
      printEmps(emptab, sizeof(emptab)/sizeof(emptab[0]));
      prEmps2(emptab, sizeof(emptab)/sizeof(emptab[0]));
      
      system("PAUSE");	
      return 0;
    }
    
    void printEmps(struct employee table[], int n)
    {
       int i;
       
       for (i = 0; i < n; i++)
          printf("Employee Number: %d\n Name: %s\n Phone: (%s)%s-%s\n Age: %i\n",
          table[i].number, table[i].name, table[i].phone1.areacode,
          table[i].phone1.localprefix, table[i].phone1.finalfour, table[i].age);
    }
    
    void prEmps2(struct employee *ptr, int n)
    {
       struct employee *endptr = ptr + n;
       
       for (; ptr < endptr; ptr++)
          printf("Name: %s\n", ptr->name);
    }

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    17
    But what does this mean?

    52 -- conflicting types for 'prEmps2'
    35 -- previous implicit declaration of 'prEmps2' was here

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    22
    Quote Originally Posted by jeremy75
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAXNAME     40
    #define MAXDIGITS   27
    
    struct phone
       {
         char areacode[MAXDIGITS];
         char localprefix[MAXDIGITS];
         char finalfour[MAXDIGITS];
       } ;
    
    struct employee
    {
       long number;
       char name[MAXNAME];
       struct phone phone1;
       int age;
    } ;
    
    int main(void)
    {
      void printEmps(struct employee table[], int);
      void prEmps2(struct employee *ptr, int);
      
      struct employee emptab[] =
      {
         {1986, "mario, super",   "758", "748", "2634", 64},
         {2012, "man, super",     "423", "436", "2353", 98},
      };
      
      printEmps(emptab, sizeof(emptab)/sizeof(emptab[0]));
      prEmps2(emptab, sizeof(emptab)/sizeof(emptab[0]));
      
      system("PAUSE");	
      return 0;
    }
    
    void printEmps(struct employee table[], int n)
    {
       int i;
       
       for (i = 0; i < n; i++)
          printf("Employee Number: %d\n Name: %s\n Phone: (%s)%s-%s\n Age: %i\n",
          table[i].number, table[i].name, table[i].phone1.areacode,
          table[i].phone1.localprefix, table[i].phone1.finalfour, table[i].age);
    }
    
    void prEmps2(struct employee *ptr, int n)
    {
       struct employee *endptr = ptr + n;
       
       for (; ptr < endptr; ptr++)
          printf("Name: %s\n", ptr->name);
    }

    can you see what's wrong?

    its missing the name of variable on the declaration of the function(red marker).

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    missing the name of variable on the declaration of the function(red marker).
    it is optional
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested structs
    By bandal27 in forum C Programming
    Replies: 26
    Last Post: 12-13-2008, 09:14 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. Nested Structs
    By Monkey83 in forum C Programming
    Replies: 2
    Last Post: 09-15-2006, 11:03 AM
  4. accessing members in nested structs
    By amidstTheAshes in forum C Programming
    Replies: 2
    Last Post: 03-23-2005, 02:00 PM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM