Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Defining Structure Array at Run-Time in C Language??

Status
Not open for further replies.

xpress_embedo

Advanced Member level 4
Advanced Member level 4
Joined
Jul 5, 2011
Messages
1,154
Helped
161
Reputation
396
Reaction score
189
Trophy points
1,353
Location
India
embeddedlaboratory.blogspot.in
Activity points
10,591
Hello!!! Everyone i am using Ubuntu 14.04 and Code::Blocks GCC to do some programming in C language.

I want to write a program, in which i have to declare a structure array, the size is not fixed and i have to define it at run-time.

Here is my Code which is working Properly but i am not able to understand few points, hope someone will clarify my doubts.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdio.h>
#include <stdlib.h>
 
typedef struct
{
    unsigned char Name[10];
    unsigned int Fees;
}DATABASE;
 
int main()
{
    DATABASE *DataBase;
    unsigned int NumberOfStudents,i;
    printf("Enter the Number of Students?\n");
    scanf("%d",&NumberOfStudents);
 
    DataBase = (DATABASE *)malloc(sizeof(DATABASE)*NumberOfStudents);
    if(DataBase == NULL)
    {
        printf("\nUnsufficient Memory for Operation\n");
        exit(1);
    }
    printf("\n");
    for(i=0;i<NumberOfStudents;i++)
    {
        printf("\nEnter the Name of Student %d ?\n", i+1);
        scanf("%s",(DataBase+i)->Name);
        printf("\nEnter the Fees of Student %d ?\n", i+1);
        scanf("%u",&(DataBase+i)->Fees);
    }
 
    printf("\n");
    for(i=0;i<NumberOfStudents;i++)
    {
        printf("\nStudent %d Name is %s \n", i+1,(DataBase+i)->Name);
        printf("\nStudent %d Fees is %u \n", i+1,(DataBase+i)->Fees);
    }
    free(DataBase);
    return 0;
}



Code_Working.png


First of all tell me is it right method.
If yes then i want to know one more question

printf("\nEnter the Fees of Student %d ?\n", i+1);
scanf("%u",&(DataBase+i)->Fees);


I dont know why i write '&' in scanf statement, i know that in scanf one have to pass the address of the variable.

Does (DataBase+i)->Fees) doesn't points to array, and after adding & it will point to address.
Then why not with the Name String, because it is declared as an array and first byte of array points to address.

Please tell me if i am right or not.
 

in C function parameters are passed by value (a copy of the parameter is made and the function does have access to the original parameter). If a function needs to pass data to the original parameter one uses the & (address of operator) to pass the address of the parameter to the function which can then access and alter the memory allocated to the parameter. e.g.
Code:
    printf("Enter the Number of Students?\n");
    scanf("%d",&NumberOfStudents);
the address of NumberOfStudents is passed to scanf() which can return the value read into the memory allocated to it

in the case of an array as a function parameter, e.g.
Code:
        printf("\nEnter the Name of Student %d ?\n", i+1);
        scanf("%s",(DataBase+i)->Name);
there is no need to use & to take the address of (DataBase+i)->Name because it is already an address, i.e. the array name (DataBase+i)->Name is a constant pointer to the first element.
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top