944,131 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1443
  • C++ RSS
Aug 22nd, 2006
0

simple structure question

Expand Post »
C++ Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006
Aug 22nd, 2006
0

Re: simple structure question

Click to Expand / Collapse  Quote originally posted by degamer106 ...
C++ Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is offline Offline
7,749 posts
since May 2006
Aug 22nd, 2006
1

Re: simple structure question

> 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.
C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Aug 26th, 2006
0

Re: simple structure question

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.
Reputation Points: 14
Solved Threads: 1
Junior Poster in Training
himanjim is offline Offline
67 posts
since Jul 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Array and Loop for Paycheck
Next Thread in C++ Forum Timeline: Errors While Compiling





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC