Creating array of structs

This is a discussion on Creating array of structs within the C++ Programming forums, part of the General Programming Boards category; I'm having some trouble using an array of structs. I need to allocate sufficient memory then pass a pointer to ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Location
    Oxford, England
    Posts
    27

    Creating array of structs

    I'm having some trouble using an array of structs. I need to allocate sufficient memory then pass a pointer to this to a function that will create the array of structs, so that I can use the pointer later to search this array. It looks something like this (I've cut out much that is irrelevant):

    Code:
    typedef struct 
    {
      char filename[128];
      char group[32];
      int type;
    } Experiment;
    
    void fill(Experiment * exptarr)
    {
      int count = 0;
      // parse a file to extract the data for each of maxexpts Experiments,
      // and attempt to copy the data to the array of structs
      {
        Experiment experiment;
        strcpy(experiment.filename,filename);
        strcpy(experiment.group,group);
        experiment.type = type;
        exptarr[count] = experiment;
        count++;
      }
    }
    
    int main(int argc, char * argv[])
    {
      Experiment * exptarr;
      int maxexpts = 0;
      // code to count the maximum number of experiments goes here,
      // and updates maxexpts
      exptarr = malloc(sizeof(Experiment) * maxexpts);
      fill(&exptarr);
      // expect to find that exptarr is an pointer to an array of Experiment structs...
    }
    This doesn't work at all, probably because I don't understand the syntax properly (I'm not a C or C++ programmer) or have perpetrated some other basic error(s). Would someone mind pointing the error(s) out?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    fill(&exptarr);
    has one too many levels of indirection - remove the &. exptarr is already a pointer, and your function expects a pointer. If you enable warnings (or even compile as C++ which is the forum you are writing in), it should tell you that you have the wrong pointer type.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Location
    Oxford, England
    Posts
    27
    Thanks.
    I have been compiling it as C++ and it tells me that at the "malloc" line that I'm trying an invalid conversion from 'void*' to 'experiment*'. What could be causing that?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by knirirr View Post
    Thanks.
    I have been compiling it as C++ and it tells me that at the "malloc" line that I'm trying an invalid conversion from 'void*' to 'experiment*'. What could be causing that?
    Exactly what it says: You are trying to convert between a void * to a experiment * - in C that is valid (any pointer can be converted to void * in both C and C++, but C allows you to convert a void * to any type of pointer, C++ doesn't let you do that). You can overcome this by either adopting the C++ style of using new, or by casting the pointer type to experiment *.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Location
    Oxford, England
    Posts
    27
    Quote Originally Posted by matsp View Post
    You can overcome this by either adopting the C++ style of using new, or by casting the pointer type to experiment *.
    Would you mind telling me the syntax for either of these methods, or where I might find information on it? I am not having any success with what I think it should be (e.g. (Experiment)* malloc...).

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    (Experiment *) malloc. The type is "pointer to Experiment", a/k/a Experiment *.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Location
    Oxford, England
    Posts
    27
    Thanks - that now works. Perhaps if I had tried learning C before scripting languages this might be rather less confusing...

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by knirirr View Post
    Thanks - that now works. Perhaps if I had tried learning C before scripting languages this might be rather less confusing...
    Possibly, but it's probable that you are just going through the learning curve of pointers, memory allocation and generally understanding C as a language.

    But yes, many of the higher level languages do not bother with letting you allocate memory, or have pointers to data. It just "automagically works anyways".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    The C++ version using new/delete:
    Code:
    Experiment * exptarr;
    
    ...
    
    exptarr = new Experiment[maxexpts];
    
    ...
    
    delete [] exptarr;
    You should be using free after you are done using you're pointer if using malloc to allocate memory.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    Code:
      // parse a file to extract the data for each of maxexpts Experiments,
      // and attempt to copy the data to the array of structs
      {
        Experiment experiment;
        strcpy(experiment.filename,filename);
        strcpy(experiment.group,group);
        experiment.type = type;
        exptarr[count] = experiment;
        count++;
      }
    Well don't do it that way. The experiment array already has N structs to be filled. What you're doing here is assigning pointers some dummy info from local variables which don't persist after the function returns. You're better off just using exptarr[count] as your memory.

    Even if you do it your way and it appears to work, you've leaked memory.

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    2
    nice program

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> The C++ version using new/delete:
    I would rather show the C++ version using vector. There's no reason to use new/delete for this.

  13. #13
    Registered User
    Join Date
    Jan 2008
    Location
    Oxford, England
    Posts
    27
    Quote Originally Posted by citizen View Post

    Even if you do it your way and it appears to work, you've leaked memory.
    A good point - now corrected, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a Spreadsheet using a multidimensional array
    By Apiatan in forum C++ Programming
    Replies: 1
    Last Post: 03-21-2009, 04:18 PM
  2. Getting the size of an array of structs
    By steve1_rm in forum C++ Programming
    Replies: 3
    Last Post: 12-17-2008, 05:29 AM
  3. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  4. Creating a menu that reads input via an array?
    By Nalif in forum C Programming
    Replies: 6
    Last Post: 09-29-2006, 09:21 PM
  5. Creating an array of object pointers
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2001, 10:01 PM

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