1

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;
}
1
  • You're assigning { {0} } to a pointer. Commented Jan 8, 2013 at 10:56

4 Answers 4

1

As stated by chris on a comment, you are assigning "something not an integer" to a pointer. You should assign { {0} } instead to the content of that pointer (*pointer):

*pptr= { {0} }; //bad!

Anyway this is not the correct way to populate that structure and you should use:

*pptr= (student){ {0}, {0}, 0 };

as an example

edited to solve the error pointed by unwind

Sign up to request clarification or add additional context in comments.

1 Comment

I think it's valif IF he can do it with the variable. "student p; /// here i can do student p={ {0} };", just wanted to explain the mechanics.
1

If all you want is a 0-initialized structure, you might as well do:

struct student *pptr = calloc(1, sizeof(struct student));

alternatively, following the code you posted:

struct student p;
struct student *pptr = &p;

memset(pptr, 0, sizeof(struct student));

of course, these approaches will only work if you want a 0-initialized struct. for anything more facy you'll have to write it out, or use some memcpy magic.

5 Comments

Is it correct to use memset without preallocating (malloc) the actual structure? , for the second case ofcourse in your solution.
@Sibi you don't have to use malloc in the second example, because pptr points to the memory location of p, which resides in full size on the stack.
You mean in the stack frame of main() ?
@Sibi I'd guess so, since it's a local variable. do you disagree?
Got you. . And the sizeof(struct student) in memset cleared the doubt I had. Thx a lot.
1

If you're using C99 or later, you can use compound literals. Otherwise you cannot, and must initialize each field separately.

With literals:

*pptr = (student) { 0, 0, 0 };

without:

pptr->roll_id[0] = 0;
pptr->name_id[0] = 0;
pptr->postn = 0;

UPDATE Edited, for some reason I thought that the roll_id and name_id fields were strings, but they're not.

It might be easier to just use memset() for this, to initialize all the values to zero:

memset(pptr, 0, sizeof *pptr);

Notice, as always, that this is a fantastic opportunity to use sizeof with the pointer, since reduces the typing and is easier to read and verify (for reasonable variable names such as we have here).

5 Comments

I believe you're trying to assign a string literal as an element of an int array.
@unwind: pptr->roll_id[0-9]=0; // can i do this without loop
This is wrong! There are no character strings in the question, and braced-enclosed initializer lists are not for assignment.
@MikeKwan Thanks, must have mis-read the OP's code in some way.
@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.
0

use as below

int main()
{

  student p[] = { {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};

  student *pptr = p;

  pptr->roll_id[9] = 1;
  printf (" %d\n", pptr->postn);   
  return 0;
}

if you want to initialize entire array that is a member of structure , you have to do as like above.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.