build an array of pointers to structures and dinam allocate strings within the struct

This is a discussion on build an array of pointers to structures and dinam allocate strings within the struct within the C Programming forums, part of the General Programming Boards category; Hello I have the following code hoping to build an array of pointers to structures and dinamically allocate strings within ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Location
    Dallas
    Posts
    6

    build an array of pointers to structures and dinam allocate strings within the struct

    Hello I have the following code hoping to build an array of pointers to structures and dinamically allocate strings within the structures. Any help will suffice!

    Code:
    #include "v4.h"
    
    int main()
    {
    
    
        int i;
        int num;
        studentRecord *student;
    
    
        scanf("%d",&num);
        for(i = 0; i < num; i++)
        {
            student = (studentRecord *) malloc (sizeof(studentRecord));
            scanf("%s %s %s %d %d %d",student->lastName, student->firstName, 
                   student->idNum, &student->exam1, &student->exam2, 
                   &student->exam3);
    
    
            student->avg = (student->exam1 + student->exam2 + student->exam3)/3.;
            if( student->avg >= 90.0 ) student->grade = 'A';
            else if( student->avg >= 80.0 ) student->grade = 'B';
            else if( student->avg >= 70.0 ) student->grade = 'C';
            else if( student->avg >= 60.0 ) student->grade = 'D';
            else student->grade = 'F';
    
    
            printf("%s, %s: %s, %s \n\t %4d %4d %4d %s %6.2f %s  %c\n",
                   student->lastName, student->firstName, student->idNum, 
                   "has exams scores of:", student->exam1, student->exam2, 
                   student->exam3, "for an average of ", student->avg,
                   " and a grade of ",student->grade); 
        }
        return 0; 
    }
    v4.h file

    Code:
    #include <stdio.h>
    
    
    typedef struct {
            char lastName[30];
            char firstName[30];
            char idNum[9];
            int exam1;
            int exam2;
            int exam3;
            float avg;
            char grade;
        }studentRecord;
    Last edited by mz0086; 04-02-2012 at 07:24 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So where's the question here?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I would normally mention that dynamic is spelled with a y, but I'm trying to cut down.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Feb 2012
    Location
    Dallas
    Posts
    6
    How to build an array of pointers to structures using what i have?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by mz0086 View Post
    How to build an array of pointers to structures using what i have?
    Pretty simple:
    Code:
    studentRecord *student;
    Becomes:
    Code:
    studentRecord *student[ SOME_SIZE ];

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Feb 2012
    Location
    Dallas
    Posts
    6
    Thanks. How would I go about dynamically allocating the strings in the dynamically allocated structure? Does this make sense?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Do you know how to dynamically allocate memory for a pointer? If not, read this: Pointers in C - Tutorial - Cprogramming.com
    Once you understand that, give it a try and post your updated code.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. typedef struct: naming structures using pointers
    By BinaryBlock in forum C Programming
    Replies: 9
    Last Post: 02-05-2012, 07:11 PM
  2. Replies: 7
    Last Post: 02-25-2010, 10:38 AM
  3. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 09:35 PM
  4. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  5. Dynamically allocate size of array for strings
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 05-04-2002, 05:06 PM

Tags for this Thread


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