Thread: another malloc of ptr in struct

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    62

    another malloc of ptr in struct

    Hi, revisiting the old malloc of pointer in a struct.
    This time the I am using a string and I am wandering around again.
    Here's what I have
    Code:
    #include "stdio.h"
    #include "math.h"
    #include "string.h"
    #include "stdio.h"
    
    #define MaxNameLen 512
    #define MaxNum 10
    
    struct Students
    {
        char **names[MaxNameLen + 1];
        char grades;
    };
    
    typedef struct Students Students;
    
    int main(int argc, char** argv)
    {
        int i,j;
        int numStudents;
        Students stu;
    
        do{
            printf( "Enter the number of students (1-10):  ");
            scanf( " %d", &numStudents);
        }while(numStudents <= MaxNum && numStudents > 0);
    
        printf( "Enter the %d names and grades:  ", numStudents);
        stu.names = malloc(numStudents * sizeof(names));
    
        for (i = 0; i < numStudents; i++)
        {
            stu.names[i] = malloc(stu.grades * sizeof(stu.names[i]));
            printf( "Name  %d: ", i + 1);
            fgets(stu.names[i],MaxNameLen + 1,stdin);
    
            for (j = 0; j < numStudents; j++)
            {
                printf( "Grade %d", j + 1);
                scanf( " %s", stu.grades);
                (stu.names[i][j] = stu.grades);
            }
        }
    
        for (i = 0; i < numStudents; i++)
        {
            for (j = 0; j < numStudents; j++)
            {
                printf("%s  ",stu.names[i]);
                printf( "%s", stu.names[i][j]);
            }
            printf("\n");
        }
    
        for (i = 0; i < numStudents; i++)
        {
            free(stu.names[i]);
        }
        free(names);
        return 0;
    }
    this is the trouble spot:
    Code:
    stu.names = malloc(numStudents * sizeof(names));
    I assume allocating char type make a difference.

    Thanks for any insight on this.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ehm, why do you need an array of pointer to pointer for what you are trying to do?

    It would make a whole lot more sense to have an array of [pointer to] struct student - as I presume grades is some sort of studen classification that is "one per student", right?

    The general idea of a struct is to "keep things that belong together in one container", e.g student and grades.

    Also, this doesn't do what you want, I think:
    Code:
    malloc(numStudents * sizeof(names))
    That will allocate numstudents times the size of pointer times (Maxnamelen + 1).

    I'm not entirely sure what you are tyring to do, so I guess you'd better explain what your goal is first, then we can perhaps come up with a plan for how to store things.

    I think you are a little bit confused about where and how to use dynamically allocated memory.

    --
    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
    Oct 2007
    Posts
    62
    Quote Originally Posted by matsp View Post

    I think you are a little bit confused about where and how to use dynamically allocated memory.

    --
    Mats
    agreed

    I am trying to accomplish this:
    -accept an arbitrary number of student names and grades
    -print formatted list of those names and grades.
    -Prompt for the number of students
    -loop to ensure the entered number is valid (between 1 and 10
    -Define a "struct Student" to hold the (char*) names and (float) grades
    -use a typedef so you can declare variables of type Student
    -Use malloc(), for all arrays, including the array of Student structs, to acquire and print the grades and names, and the arrays of characters to contain the names that are a part of each Student struct.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    62
    WAIT!!!

    I think I see a LITTLE clearer.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    62
    Nope I need help!

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    62
    I figured it out. At least I get it to work:

    Code:
    #include "stdio.h"
    #include "math.h"
    #include "string.h"
    #include "stdio.h"
    
    #define MaxNameLen 512
    #define MaxNum 10
    
    struct Students
    {
        char **names;
        float *grades;
    };
    
    typedef struct Students Students;
    
    int main(int argc, char** argv)
    {
        int i,j;
        int numStudents;
        Students stu;
    
        do{
            printf( "Enter the number of students (1-10):  ");
            scanf( " %d", &numStudents);
        }while(numStudents < 1 || numStudents > MaxNum);
    
        printf( "Enter the %d names and grades:\n", numStudents);
        for (i = 0; i < numStudents; i++)
        {
            stu.names[i] = malloc(numStudents * sizeof(MaxNameLen));
            printf( "Name  %d: ", i + 1);
            scanf(" %s", stu.names[i]);
            printf( "Grade %d: ", i + 1);
            scanf( " %f", &stu.grades[i]);
    
        }
        for (i = 0; i < numStudents; i++)
        {
                printf("%1.2f  %s  ",stu.grades[i], stu.names[i]);
                printf("\n");
        }
        for (i = 0; i < numStudents; i++)
        {
            free(stu.names[i]);
            stu.names = NULL;
            free(stu.grades[i]);
            stu.grades = NULL;
        }
        return 0;
    }
    Only question...is it efficient?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't see anything directly inefficient. However, allocating really small amounts of memory like you do, is in itself not particularly efficient.

    Also, this is a bit "strange":
    Code:
    malloc(numStudents * sizeof(MaxNameLen));
    You are allocating one for each student, and the size is number of students, times size of integer (usually 4) - strange concoction, not to mention that if number of students is small, the length of the name don't have to be huge to go outside the allocated memory.

    On the other hand, you don't allocate ANY memory for "grades". You probably should.

    --
    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.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    62
    Okay, I think I am slowly getting the gist. Thank you for your guidance!!!

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. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  4. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  5. Replies: 16
    Last Post: 10-29-2006, 05:04 AM