Can anyone help me on this. If you have got an array of structures pointed to by a pointer, how do you access the subscript and then the subsequent members of that structure?
Thanks
Can anyone help me on this. If you have got an array of structures pointed to by a pointer, how do you access the subscript and then the subsequent members of that structure?
Thanks
Be a leader and not a follower.
Something like that?Code:struct house { int people; int toilets; char *address; } house myStreet = new house[5]; myStreet[2]->people = 1; myStreet[4]->address = new char[strlen((const char *)"4 Subdene") + 1]; strcpy(myStreet[4]->address, "4 Subdene"); myStreet[4]->address[0] = '5';
Last edited by Dual-Catfish; 06-30-2002 at 04:29 PM.
Lets say that i defined a structure as follows :
"ptrStudent[i]->name" is the same as sayingCode:#include <stdio.h> #include <string.h> struct Person { char* name; char* surname; }; ... int main() { struct Person student[20]; struct Person*ptrStudent; int i; ptrStudent = student; //Initialize the pointer for(i=0;i<20;i++) { ptrStudent[i]->name = (char*)malloc(20); ptrStudent[i]->surname = (char*)malloc(20); } return 0; }
"(*ptrStudent[i]).name" or it should be.
Thanks, for your replies, thats helped alot. Dual just one quick question. In this section of code when you are assigning the length of the variable,
why can't you do:-Code:myStreet[4]->address = new char[strlen((const char *)"4 Subdene")];
don't fully understand what the const char is there for.Code:myStreet[4]->address = new char[strlen("4 Subdene")];
Thanks.
Be a leader and not a follower.
Since a "string" is already a const char *, it seems entirely redundant to say it
Besides, it's needs to be
myStreet[4]->address = new char[ strlen("4 Subdene") + 1 ];
Gotta remember to add space for that all important \0 at the end of the string
this thing below is wrong ...
it should be :Code:struct Person*ptrStudent; for(i=0;i<20;i++) { ptrStudent[i]->name = (char*)malloc(20); ptrStudent[i]->surname = (char*)malloc(20); }
the subscript operatorCode:struct Person*ptrStudent; for(i=0;i<20;i++) { ptrStudent[i].name = (char*)malloc(20); ptrStudent[i].surname = (char*)malloc(20); }
here
ptrStudent[i]
access the value pointed by ptrStudent
and this value is NOT a pointer !
so using '->' (access fields by pointer) is wrong.
it should be '.' (access fields by value/object)
be carefull what you write ...
I have been declaring the pointer to the structure a different way.
is this wrong?Code:struct House { //whatever }; House* Street; Street = new House[NUM_HOUSES];
Be a leader and not a follower.
Nope.
Well everytime I try to access a member i get the following error:
[C++ Error] New.cpp(48): E2288 Pointer to structure required on left side of -> or ->*
Code:#include <iostream.h> const int MAX_ADDRESS=50; struct House { int NumWindows; char Address[MAX_ADDRESS]; }; int main(void) { const int NUM_HOUSES=5; House *Street; Street = new House[NUM_HOUSES]; strcpy(Street[1]->Address, "22 Holgate rd\0"); cout << Street[1]->Address; getchar(); return 0; }
Be a leader and not a follower.
Can someome have a quick look at the problem, I can't go any further until this is resolved.
here we go.....hope it ll work as u would like.........
--------------------------------------------------------------------------------
#include <iostream.h>
#include <conio.h>
const int MAX_ADDRESS=50;
struct House
{
int NumWindows;
char Address[MAX_ADDRESS];
};
int main(void)
{
const int NUM_HOUSES=5;
House* Street= new House[NUM_HOUSES];
strcpy(Street[1].Address, "22 Holgate rd\0");
cout << Street[1].Address;
getche();
return 0;
}
Programming is a high logical enjoyable art for both programer and user !!
Thanks, for the reply. That got the problem sorted. How come I did not have to use the -> operator, as I delcared a pointer to an object?
Be a leader and not a follower.
thats because yr using (ARRAY) of the strucure, so u ve to use the (.) while u ve to use ( ->) if ur using a pointer to single structure.