Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

structure initialization

The output of the code has to be zero, but I am having an error, please guide. I can initialize the structure with its type variable but how can I do with the pointer to initialize the whole structure?

#include<stdio.h>
typedef struct student
{
 int roll_id[10];
 int name_id[10];
 int postn;
} student;
int main()
{
 student p; /// here i can do student p={ {0} };
 student *pptr=&p;
 pptr= { {0} }; /// but can i initialize in this way ?? //error: expected 
 expression before ‘{’ token
 pptr->roll_id[9]=1;
 printf (" %d\n", pptr->postn); 
 return 0;
}

Answer*

Draft saved
Draft discarded
Cancel
5
  • 1
    I believe you're trying to assign a string literal as an element of an int array. Commented Jan 8, 2013 at 11:13
  • @unwind: pptr->roll_id[0-9]=0; // can i do this without loop Commented Jan 8, 2013 at 11:16
  • 1
    This is wrong! There are no character strings in the question, and braced-enclosed initializer lists are not for assignment. Commented Jan 8, 2013 at 11:37
  • @MikeKwan Thanks, must have mis-read the OP's code in some way. Commented Jan 9, 2013 at 13:22
  • @acraig5075 Fixed the string mis-match, must have been confused yesterday. But yes, compound literals ("brace-enclosed initializer lists") are fine from C99, as I say. Commented Jan 9, 2013 at 13:23

lang-c

AltStyle によって変換されたページ (->オリジナル) /