malloc struct

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • palidhjede
    New Member
    • Feb 2010
    • 2

    malloc struct

    Hi I'm having difficulties trying to malloc a struct this is what it looks like and how I'm approaching the problem.

    Code:
    struct Records
    {
      char * fname;
      char * lname;
    };
    
    Records**   g_Recs; //Array of pointers
    
    
    void fakefunc()
    {
     g_Recs = (struct StudentRecords **) malloc (sizeof(struct StudentRecord*));
     g_Recs = (struct Records *) malloc (sizeof(struct StudentRecord));
    
     g_Recs->fname= (char *) malloc(strlen(buffer));
    
    
    }
    Thats a very basic example of my code. I get an error that ->fname must point to class/struct/union/generic type.

    I'm assuming that I am not mallocing the struct properly and thats is why im getting this erro. If anyone could help me with this, I would greatly appreciate it.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    struct Records
    {
    char * fname;
    char * lname;
    };

    Code:
    Records**   g_Recs; //Array of pointers
    This is not an array of pointers. This is one pointer that is a pointer to a Record.

    Remember, the name of the array is the address of element 0. Therefore, if g_Recs is an array of Records, then g_recs is the address of element 0. That makes g_recs the address of a Records.

    That is, g_recs is a Records*.

    To allocate an array of records you need to know how many Records are in the array. Let's say there are 20. So you allocate memory for 20 Records:

    Code:
    Records* g_recs = malloc(sizeof(Records) * 20);

    Comment

    • palidhjede
      New Member
      • Feb 2010
      • 2

      #3
      Thank you for the clarification, but I'm still unsure of the meaning of the double star **. As well I'm not sure how i would malloc one of the members of the struct. If you or someone could show me an example of mallocing a struct that is declared the way mine is (with the **) I would greatly appreciate it. Thanks for your time

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Code:
        struct Records {
            char *fname;
            char *lname;
        };
        
        // Relationship between object, pointer, and pointer-to-pointer.
        struct Records theRecord;
        struct Records *pRecord = &theRecord;
        struct Records **ppRecord = &pRecord;
        
        // How to access a record.
        char *pText;
        pText = theRecord.fname;
        pText = pRecord->fname;
        pText = (*ppRecord)->fname;   // I'm not sure about this one.
        Suppose sizeof(void*) on this platform is 4. Then sizeof(theRecor d) is 8, sizeof(pRecord) is 4, and sizeof(ppRecord ) is 4.

        I don't know if the parentheses are needed in the last pText example.

        In general to avoid confusion, you should have a very good reason for using double-start pointers-to-pointer. As a rule, you should never use triple-star pointers-to-pointer-to-pointer or beyond. There are exceptions but they don't arise often.

        What are you trying to do that led you to believe that a double-star pointer would be helpful?

        Comment

        Working...