How is this done? I don't quite understand.
How is this done? I don't quite understand.
That would depend on what you are actually trying to achieve.
But here's an example:
The advantage over storing a copy of the struct inside gadget is that we can set it to a or b using a simple pointer move. Copying the whole struct itself would involve a lot more data to be copied (if the struct is large).Code:struct widget
{
... some data - fairly large.
};
struct gadget
{
struct widget *widgetPtr;
...
};
widget a;
widget b;
gadget g;
...
somefunc(int flag)
{
if (flag)
g.widgetPtr = &a;
else
g.widgetPtr = &b;
...
}
I used global variables above to make the code small - it is not a recommended method, but it makes the example short.
--
Mats
I have these structs:
and this function:Code:struct Listnode
{
char lastname[30];
char firstname[30];
int age;
};
struct Listtype
{
Listnode* person[30];
int size;
};
Its a function to insert records into the struct... And if you insert somewhere where there already is a record I just want the function to make sure that you move them so that nothing is erased... But I get an error that the "left operand must be an L-value" at these 2 lines:Code:void doInsert (Listtype &List)
{
char first[30]; //entry firstname
char last[30]; //entry lastname
int years; //entry age
int number; //slot number to place record at
int i;
system("cls");
cout<<"Enter a record in this format: LASTNAME <SPACE> FIRSTNAME <SPACE> AGE"<<endl;
cout<<"\n";
cin>>first>>last>>years;
cout<<"Insert this record at what position: ";
cin>>number;
for (i=List.size+1; i>number; i--) //moving all records down 1 position to make room
{
List.person[i]=List.person[List.size];
}
List.person[number]->firstname=first;
List.person[number]->lastname=last;
List.person[number]->age=years;
List.size=i;
}
I'm a little confused as to whether what I'm attempting here is a copy of the structure data or copying the pointer. And why I'm getting that specific error.Quote:
List.person[number]->firstname=first;
List.person[number]->lastname=last;
What you're trying to do is assign to an array. Perhaps you mean to strcpy instead.
right now I have:
for some reason this is causing an output where the new record to be inserted appears at List.person[number] and at List.person[number+1]Code:void doInsert (Listtype &List)
{
char first[30]; //entry firstname
char last[30]; //entry lastname
int years; //entry age
int number; //slot number to place record at
int i;
system("cls");
cout<<"Enter a record in this format: LASTNAME <SPACE> FIRSTNAME <SPACE> AGE"<<endl;
cout<<"\n";
cin>>first>>last>>years;
cout<<"Insert this record at what position: ";
cin>>number;
for (i=List.size; i>number-1; --i) //moving all records down 1 position to make room
{
List.person[i]=List.person[i-1];
}
strcpy(List.person[number-1]->firstname, first); //placing record in number slot
strcpy(List.person[number-1]->lastname, last);
List.person[number-1]->age=years;
List.size++;
}
I don't know why either... I tried outputting the loop verbosely in a separate program which outputs:
4 copies to 5
3 copies to 4
2 copies to 3
and thats the end of the loop. Which is correct if number = 3. In the actual program the record is copied twice though at number 3 and at number 4 for some reason.
[edit] - I figured it out I think, but I don't know why it happens or how I can "fix it"... The problem arises I'm thinking because when I assign a value at List.person[number-1]... that pointer points to 2 positions in the list after the function is done... I'm not sure why though..., shouldn't there just be garbage there if I moved everything else?
[edit2] - Got it... The problem was what I thought it was... I just made a new Listnode object there so it wasn't being pointed too again.