simple structure question

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

simple structure question

 
0
  #1
Aug 22nd, 2006
  1. #include <stdio.h>
  2.  
  3. struct FUN {
  4. char x;
  5. char *y;
  6. int z[20];
  7. };
  8.  
  9. int main(void) {
  10.  
  11. struct FUN fn1;
  12. struct FUN fn2;
  13. struct FUN fn3[10];
  14. struct FUN fn4[50];
  15.  
  16. return 0;
  17. }


is an assignment like fn4[23] = fn3[5] invalid?
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,131
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: simple structure question

 
0
  #2
Aug 22nd, 2006
Originally Posted by degamer106 View Post
  1. #include <stdio.h>
  2.  
  3. struct FUN {
  4. char x;
  5. char *y;
  6. int z[20];
  7. };
  8.  
  9. int main(void) {
  10.  
  11. struct FUN fn1;
  12. struct FUN fn2;
  13. struct FUN fn3[10];
  14. struct FUN fn4[50];
  15.  
  16. return 0;
  17. }

is an assignment like fn4[23] = fn3[5] invalid?
What happened when you tried it?

Set up a function to move the parts of FUN from one value to another.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: simple structure question

 
1
  #3
Aug 22nd, 2006
> is an assignment like fn4[23] = fn3[5] invalid?
Well it's fine as far as the syntax is concerned.

However, the pointer inside the structure presents big problems.
  1. fn1.y = malloc( 10 * sizeof *fn1.y );
  2. fn2 = fn1;
  3. free( fn1.y );
  4. // fn2.y is now a dangling pointer.

Structure assignments in C know nothing about the internals of the struct, it's just a handy wrapper around
memmove( &fn2, &fn1, sizeof fn2 );

In C++, we would use a proper copy constructor to replicate what the pointer pointed to rather than just making a copy of the pointer.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 67
Reputation: himanjim is an unknown quantity at this point 
Solved Threads: 1
himanjim himanjim is offline Offline
Junior Poster in Training

Re: simple structure question

 
0
  #4
Aug 26th, 2006
structures can be copied directly into each other but copying array as such leads to error. In such cases copy the structures using pointers to the strutures.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1085 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC