1

In the following program I am having problems with structure initialization. After initialization I need to cout each strcture element using Pointer.

#include <iostream>

using namespace std;

struct student{
int rollno;
float marks;
char name[45];
}*ptr;

int main(){

    //Getting initialization error
student s1[2]={1,50.23,"abc",2,65.54,"def"};

    for(int i=0;i<2;i++){

            //Need to cout using pointers
    cout<<s1[i].rollno<<s1[i].marks<<s1[i].name;
}
return 0;
}
9
  • And the question is... ? Commented Jan 19, 2012 at 0:36
  • @EdS. Question is regarding how to Initialize Array of Structure and Accessing each structure element via Pointer. Commented Jan 19, 2012 at 0:39
  • Works here: ideone.com/1ZFgh Commented Jan 19, 2012 at 0:39
  • BTW deleted the C tag, because C is quite different in handling this code, particularly, you'd need struct student instead of student, etc. Commented Jan 19, 2012 at 0:41
  • @jpalecek Good to know that it works but why it isn't it working in eclipse IDE? Commented Jan 19, 2012 at 0:41

1 Answer 1

2

You should wrap every member of the array in {}:

student s1[2]={{1,50.23,"abc"},{2,65.54,"def"}};
Sign up to request clarification or add additional context in comments.

2 Comments

This is unneeded, much more a style issue.
weird. It's work fine for me. (In fact, your original code is work too, but the compiler prints warning for the missing {})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.