![]() |
|
#11
|
|||
|
|||
Re: Sorting program using pointer notation NEED HELP!!Quote:
That little wisecrack at the end of my post was not (not) directed at any currently active members, helpers or guests; it's just another of the silly attempts that I make from time to time to try lighten things up a little. Sometimes people think I mean something personal, bit I really don't (really). (But seriously: if you don't know pointers, you don't know C, in my very humble opinion.) I have learned more (about pointers and lots of other things) from following threads on this forum for the last year or so than I have in many, many, many years of previous experience. (How many? Don't ask.) That's the main reason I keep coming back: I always (always) learn something. (I have tried to stay away, but it's more than a little addictive, right?) Regards, Dave |
|||
|
#12
|
||||
|
||||
Re: Sorting program using pointer notation NEED HELP!!Quote:
Quote:
Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#13
|
|||
|
|||
Re: Sorting program using pointer notation NEED HELP!!Just a thought. A pionter is nothing more than a long so:
CPP / C++ / C Code:
Or am I way out of line here ;-) Last edited by LuciWiz : 14-Nov-2005 at 03:04.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|
#14
|
|||
|
|||
Re: Sorting program using pointer notation NEED HELP!!Quote:
There is nothing in the C or C++ language specification that says "a pointer is nothing more than a long". Where did you get the idea that a pointer and a long are the same thing? Pointer arithmetic is defined for the following: 1.Adding an integer data type to a pointer. 2. Subtracting an integer data type from a pointer. 3. Subtracting a pointer from another pointer (Where the pointers point to the same thing). If you actually try your code, the compiler will refuse to compile, and you will see a compile-time error message. (If you don't see an error message, your compiler is not compliant with the C standard language specification. I would be interested in knowing what compiler allows exclusive-or operations on pointers.) Here comes the editorial: [edit] Skip the editorial if you want to, but you might (or might not) be interested in the p.s. at the end[/edit] Why, oh why, would you deliberately create a program that does things not defined in the language standard? (Especially since there are several legal ways to do what you propose --- swap the values of two variables.) The following points are my opinion only, and you are entitled to your opinion (even if it differs from mind): 1. The use of exclusive-or operations to swap the values of two integer data types is looked upon by some as a neat trick that is somehow more efficient than use of a temporary variable (the way that the original program did for its pointers). That was actually a valuable thing in one's little bag of tricks in 1973 with assembly language programs for an Intel 8008 processor that didn't have very many registers, and for which memory accesses were agonizingly slow and took several instructions to set up address registers for memory access. 2. It's a good academic exercise for people studying bit operators and can show some of the interesting properties arithmetic with the exclusive-or operatiion. 3. It is not more efficient in time or code than use of a temporary variable for any modern processor on which people are likely to be running their code. If you find a situation where it is better in any way for your application: use it (It's your decision to make, but I would be interested in seeing the context in which anyone thinks it's appropriate --- for whatever reason.). Otherwise store it away for the day when someone comes to you trying to do this with floating point variables (using casts to make the compiler treat them as ints), and asks why it doesn't work: It only works for integer data types. 4. In C or C++, exclusive-or operations are only defined for integer data types (int, long, char, unsigned int, unsigned long, etc.), so the compiler will not even allow you to try it for pointers, floats, and other non-integer data types. Regards, Dave P.S. Opinions are like belly-buttons: everyone has one. Furthermore, everyone has a right to his/her own opionion, and everyone has a right to defend his/her own opinions --- actively or passively --- against unwanted invasion. I can't think of anything more frightening than a world populated entirely with people whose opinions on everything are exactly like mine. D Last edited by davekw7x : 14-Nov-2005 at 09:02.
|
|
#15
|
|||
|
|||
Re: Sorting program using pointer notation NEED HELP!!That's exactly why I added:
"Or am I way out of line here ;-)" |
|
#16
|
|||
|
|||
Re: Sorting program using pointer notation NEED HELP!!Quote:
Your stated assumption that, "a pointer is nothing more than a long" is incorrect. I think I covered that. The question, "am I way out of line?" is requesting a judgment call, which I am not prepared to make. I gave a few reasons that I wouldn't do it (even if I could). So, I "answered" the question with a question: "Why would you do this (even if you could)?" Some people say that there's no such thing as a dumb question. I question that--- I've seen lots of dumb questions. However, I don't think that your question was a dumb question at all. Lots of people have seen this little trick (swapping values using exclusive-or instead of a temporary variable) and think it's a good, clever, thing to put into a C program. I enjoy the opportunity to show the downside of it. Regards, Dave |
|
#17
|
|||
|
|||
Re: Sorting program using pointer notation NEED HELP!!Althoug in 1973 I was a little young, when I started C/C++ programming the intell 80286 was something for people with a lot of money. I used these tricks a lot. Unfortunately, faith moved me to Java and is now moving me back to C++. I seem to have forgotten some things.
Since I work on software for small devices (mobile phones), I guess this neat trick is still might be usefull, i guess it just won work for pointers. As to dumb questions. Most questions aren't dumb IMHO. The dumbest questions are the ones not asked. |
Recent GIDBlog
Once again, no time for hobbies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Pointer Usage in C++: Beginner to Advanced | varunhome | C++ Forum | 0 | 19-Aug-2005 10:25 |
| Type casts ? | kai85 | C++ Forum | 12 | 23-Jun-2005 13:04 |
| [Tutorial] Pointers in C (Part II) | Stack Overflow | C Programming Language | 0 | 27-Apr-2005 18:36 |
| [Tutorial] Pointers in C (Part I) | Stack Overflow | C Programming Language | 1 | 08-Apr-2005 19:35 |
| fltk-2.0 cvs | Plumb | FLTK Forum | 20 | 13-Nov-2004 08:10 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The