Confused with Structs and pointers

This is a discussion on Confused with Structs and pointers within the C Programming forums, part of the General Programming Boards category; Hi, Im trying to print out Structs within structs...but Im confused in terms of how you call it from within ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    20

    Confused with Structs and pointers

    Hi,

    Im trying to print out Structs within structs...but Im confused in terms of how you call it from within a printf statement.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    
    struct s_room{
    char *room;
    int stock;
    };
    
    struct s_addition {
      char *name;
      int volumetype;
    struct s_room *stock; //sub types
    };
    
    
    
    struct s_additions_type {
      char *name; //type of addition
      struct s_addition *type; //sub types
    };
    
    struct s_addition_volume {
      char *name;
      int volume;
    };
    
    int main(int argc, char *argv[])
    {
      struct s_additions_type *additions_list;
      struct s_addition_volume *addition_volumes;
    struct s_room *addition_rooms;
    
      addition_rooms = malloc(sizeof(struct s_room) * (3 + 1));
    
      addition_rooms[0].room = malloc(sizeof(char) * (strlen("3p11") + 1));
      strcpy(addition_rooms[0].room,"3p11");
    
        addition_rooms[0].room = malloc(sizeof(char) * (strlen("3p27") + 1));
      strcpy(addition_rooms[0].room,"3p27");
    
          addition_rooms[0].room = malloc(sizeof(char) * (strlen("3p28") + 1));
      strcpy(addition_rooms[0].room,"3p28");
    
      addition_volumes = malloc(sizeof(struct s_addition_volume) * (5 + 1));
    
    /* setup number types */
      addition_volumes[0].name = malloc(sizeof(char) * (strlen("dash") + 1));
      strcpy(addition_volumes[0].name,"dash");
      addition_volumes[0].volume=1;
    
      addition_volumes[1].name = malloc(sizeof(char) * (strlen("splash") + 1));
      strcpy(addition_volumes[1].name,"splash");
      addition_volumes[1].volume=2;
    
      addition_volumes[2].name = malloc(sizeof(char) * (strlen("little") + 1));
      strcpy(addition_volumes[2].name,"little");
      addition_volumes[2].volume=3;
    
      addition_volumes[3].name = malloc(sizeof(char) * (strlen("medium") + 1));
      strcpy(addition_volumes[3].name,"medium");
      addition_volumes[3].volume=4;
    
      addition_volumes[4].name = malloc(sizeof(char) * (strlen("lots") + 1));
      strcpy(addition_volumes[4].name,"lots");
      addition_volumes[4].volume=5;
    
    
    
    
      additions_list = malloc(sizeof(struct s_additions_type) * (5 + 1));
    
      /* setup milk types */
      additions_list[0].name = malloc(sizeof(char) * (strlen("milk") + 1));
      strcpy(additions_list[0].name,"milk");
    
      additions_list[0].type = malloc(sizeof(struct s_addition) * (5 + 1));
    
      additions_list[0].type[0].name = malloc(sizeof(char) * (strlen("cream") + 1));
      strcpy(additions_list[0].type[0].name,"cream");
      additions_list[0].type[0].volumetype = 1;
      additions_list[0].type[0].stock[0].stock = 10;
      additions_list[0].type[0].stock[1].stock = 10;
        additions_list[0].type[0].stock[2].stock = 10;
    
    
      additions_list[0].type[1].name = malloc(sizeof(char) * (strlen("whole-milk") + 1));
      strcpy(additions_list[0].type[1].name,"whole-milk");
      additions_list[0].type[1].volumetype = 1;
      additions_list[0].type[1].stock = 5;
    
      additions_list[0].type[2].name = malloc(sizeof(char) * (strlen("part-skim") + 1));
      strcpy(additions_list[0].type[2].name,"part-skim");
      additions_list[0].type[2].volumetype = 1;
      additions_list[0].type[2].stock = 5;
    
      additions_list[0].type[3].name = malloc(sizeof(char) * (strlen("skim") + 1));
      strcpy(additions_list[0].type[3].name,"skim");
      additions_list[0].type[3].volumetype = 1;
      additions_list[0].type[3].stock = 5;
    
      additions_list[0].type[4].name = malloc(sizeof(char) * (strlen("non-dairy") + 1));
      strcpy(additions_list[0].type[4].name,"non-dairy");
      additions_list[0].type[4].volumetype = 1;
      additions_list[0].type[4].stock = 5;
    
      additions_list[0].type[5].name = NULL;
      additions_list[0].type[5].volumetype = 0;
      additions_list[0].type[5].stock = 0;
    
      /* setup syrup types*/
      additions_list[1].name = malloc(sizeof(char) * (strlen("syrup") + 1));
      strcpy(additions_list[1].name,"syrup");
    
      additions_list[1].type = malloc(sizeof(struct s_addition) * (4 + 1));
    
      additions_list[1].type[0].name = malloc(sizeof(char) * (strlen("vanilla") + 1));
      strcpy(additions_list[1].type[0].name,"vanilla");
      additions_list[1].type[0].volumetype = 0;
      additions_list[1].type[0].stock = 5;
    
     additions_list[1].type[1].name = malloc(sizeof(char) * (strlen("almond") + 1));
      strcpy(additions_list[1].type[1].name,"almond");
      additions_list[1].type[1].volumetype = 0;
      additions_list[1].type[1].stock = 5;
    
      additions_list[1].type[2].name = malloc(sizeof(char) * (strlen("raspberry") + 1));
      strcpy(additions_list[1].type[2].name,"raspberry");
      additions_list[1].type[2].volumetype = 0;
      additions_list[1].type[2].stock = 5;
    
      additions_list[1].type[3].name = malloc(sizeof(char) * (strlen("chocolate") + 1));
      strcpy(additions_list[1].type[3].name,"chocolate");
      additions_list[1].type[3].volumetype = 0;
      additions_list[1].type[3].stock = 5;
    
      additions_list[1].type[4].name = NULL;
      additions_list[1].type[4].volumetype = 0;
      additions_list[1].type[4].stock = 0;
    
      /* setup sweetner types*/
      additions_list[2].name = malloc(sizeof(char) * (strlen("sweetner") + 1));
      strcpy(additions_list[2].name,"sweetner");
    
      additions_list[2].type = malloc(sizeof(struct s_addition) * (4 + 1));
    
      additions_list[2].type[0].name = malloc(sizeof(char) * (strlen("white-sugar") + 1));
      strcpy(additions_list[2].type[0].name,"white-sugar");
      additions_list[2].type[0].volumetype = 0;
      additions_list[2].type[0].stock = 5;
    
      additions_list[2].type[1].name = malloc(sizeof(char) * (strlen("sweetner") + 1));
      strcpy(additions_list[2].type[1].name,"sweetner");
      additions_list[2].type[1].volumetype = 0;
      additions_list[2].type[1].stock = 5;
    
      additions_list[2].type[2].name = malloc(sizeof(char) * (strlen("raw-cane") + 1));
      strcpy(additions_list[2].type[2].name,"raw-cane");
      additions_list[2].type[2].volumetype = 0;
      additions_list[2].type[2].stock = 5;
    
      additions_list[2].type[3].name = malloc(sizeof(char) * (strlen("honey") + 1));
      strcpy(additions_list[2].type[3].name,"honey");
      additions_list[2].type[3].volumetype = 0;
      additions_list[2].type[3].stock = 5;
    
      additions_list[2].type[4].name = NULL;
      additions_list[2].type[4].volumetype = 0;
      additions_list[2].type[4].stock = 0;
    
    /* setup spice types*/
      additions_list[3].name = malloc(sizeof(char) * (strlen("spice") + 1));
      strcpy(additions_list[3].name,"spice");
    
      additions_list[3].type = malloc(sizeof(struct s_addition) * (2 + 1));
    
      additions_list[3].type[0].name = malloc(sizeof(char) * (strlen("cinnamon") + 1));
      strcpy(additions_list[3].type[0].name,"cinnamon");
      additions_list[3].type[0].volumetype = 0;
      additions_list[3].type[0].stock = 5;
    
      additions_list[3].type[1].name = malloc(sizeof(char) * (strlen("cardamon") + 1));
      strcpy(additions_list[3].type[1].name,"cardamon");
      additions_list[3].type[1].volumetype = 0;
      additions_list[3].type[1].stock = 5;
    
      additions_list[3].type[2].name = NULL;
      additions_list[3].type[2].volumetype = 0;
      additions_list[3].type[2].stock = 0;
    
    /* setup alcohol types*/
      additions_list[4].name = malloc(sizeof(char) * (strlen("alcohol") + 1));
      strcpy(additions_list[4].name,"alcohol");
    
      additions_list[4].type = malloc(sizeof(struct s_addition) * (5 + 1));
    
      additions_list[4].type[0].name = malloc(sizeof(char) * (strlen("brandy") + 1));
      strcpy(additions_list[4].type[0].name,"brandy");
      additions_list[4].type[0].volumetype = 1;
      additions_list[4].type[0].stock = 5;
    
      additions_list[4].type[1].name = malloc(sizeof(char) * (strlen("whisky") + 1));
      strcpy(additions_list[4].type[1].name,"whisky");
      additions_list[4].type[1].volumetype = 1;
      additions_list[4].type[1].stock = 5;
    
      additions_list[4].type[2].name = malloc(sizeof(char) * (strlen("rum") + 1));
      strcpy(additions_list[4].type[2].name,"rum");
      additions_list[4].type[2].volumetype = 1;
      additions_list[4].type[2].stock = 5;
    
      additions_list[4].type[3].name = malloc(sizeof(char) * (strlen("kahlua") + 1));
      strcpy(additions_list[4].type[3].name,"kahlua");
      additions_list[4].type[3].volumetype = 1;
      additions_list[4].type[3].stock = 5;
    
    additions_list[4].type[4].name = malloc(sizeof(char) * (strlen("aquavit") + 1));
      strcpy(additions_list[4].type[4].name,"aquavit");
      additions_list[4].type[4].volumetype = 0;
      additions_list[4].type[4].stock = 5;
    
      additions_list[4].type[5].name = NULL;
      additions_list[4].type[5].volumetype = 1;
      additions_list[4].type[5].stock = 0;
    
    /* setup end marker */
      additions_list[5].name = NULL;
      additions_list[5].type = NULL;
    
    
    
       int i,m,s;
    
    
      for (i=0;additions_list[i].name!=NULL;i++) {
      printf("%s\n",additions_list[i].name);
      for (m=0;additions_list[i].type[m].name!=NULL;m++) {
        //printf("\t%s %d\n",additions_list[i].type[m].name,additions_list[i].type[m].stock);
         printf("\t%s %s\n",additions_list[i].type[m].name,additions_list[i].type[m]);
      }
       for (s=0;additions_list[i].type[m].name.stock[s]!=NULL;s++) {
        //printf("\t%s %d\n",additions_list[i].type[m].name,additions_list[i].type[m].stock);
         printf("\t\t%s %s %d\n",additions_list[i].type[m].name,additions_list[i].stock[s].stock);
       }
    }
    
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Can you narrow down the portion of the code you're confused about?

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    20
    The printf's at the bottom...

    I am also getting a few errors when I compile it.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Post the compile-time errors you are getting. Can you narrow it down to a single printf() to make it easier?

  5. #5
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Code:
    typdef struct _foo
    {
           int stuff;
    }foo;
    
    typedef struct _bar
    {
          foo foobar;
          foo *barfoo;
    }bar;
    
    ...
    
    bar *FilthyBar, SleezyBar;
    
    FilthyBar = malloc(sizeof(bar));
    
    FilthyBar->foobar.stuff = 2;
    FilthyBar->barfoo = malloc(sizeof(foo));
    
    SleezyBar.foobar.stuff = 10;
    SleezyBar.barfoo = malloc(sizeof(foo));
    SleezyBar.barfoo->stuff = 20;
    
    printf("%d", FilthyBar->barfoo->stuff); // Both are pointers to a struct, thus ->
    printf("%d", FilthyBar->foobar.stuff); // foobar is not a pointer, thus the period .
    
    printf("%d", SleezyBar.foobar.stuff); // Neither are pointers to struct
    printf("%d", SleezyBar.barfoo->stuff); // Barfoo is a pointer
    The basic idea behind structs and pointers to struct is very simple: If it's not a pointer to a struct, then it's the period you need to access the members. If it's a pointer to a struct, you use the -> operator. The same thing applies to structs inside of structs. If you have a struct, not a pointer to one, that holds a pointer to a struct, you access the pointer to the struct with the period, and the members that the pointer points to with the -> operator(Wow, that was long).

    Hope that clears it up.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structs, pointers and functions
    By osici in forum C Programming
    Replies: 2
    Last Post: 04-29-2009, 12:35 AM
  2. Confused with pointers and structs
    By jalex39 in forum C Programming
    Replies: 6
    Last Post: 10-12-2008, 02:22 PM
  3. passing structs & pointers to structs as arguments
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 03-16-2004, 06:14 PM
  4. pointers to pointers within structs
    By Lord_azrael99 in forum C Programming
    Replies: 2
    Last Post: 08-28-2003, 04:29 AM
  5. structs like pointers?
    By mart_man00 in forum C Programming
    Replies: 5
    Last Post: 03-14-2003, 02:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21