How do I use an array variable inside an array of structures?

ash9209

I'm trying to grasp the concept of arrays of structures and came up with a problem. Hopefully you can help me out.

Okay, so, the problem I'm facing is how to declare and use (i.e. accept & display values) of an array variable within an array of structures?

This sample code may visually help you to understand my problem:

#include<stdio.h>

struct node{

    int roll;
    char name[10];
    int grades[5]; // Accepts 5 grades for each student
};

int main()
{
    struct node student[3];

    /*Accept and display values for structure members here*/

    return 0;
}

I know there's a similar example here.

But I don't understand line 4 in the accepted answer's main() section, where memory is allocated using malloc() :

list[ip].inputs[inp]= (char*)malloc(25);

I'm getting confused between the 25 bytes allocated here and the 10 defined in char* inputs[10];

What exactly is happening here? And how do you solve the problem I mentioned above?

Mihai Maruseac

There's an extra * in the example you quote. The malloc is needed only because of that, in your example inputs is an array of 10 pointers to char while here name is a buffer holding 10 chars. You don't need any malloc in your code.

Your structure looks like this in memory (assuming 4-bytes ints):

enter image description here

Your student array from main looks like:

enter image description here

As you see, fields are laid out one after another. Thus, to read the name of the first student you have to write into student[0].name (using strncpy to ensure that there's no overflow). To change the third letter of the name of the second student you'll use student[1].name[2].

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I use ScalaTest "inside" with an Array?

How do I use an "array" as a Function variable?

How do i use variable in array index?

How do I construct an array of ctype structures?

How do I pass an array of structures to a function?

How do I insert char* to array of structures?

How do I use INDIRECT inside an Excel array formula?

How do I use RecyclerView with an array of TextViews inside a fragment?

How do I Store an Array Variable Inside of Another Array Variable using while loop and for each?

How to use an array inside a GraphQL Query Variable

How do I use Docker environment variable in ENTRYPOINT array?

How do I use the contents of an array as a variable name?

How do I use a variable as an array index in Python?

How do i concatenate a string to an array variable inside the loop and access array elements?

How can I use an if statement inside an array?

How do I initialize an array of pointers to structures in C?

How do I filter an array of structures' properties with a keyword?

How Do I access data in a Matlab Array of Structures in a mex file

How do I print elements of structures contained in an array

How do I add an element to an array of structures in C?

How do I place this object inside an array

How do I remove an array inside an array in mongoDB?

How do i filter an array inside of a array of objects?

How do I access an 'Array' inside an Array using Angular?

How do I get Array inside array in PHP using Codeigniter?

How do I select a random array, and then a random index inside THAT array?

How do i filter an array inside of a array of objects using filter?

How do I remove the last item of an array inside of an array

How do I save/update/pull information for an array inside of an array?