Results 1 to 6 of 6
Code:
void swap(int *px, int *py)
{
int t;
t = *px;
*px = *py;
*py = t;
}
swap(&a, &b);
The code above makes perfect sense to me. I ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 07-05-2011 #1
Pointers in C
The code above makes perfect sense to me. I understand what it does, how it does it and why it uses pointers, but I don't understand ifCode:void swap(int *px, int *py) { int t; t = *px; *px = *py; *py = t; } swap(&a, &b);is better thanCode:*(primes + 7)
and if it is, then why? Except for the fact it looks way coolerCode:primes[7]
If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)
Snakey Wakey!
The Fifth Continent reborn
- 07-05-2011 #2
Additionally, which is the better practice in c?
orCode:int say(const int object, int room, const char n[]) { char s[MAXINPUT]; int i = 0, r = -1; printf("OK: %s", n); if (carrying[MAGIC_SPELLS] && XYZZY == object && COLD_CHAMBER != room) { printf("*** MAGIC occurs ***\n"); // should move to a random room but that hasn't been covered r = DARK_CORNER; } else if (carrying[2] && XYZZY == object && COLD_CHAMBER == room) { flags[BARRIER_DOWN] = TRUE; } printf("\nPress return to continue"); i = getLine(s, MAXINPUT); return r; } currentRoom = say(object, currentRoom, noun);
Code:void say(const int object, int *room, const char n[]) { char s[MAXINPUT]; int i = 0; printf("OK: %s", n); if (carrying[MAGIC_SPELLS] && XYZZY == object && COLD_CHAMBER != *room) { printf("*** MAGIC occurs ***\n"); // should move to a random room but that hasn't been covered *room = DARK_CORNER; } else if (carrying[2] && XYZZY == object && COLD_CHAMBER == *room) { flags[BARRIER_DOWN] = TRUE; } printf("\nPress return to continue"); i = getLine(s, MAXINPUT); } say(object, ¤tRoom, noun);If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)
Snakey Wakey!
The Fifth Continent reborn
- 07-05-2011 #3Just Joined!
- Join Date
- Jul 2011
- Posts
- 6
na both are interpreted the same way...
*(prime + 7) and prime[7]...
btw even 7[prime] will give you the same result...
- 07-05-2011 #4Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 10,227
Use pointer when it is more sensible, bearing in mind that you REALLY need to verify that the pointer is not null before you dereference it. So, if you can get away without pointers, it makes for more secure software. That said, if you pass in an array (same as a pointer), you should also pass in an array size argument so that if the array has 7 valid elements, you can reject an index >= 7, or less than 0.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 07-05-2011 #5
You don't often see *(prime + 7) used in lieu of prime[7]. However, if prime is an array, you might often see (prime + 7). This would give you the address of the eighth element of the prime array.
For the second one, I think that there are occasions when both are appropriate. A common case for modifying the parameters of a function (sometimes called output parameters) is when the function returns more than one value: you can have the user pass in locations to store the return values. See, for instance, the standard pipe() function.
In this particular case, I personally prefer the first piece of code. This is because you are easily able to return error values. Suppose that something went wrong: in the first piece of code, you would just return some error value, and the caller could handle it. In the second piece of code, to return an error, you must overwrite some data that the caller passed in, which means that they might have weird data (they might think the current room is "Player Doesn't Exist" or something).
I generally prefer to use return values for as much as possible, unless there's some reason that I need a memory address passed in. It allows the caller of a function to use the function in more ways than I might have expected.
- 07-06-2011 #6
Thanks to everyone for this, it has helped clarify things a bit.
In the case of the second question, it's what I would prefer but in the book (and now books) I am reading, they get you to rewrite nearly every preceding exercise using pointers which is probably for the practice but did give them artificial weight in my mind.
Thanks again.If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)
Snakey Wakey!
The Fifth Continent reborn


1Likes
Reply With Quote
