* pointer not needed?


#1

Hello!!

I have some doubts about the ponter we can see in page 58, *myPointer;
after being declared we can see:

myPointer = NULL;

and here we dont see the ’ * ’ anymore.
The same thing happens in page 69 with startOfBuffer, so if I got it right, one you have declared a pointer YOU DONT NEED TO SHOW THE ’ * ’ TO ASSING VARIABLE’S VALUE TO THE POINTER.

thanks!!..ah!..the book is great!!..even for a lawyer!!


#2

That’s right – You include a * when you declare a variable that holds an address, but then you can use that variable without the *.


#3

But if you DO, the meaning (namely, the effect) of the assignment changes totally.

For example:

//  main.m

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    int *foo = NULL;
    int bar = 32;
    int fooBar = 64;

    foo = &bar;
    // *foo == bar
    assert (*foo = bar);
    NSLog (@"bar = %d fooBar = %d *foo = %d", bar, fooBar, *foo);

    foo = &fooBar;
    // *foo = fooBar;
    assert (*foo == fooBar);
    NSLog (@"bar = %d fooBar = %d *foo = %d", bar, fooBar, *foo);

    *foo = bar;
    // *foo == bar; fooBar == bar
    assert (*foo == bar);
    assert (fooBar == bar);
    NSLog (@"bar = %d fooBar = %d *foo = %d", bar, fooBar, *foo);

    return 0;
}

Note what happens after the last assignment statement (*foo = bar;)


#4

dear ibex10

please tell me, why does the fooBar changes too, but not in the other way ???
mean *foo = fooBar; doesn’t change bar. I also tried other names instead fooBar, eleminated the foo in it, etc.
did i overlook something?
i don’t see what happened. but I saw you are very profound in explaining, i’m curious…
greez


#5

Thank you for trying to understand and also for being curious.

The assignment statement above would only change bar if foo contained the address of bar.

foo = &bar;      // let foo contain the address of bar (let foo point to bar)
// *foo == bar

*foo = fooBar;  // set the value of whatever foo points to to the value of fooBar
// *foo == fooBar, bar == fooBar

#6

oh, sure, cool, thank you very much.
variable (declared with *, to point to an adress) first set to an explicit adress and then set to the value wich is already on an other adress. then both adresses and the variable (token with %d and asterix variable, pointing of one of the adresses) shows the same content.
ah, that was an easy one :slight_smile:

PS: ibex10 I read your explaination with the factorial-theory, and yes that was a heavy one! even (or specially?) if you get the logic within the behaviour of the stack and its frames. I tried to understand en.wikipedia.org/wiki/Factorial but…hm I understood the behaviour of the beer-thing, so it’s enough so far :slight_smile:

keep teaching,
peace
:mrgreen:
stefan


#7

Ibex,
I just finished reading Chapter 8. However, I am having trouble understanding you example above. If I am understanding what you wrote correctly, the first statement assigns the hexidecimal value of bar’s adress to the foo pointer. Then you print what’s actually at this address (which the foo pointer now references). However, how is this any different than what you are doing in the third statement? (aren’t you just saying that the foo pointer should now point to the integer 32 instead of the address of bar)

    *foo = bar;
    // *foo = bar; fooBar = bar
    NSLog (@"bar = %d fooBar = %d *foo = %d", bar, fooBar, *foo);

If you or someone else could clarify what this difference is. That would be great. Thanks.


#8

First of all, there is no such thing as a hexadecimal value of an address. However, there is a hexadecimal representation of the value of an address.

The first statement just assigns the address of bar (of type int) to foo (of type int *).

The third statement changes the value of the object pointed to by foo (that object is fooBar due to the second statement.)
(By the way, the comment after the third statement should be // *foo == bar; fooBar == bar. I will correct this error next.)