Thread: help with structs and pointers in C++

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    3

    Question help with structs and pointers in C++

    Ok this is my situation I have this header file:

    Code:
    struct dir
    {
       char name[3];
       int root_dir;
       int has_children;
       int num_children;
       int offset_to_children[2];
       int offset_to_files[16];
    };
    struct files
    {
       char name[5];
       int size;
       int offset_to_beginning;
       int has_fragment;
       int next_fragment;
    };
    struct fat
    {
       int files;
       int dirs;
       struct dir *dir_array;
       struct files *file_array;
    };
    In my program if I want to access the struct fat member *dir_array or *file_array, given that this member are pointers to another struct how can I access this members from my main program? This is what I'm doing and it compiles:

    Code:
    fat *pfat;
    pfat->dir_array->root_dir=0;
    My doubt is if I'm doing it right. Can anybody clarify my doubt and point my to the right direction. Thanks!!!

  2. #2
    Registered User
    Join Date
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92
    Any pointers inside of structs start pointing to nothing. You have to allocate space for them:

    Code:
    fat pfat;
    pfat->dir_array = (dir *)malloc(sizeof(dir));
    pfat->dir_array->root_dir=0;
    EDIT: If you want an array of dir or files you have to multiply by the size of the array
    Code:
    fat pfat;
    int array_size = 4;
    pfat->dir_array = (dir *)malloc(sizeof(dir) * array_size);
    pfat->dir_array[0]->root_dir = 0;
    If you want fat to be a pointer you have to allocate that as well... Also make sure you create a free function for your structure that first frees any allocated members (dir_array or files_array) then frees the struct itself.
    Code:
    fat *pfat = (fat *)malloc(sizeof(fat));
    Last edited by 127.0.0.1; 05-24-2011 at 07:56 PM.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Are you using C or C++?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Quote Originally Posted by casper29 View Post
    My doubt is if I'm doing it right. Can anybody clarify my doubt and point my to the right direction. Thanks!!!
    You are doing it correctly. The rule is that structs use "." to access members, while pointers (to structs) use "->" to access members. This is the sort of error your compiler will safely catch when you mess up.
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with pointers and structs in C
    By omega666 in forum C Programming
    Replies: 12
    Last Post: 04-17-2011, 09:59 PM
  2. passing structs & pointers to structs as arguments
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 03-16-2004, 07:14 PM
  3. need help with structs and pointers
    By rozner in forum C Programming
    Replies: 4
    Last Post: 04-07-2003, 03:33 PM
  4. structs like pointers?
    By mart_man00 in forum C Programming
    Replies: 5
    Last Post: 03-14-2003, 03:16 AM
  5. Pointers to structs
    By csmatheng in forum C Programming
    Replies: 2
    Last Post: 09-11-2002, 03:47 AM

Tags for this Thread