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