Question:
how can i create an array of structure with pointer in c++?
j t
2008-08-22 12:26:02 UTC
void main()
{
struct student
{
char name[20];
int marks;
};
student *ptr;
ptr=new student[5];

now how can i enter data into these five objects?
Seven answers:
Gopinath M
2008-08-23 04:34:31 UTC
include

#include



struct student

{

int rollno;

char name[30];

};



void main()

{

int i,n;

student *ptr;



clrscr();

cout<<"\n Enter the no. of students: ";

cin>>n;



ptr=new student[n];



for(i=0;i
{

cout<<"\n\n Enter student detail "<
<<"\n\n Roll no: ";

cin>>(ptr+i)->rollno;

cout<<" Name : ";

cin>>(ptr+i)->name;

}



clrscr();

cout<<"\n Displaying details...\n";

for(i=0;i
cout<<"\n "<<(ptr+i)->rollno<<"\t"<<(ptr+i)->name;

getch();

}
Jon
2008-08-22 12:34:07 UTC
The previous answer is close. It would actually be:



ptr[0].name = ...

ptr[0].marks = ...

ptr[1].name = ...

ptr[1].marks = ...



You wouldn't do "student[0]" because student is the name of the type, not the name of the pointer.



Update: D'oh! The Last Palladin is absolutely correct.
VarmintHunter07
2008-08-22 14:21:02 UTC
If your program is in C++ then the name attribute should be an std::string. Otherwise, if it must be an array of chars, then you should use strncpy() not strcpy() as The Last Paladin has suggested, to avoid bounds overflow.



Also, for posterity, add a constructor to the student struct.



struct student

{

public:

student( )

: marks( 0 )

{

memset( name, 0L, sizeof( name ) );

}



char name[20];

int marks;

};
The Last Paladin
2008-08-22 12:42:18 UTC
both first and second answers have problems...



it would be



ptr[0].marks=100;

strcpy(ptr[0].name,"john");



you just cant do ptr[0].name= because it's an array.
pankaj shukla
2008-08-22 12:52:39 UTC
ptr[0]->name = "a";

ptr[0]->marks = 1;



ptr[1]->name = "ab";

ptr[1]->marks = 11;



ptr[2]->name = "abc";

ptr[2]->marks = 111;



ptr[3]->name = "abcd";

ptr[3]->marks = 1111;



ptr[4]->name = "abcde";

ptr[4]->marks = 11111;
?
2016-12-03 18:58:05 UTC
First, pointer1 is in simple terms going to alter into fake if library_create_create_struct() returns null. without seeing the implementation of library_create_create_struct(), i won't be able of be effective that it relatively is working properly. 2d, you look placing those different issues equivalent to pointer1, then placing pointer1 to a pair different fee, back and back. What precisely is the purpose of that? If library_create_create_struct() allocates memory (as its call shows), and you have not the different shield in place to stay away from a memory leak, then of direction a memory leak is going to ensue. it relatively is recommended to re-examine this technique good judgment you're applying right here. because it relatively is i'm not completely effective what you are trying to do.
oopjosh
2008-08-22 12:30:40 UTC
student[0].name = ...

student[0].marks = ...

student[1].name = ...

student[1].marks = ...



etc.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...