how to use malloc with a struct?

This is a discussion on how to use malloc with a struct? within the C Programming forums, part of the General Programming Boards category; Ok here is a portion of my code... Code: struct studentGrades2 { char lastName[30]; char firstName[30]; int grade1, grade2, grade3, ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    11

    how to use malloc with a struct?

    Ok here is a portion of my code...

    Code:
    struct studentGrades2
    {
         char lastName[30];
         char firstName[30];
         int grade1, grade2, grade3, grade4, grade5;      
    };
    
    int numberOfRecords = 11;
    
    struct studentGrades2 *grades2;
    
    grades2 = malloc(numberOfRecords * sizeof(int));
    
    grades2[1].lastName = "morris";
    Every time I try to compile I get the message: Incompatible types in assignment, where I try to set the lastName = to "morris"

    What am I doing wrong? I've been at this for hours
    Last edited by allplay; 09-13-2006 at 03:48 PM.

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    11
    and why does the same code work when I put in...

    grades[1].grade1 = 1


    does malloc just work for ints?

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >grades[1].lastName = "morris";
    Use strcpy to copy strings:
    Code:
    #include <string.h>
    .
    .
    strcpy(grades[0].lastName, "morris");
    And arrays start at 0, not 1.

    And check your malloc call, you're not allocating enough memory for your structure.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    11
    wow I'm stupid....


    I've never used malloc before, how to I find out how to allocate enough memory? There are 11 pieces of data in the file.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >grades2 = malloc(numberOfRecords * sizeof(int));
    The part in red is where you want to tell malloc how big each record is. Let the compiler do the work for you by taking the sizeof the first element of the array.
    Code:
    grades2 = malloc(numberOfRecords * sizeof(*grades2));

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >grades[1].
    I also notice you have grades instead of grades2, which is your declaration. I'd change the name of the array to grades.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    11
    That grades[1] was just an error when posting.

    But you've been a HUGE help swoopy, thanks alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list question
    By brb9412 in forum C Programming
    Replies: 16
    Last Post: 01-04-2009, 04:05 PM
  2. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-03-2008, 12:10 AM
  3. Help with malloc and a complex struct
    By mc61 in forum C Programming
    Replies: 2
    Last Post: 01-14-2008, 12:12 PM
  4. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

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