As per suggestions I have modified the code, but how can I initialize single element in the structure ??
#include<stdio.h>
typedef struct student
{
int roll_id[10];
int name_id[10];
} student;
int main()
{
student p = { {0} }; // if i want to initialize single element ''FIX HERE, PLs''
student *pptr=&p;
pptr->roll_id[9]={0}; // here is the error pointed
printf (" %d\n", pptr->roll_id[7]);
return 0;
}
René Höhle
27.4k22 gold badges78 silver badges91 bronze badges
3 Answers 3
{0} is valid only as an aggregate (array or struct) initializer.
int roll_id[10] = {0}; /* OK */
roll_id[0] = 5; /* OK */
int roll_id[10] = 5; /* error */
roll_id[0] = {0}; /* error */
What you seem to want is to initialize p of type struct student. That is done with a nested initializer.
student p = { {0} }; /* initialize the array inside the struct */
answered Jan 7, 2013 at 9:49
Potatoswatter
139k29 gold badges280 silver badges435 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
Swetha.P
if there are two elements in the structure (for e.g roll_id[10]and name_id[10] ), If i want to initialize only one element then "student p = { {0} };" this doesnt work, right?? how to initialize single element in the structure.
Swetha.P
@WhozCraig : Yes thats correct, I would like to know if there is any options to change ??
WhozCraig
@Swetha.P in C99 you can use member-initialization :
student p = { .roll_id={0} } If your C-compiler is pre 99 standard, you're kind of out of luck on only partials.Potatoswatter
@Swetha.P You have to initialize them from first to last (edit: except in C99, as in WhozCraig's comment). But you can always initialize with e.g. an empty string. And you don't have to initialize them all, so in your last example, yes it would still work :)
I can see two errors in your code
#include<stdio.h>
typedef struct student
{
int roll_id[10];
} student;
int main()
{
student p;
student *pptr=&p;
pptr->roll_id[10]={0}; // in this line it should be pptr->roll_id[9]=0;
printf (" %d\n", pptr->roll_id[7]);
return 0;
}
as the length of array is 10 so the index should be 9 and u can use {0} only at the initialization of an array.
answered Jan 7, 2013 at 9:53
Tushar Mishra
1771 silver badge7 bronze badges
2 Comments
Swetha.P
Actually I want to initialize the array in the structure, something like int array_zero[ARRAY_SIZE] = { 0 };
Tushar Mishra
if you want to initialize it in the structure then you can do that... for initialization of array you can do like this.. int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 }; Elements with missing values will be initialized to 0: int myArray[10] = { 1, 2 }; //initialize to 1,2,0,0,0... So this will initialize all elements to 0: int myArray[10] = { 0 }; //all elements 0
use as below for single array element initialization:
pptr->roll_id[x] = 8 ; // here x is the which element you want to initialize.
use as below for entire array initialization:
student p[] = {{10, 20, 30}};//just example for size 3.
student *pptr = p;
for (i = 0 ; i < 3; i++)
printf ("%d\n", pptr->roll_id[i]);
answered Jan 7, 2013 at 12:16
nagaradderKantesh
1,7004 gold badges18 silver badges31 bronze badges
Comments
lang-c