0

I am having trouble figuring out how to dynamically allocate memory to a array structure. I need to use an array of my defined structure so that I can iterate over it to print the info. However, I am also asked to dynamically allocate memory to the array.

The problem is that when I use malloc() in the code (you will see it is commented out) it throws the pointer off from the array index. Is there any way to accomplish dynamically allocating memory while pointing to the array indicies to store the data?

DISCLAIMER: I am very new to C, if my coding hurts your eyes, I sincerely apologize in advance. Just looking to understand the program rather than quickly finish it.

PROGRAM INSTRUCTIONS:

Write a C program that uses 'struct' to define a structure containing the following Student:

Information:

  1. Initial [character]
  2. Age [integer]
  3. Id [integer]
  4. Grade [character]

Your program should print the above information for 5 students.

Hint: Use array of structures and iterate over the array to print the information.

Create a structure with:

  1. Variable data of type int
  2. A character pointer called tag. This pointer should point to the address of a string.

Check if memory is available and then dynamically allocate required memory to your structure.

Assign values to your structure variable and print them to console.

Code so far:

# include <stdio.h>
# include <string.h>
# include <stdlib.h>

int main (void)
{
    struct student  
    {
        char initial [21];
        int age;
        float id;
        char grade [3];
    } list[5];

    struct student * tag = list;

    //LINE BELOW IS WHAT I WAS TRYING TO DO; BUT THROWS POINTER OFF OF STRUCT ARRAY
    //tag = ( struct student * ) malloc ( sizeof ( struct student ));
    strcpy(tag->initial, "KJ");
    tag->age = 21;
    tag->id = 1.0;
    strcpy (tag->grade, "A");
    tag++;

    strcpy(tag->initial, "MJ");
    tag->age = 55;
    tag->id = 1.1;
    strcpy (tag->grade, "B");
    tag++;

    strcpy(tag->initial, "CJ");
    tag->age = 67;
    tag->id = 1.2;
    strcpy (tag->grade, "C");
    tag++;

    strcpy(tag->initial, "SJ");
    tag->age = 24;
    tag->id = 1.3;
    strcpy (tag->grade, "D");
    tag++;

    strcpy(tag->initial, "DJ");
    tag->age = 27;
    tag->id = 1.4;
    strcpy (tag->grade, "F");
    tag++;

    int n;

    for ( n = 0; n < 5; n++ ) {
            printf ( "%s is %d, id is %f, grade is %s\n", 
              list [ n ].initial, list [ n ].age, list [ n ].id, list [ n ].grade);
            }

    return 0;

}
3
  • 2
    You are allocating a single student (hint: what does sizeof(student) mean?), but you pretend you have allocated a whole array of them. That can't work. Commented Oct 6, 2014 at 6:54
  • you are mentioned tag is char pointer in question.But in program it is structure pointer... Give proper info.. Commented Oct 6, 2014 at 7:15
  • My apologies, I am very new to C. You are correct, tag should be char pointer. Do I declare tag within student to make it char pointer? Thank you as well for sizeof(student) advice - your feedback is sincerely appreciate. Commented Oct 6, 2014 at 7:45

2 Answers 2

1

If you are using pointer to read and print the struct values then no need of array.If you are trying to use array then this is the way to do it.

 #include <stdio.h>
    # include <string.h>
    # include <stdlib.h>

    int main (void)
    {
       struct student  
       {   
          char initial [21];
          int age;
          float id; 
          char grade [3];
       } list[5];

       struct student *tag = ( struct student * ) malloc ( sizeof ( struct student ) * 5);

       int i;
       for(i=0;i<5;i++)
       {   
          printf("Enter the student initial\n");
          scanf("%s",list[i].initial);
          printf("Enter the student age\n");
          scanf("%d",&list[i].age);
          printf("Enter the student id\n");
          scanf("%f",&list[i].id);
          printf("Enter the grade\n");
          scanf("%s",list[i].grade);
          tag++;
       }   

       int n;

       for ( n = 0; n < 5; n++ ) { 
          printf ( "%s is %d, id is %f, grade is %s \n", 
                list [ n ].initial, list [ n ].age, list [ n ].id, list [ n ].grade);
       }   

       return 0;

    }
Sign up to request clarification or add additional context in comments.

Comments

0

Try this.... Here i am declaring one structure pointer tag. I am allocating memory for 5 student structure . After assigning values ,now tag is pointing to last value.so i decremented it 5 times.This program is using only one pointer. If you want you can try using array of pointer.

I mentioned changes ,in program,as //changes

# include <stdio.h>
# include <string.h>
# include <stdlib.h>

int main (void)
{
 struct student
  {
    char initial [21];
    int age;
    float id;
    char grade [3];
  } list[5];

struct student * tag;


    tag = ( struct student * ) malloc (5* sizeof (struct student));//changes

strcpy(tag->initial, "KJ");
tag->age = 21;
tag->id = 1.0;
strcpy (tag->grade, "A");
tag++;

strcpy(tag->initial, "MJ");
tag->age = 55;
tag->id = 1.1;
strcpy (tag->grade, "B");
tag++;

strcpy(tag->initial, "CJ");
tag->age = 67;
tag->id = 1.2;
strcpy (tag->grade, "C");
tag++;

strcpy(tag->initial, "SJ");
tag->age = 24;
tag->id = 1.3;
strcpy (tag->grade, "D");
tag++;

strcpy(tag->initial, "DJ");
tag->age = 27;
tag->id = 1.4;
strcpy (tag->grade, "F");
tag++;

int n;
    tag=tag-5;//changes

for ( n = 0; n < 5; n++ ) {
        printf ( "%s is %d, id is %f, grade is %s\n",
          tag->initial, tag->age, tag->id, tag->grade);
            tag++;//changes
        }

return 0;
}

Using array of pinter...(Instead of using separate assignment, you can use loop for assigning values to every student)

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    int main (void)
    {
            struct student
            {
             char initial [21];
             int age;
             float id;
             char grade [3];
            } list[5];

        struct student *tag[5];
    int i;

    for(i=0;i<5;i++)
    tag[i]= ( struct student * ) malloc (sizeof (struct student));

        strcpy(tag[0]->initial, "KJ");
        tag[0]->age = 21;
        tag[0]->id = 1.0;
        strcpy (tag[0]->grade, "A");

        strcpy(tag[1]->initial, "MJ");
        tag[1]->age = 55;
        tag[1]->id = 1.1;
        strcpy (tag[1]->grade, "B");

        strcpy(tag[2]->initial, "CJ");
        tag[2]->age = 67;
        tag[2]->id = 1.2;
        strcpy (tag[2]->grade, "C");

        strcpy(tag[3]->initial, "SJ");
        tag[3]->age = 24;
        tag[3]->id = 1.3;
        strcpy (tag[3]->grade, "D");

        strcpy(tag[4]->initial, "DJ");
        tag[4]->age = 27;
        tag[4]->id = 1.4;
        strcpy (tag[4]->grade, "F");

        for ( i = 0; i < 5; i++ )
        {
                printf ( "%s is %d, id is %f, grade is %s\n",
             tag[i]->initial, tag[i]->age, tag[i]->id, tag[i]->grade);

        }

        return 0;

     }

1 Comment

Thank you very much for the malloc size of 5 and decrement advice! Could you give an example of what you mean by array of pointers? Would it just be declaring multiple pointers like tag or pointer of a different type? Thank you again.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.