![]() |
|
#1
|
|||
|
|||
Swapping structuresI am confused about swapping defined structures , not so much swapping pointers to structures.
I'll explain within the following: CPP / C++ / C Code:
Code:
1) Is method 1 a safe method or is it a quirk, as I said, I was suprised at the results since it's a whole structure instead of a single data like: int a = 4, b; b = a; Like you can't assign an array like that (which to me is similar to a struct): char s[]="hello', s2[32]; s2 = s; ...so why can I assign a struct, it doesn't seem right, unless it's an address, but so is an array identifier... see I' confused... 2) Is it possible to transfer object name reference addresses like: pl_1 = pl_2; ...using a pointer instead of defining a temp structure as the temporary storage? (which would keep the code smaller) I hope I made some kind of sense , the more I learn the stupider I feel..; ) , thanks Last edited by Howard_L : 18-May-2009 at 13:16.
|
|||
|
#2
|
|||
|
|||
Re: swapping structuresQuote:
In order to exchange the values of two "somethings," with assignment statements you can do something like: Code:
This, of course, does not work with arrays (Them's the rules of the language, and it is because the name of an array is actually a constant pointer whose value is the address of the first element of the array), but it does work with structs. It is part of the language. Really. I hate to repeat myself, but unlike arrays, you can assign the entire contents of one struct to another with a simple assignment statement. See Footnote. Your "method 1" is perfectly valid and works in C and, by default, in C++. Your so-called "method 2" can't work, since you still only have two structs. You need that third "something" to be a "parking place" for the value of the first one before you assign the other's value to it. Your code: CPP / C++ / C Code:
Is equivalent to CPP / C++ / C Code:
Sorry. Regards, Dave Footnote. People always seem to want to do stuff like the following with arrays: CPP / C++ / C Code:
However, what if your arrays were members of a struct? You could have a struct whose only member is an array (or it could have other members as well). CPP / C++ / C Code:
Code:
Hmmm... Bottom line: Maybe "to you" an array is similar to a struct, but to a C (or C++) compiler they are different. How about some post-bottom-line pedantry? Is that allowed? If it is not allowed, then stop reading. If anyone is still interested (and awake): OK, here goes: (Got those No-Doz tablets ready? Or some of those great new Butterfinger candy bars that are laced with caffeine?) Actually, all variable names are symbolic identifiers, and the variable name is associated with a memory address. CPP / C++ / C Code:
The compiler lets you use the identifier "x" to refer to the contents of that memory location. No de-referencing is required (or allowed). That assignment statement tells the compiler to generate code that stores the integer "10" into the memory location associated with the identifier "x". CPP / C++ / C Code:
Now, the variable y is a pointer whose value is the address of the variable x. If you do something with *y, it's doing it with x. The value of y can be changed so that it is equal to something other than the address of the variable x. CPP / C++ / C Code:
Now the variable z is a pointer whose value is the address of the first element in the array of 10 ints. In this case z is a constant. In C and C++, it is absolutely forbidden, now and forevermore, to assign some other value to that instance of the variable z. Period. Full stop. CPP / C++ / C Code:
Now the identifier xxx is associated with the address in memory that holds the values of the elements of that struct. You can assign the value of another struct of the same type with an assignment statement. So, as far as the language is concerned, the statement CPP / C++ / C Code:
Is more similar to CPP / C++ / C Code:
CPP / C++ / C Code:
Maybe a struct is similar to an array in some respects, but the statements that declare them and use them have quite different semantics. And that really is the bottom line (for now). Last edited by davekw7x : 18-May-2009 at 14:57.
|
|
#3
|
||||
|
||||
Re: swapping structuresQuote:
![]() __________________
Use the force...read the source!! WYCIWYG -- what you code is what you get! |
|
#4
|
|||
|
|||
Re: Swapping structuresQuote:
I don't remember knowing that you could assign a struct. Maybe I did and it got pushed way back on the brain stack because usually I am manipulating values of a struct passed to a function for which we DO need to use address referencing. So this is all an awakening for me ...and a pleasant one at that. I find I can do this: CPP / C++ / C Code:
CPP / C++ / C Code:
Code:
Howard++; |
Recent GIDBlog
Vista ?Widgets? on Windows XP by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Help needed with structures | moody | C Programming Language | 4 | 08-Mar-2008 09:52 |
| Strings, Pointers, and Printing Alphabetically | NeRdHeRd | C Programming Language | 6 | 25-Dec-2007 12:37 |
| How Do I Create a Function that returns a usable array of pointers to structures? | UncleRic | C Programming Language | 2 | 03-Sep-2007 11:17 |
| Could anyone suggest an easy to read book on Data Structures | aijazbaig1 | C Programming Language | 1 | 06-Oct-2006 11:31 |
| DMA for Structures | wu_weidong | C Programming Language | 1 | 05-Apr-2005 04:18 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The