Thread: malloc() with array of structures

  1. #1
    Registered User khpuce's Avatar
    Join Date
    May 2003
    Posts
    165

    Question malloc() with array of structures

    Ok...its been two days now and I can't get it yet! I googled for long hours but couldn't find what exactly I was looking for. So, here it is -

    Say, I have a structure with only one element. (I'll add more elements to the structure once I get it to work!)

    Code:
    typedef struct
    {
       int roll;
    }STUDENT;
    Now when the program is run, I want the user to specify the number of students and then allocate the necessary memory using malloc(). So, I did this -

    Code:
        int num;
        REC *pt;
        pt= (REC *) malloc (num*sizeof(REC));  // I also tried this without casting!
    Now, I am having difficulties in stroing the values and then displaying those values back to the screen. I used the following code -

    Code:
    	printf("\n* * * * Enter roll no of students. * * * *\n");
    	for (i=0;i<num;i++)
    	{
    		printf("-->");
                   scanf("%d",(pt+i)->roll);
    	}
    
    	printf("\n* * * * Entered roll no. * * * *\n");
    	for (i=0;i<num;i++)
    	{	
                  printf("%d, ",*(pt+i)->roll);     //This is where the error occures!
    	}
    when I try do compile it, I get the error "invalid type argument of `unary *'".

    How can I solve this?

    P.S. I am using wxdev C++.
    Last edited by khpuce; 10-25-2010 at 11:25 PM.

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Don't get too fancy with pointer arithmetic. It's pt[i].roll. If you really really really want to do the pointer arithmetic explicitly, you will have to do this:
    Code:
    *((REC*)(pt+i))->roll
    This is read as "take pt + i, cast the result to pointer to REC, and dereference it to get the member called 'roll'"
    Last edited by QuadraticFighte; 10-25-2010 at 11:46 PM.

  3. #3
    Registered User khpuce's Avatar
    Join Date
    May 2003
    Posts
    165
    Thank you very much QuadraticFighte for your ultra quick reply

    I did try using pt[i].roll. It compiles with no errors but after taking the inputs, the program crashes! Not sure whether this is related with wxdev C++ or not.

    Again, when I use
    Code:
    printf("%d, ", *((REC*)(pt+i))->roll);
    I get the previously mentioned error i.e. "invalid type argument of `unary *'

    So, still no sign of light
    Last edited by khpuce; 10-26-2010 at 12:52 AM.

  4. #4
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    can you attach your code ?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This:
    Code:
    int num;
    REC *pt;
    pt= (REC *) malloc (num*sizeof(REC));  // I also tried this without casting!
    should be:
    Code:
    int num = 10; /* or whatever number is required */
    REC *pt = malloc(num * sizeof(*pt));
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User khpuce's Avatar
    Join Date
    May 2003
    Posts
    165
    Quote Originally Posted by laserlight View Post
    This:
    Code:
    int num = 10; /* or whatever number is required */
    num is assigned a value during runtime by the user.

    Code:
    REC *pt = malloc(num * sizeof(*pt));
    The program still crashes after taking the inputs (Perhaps its a problem of the IDE I am using!)

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should post the smallest and simplest compilable program that demonstrates the crash.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User khpuce's Avatar
    Join Date
    May 2003
    Posts
    165
    ok...it seems to work...FINALLY !

    Here's the code
    Code:
    typedef struct
    {
        int roll;
    }REC;
    
    int main()
    {
        int i, n =3; 
        REC *pt= malloc(n*sizeof(*pt));
        printf("\n* * * * Enter roll no of students. * * * *\n");
        for (i=0;i<n;i++)
       {
    	printf("-->");
    	scanf("%d", &pt[i].roll);
        }
        printf("\n* * * * Entered roll no. * * * *\n");
        for (i=0;i<n;i++)
       {
    	printf("%d, ", pt[i].roll);   
        }
        free(pt);
        getche();
        return 0;
    }
    I changed the code so many times that I cannot remember what I did in the first place ! However, I have one question
    Code:
    REC *pt= malloc(n*sizeof(*pt));
    Why use sizeof(*pt) instead of sizeof(REC) ? or is it same thing?

    Thanks to everyone for your kind help

  9. #9
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Quote Originally Posted by khpuce View Post
    Why use sizeof(*pt) instead of sizeof(REC) ? or is it same thing?
    If you change the type of pt to another type, no change on sizeof parameter is needed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 60
    Last Post: 01-09-2009, 01:09 PM
  2. Initializing array of structures causing bus error
    By Aaron M in forum C Programming
    Replies: 5
    Last Post: 03-05-2006, 01:40 AM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. filling an array of structures?
    By voodoo3182 in forum C Programming
    Replies: 9
    Last Post: 08-06-2005, 05:29 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM