Find the answer to your Linux question:
Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Cabhan
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.
  1. #1
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    3,638

    Pointers in C


    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 understand what it does, how it does it and why it uses pointers, but I don't understand if
    Code:
    *(primes + 7)
    is better than
    Code:
    primes[7]
    and if it is, then why? Except for the fact it looks way cooler
    "I used to be with it, then they changed what it was.
    Now what was it isn't it, and what is it is weird and scary to me.
    It'll happen to you too."

    Grandpa Simpson



    The Fifth Continent

  2. #2
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    3,638
    Additionally, which is the better practice in c?
    Code:
    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);
    or
    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, &currentRoom, noun);
    "I used to be with it, then they changed what it was.
    Now what was it isn't it, and what is it is weird and scary to me.
    It'll happen to you too."

    Grandpa Simpson



    The Fifth Continent

  3. #3
    Just 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...

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,695
    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!

  6. #5
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    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.
    elija likes this.

  7. #6
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    3,638
    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.
    "I used to be with it, then they changed what it was.
    Now what was it isn't it, and what is it is weird and scary to me.
    It'll happen to you too."

    Grandpa Simpson



    The Fifth Continent

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •