开发者

Pointer vs. variable, Objective-C

I have some difficulties grasping some concepts. Grateful for help.

Let's say you have the following piece of code:

int *intPtr;   // creates a pointer
int count = 10; // initiates an intiger variable
intptr = &count; // ??

The & operator gives the address to a variable, in this case the integer count. The address is then assigned to the intptr. My question is: Why is intptr = count; not sufficient. I know that count is a variab开发者_如何转开发le and intptr is a pointer, but isn´t a variable also just referring to some place in memory?

Hans


count refers to the VALUE of the variable. You don't want to assign the value of count to intptr, you want to assign the address of count. Thus the & operator is used.

If you do intptr = count, you'd be pointing to memory address 10 in this case, which is sure to be in system memory, not your application memory and you'd crash.


It is important to understand that pointers have actually a different data type.

A int variable will hold integer values.

A pointer variable will hold memory addresses.

So it is not right to assign an int variable to a pointer variable (as you suggested intptr = count;)

I believe using a typedef could help you better understand the difference.

Here's a small example:

#include <stdio.h>

typedef int* int_pointer;

int main() {

    int n; // integer
    int_pointer p; // pointer

    n = 5;

    p = &n; // p's value is now n's address

    *p = *p + 1; // add 1 to the value stored at the location that p points to
                 // and put that value back in the same location

    printf("n = %d\n", n);
    printf("*p = %d\n", *p);

    return 0;
}

This program will print

n = 6
*p = 6


A variable is a place in memory, whereas a pointer refers to a place in memory (and the place it refers to can change over time).

In your example, if you'd said intptr = count then it would've still been obvious what you meant (and a compiler could conceivably be written to guess this too). However, it would lead to inconsistencies in the kind of syntax that you could use. The value of an expression is called an lvalue if it refers to something can can be assigned to, and an rvalue otherwise. If the compiler did the kind of "guessing" that you seem to be suggesting, then some, but not all expressions of type int (namely the ones that are lvalues) would be automatically turned into pointers. There are cases where this could lead to very confusing error messages.


You can still do intptr = count, but this will not achieve what you are confused with.

You may want to make a pointer point to a address in memory and if by the grace of God the value in count is a valid address location then you may not get a segmentation fault too.

Ofcourse every variable refers to some place in memory but that doesnt mean equating two different kinds will result in a behaviour which can be assumed based on above fact. There is a well defined definition for a int variable and a pointer to int.


In some functions used in programming either in C or Objective-C or C++ you must pass in a "reference" to the value (in other words, a pointer) as a parameter, not just a variable.

Take this piece of code:

int count;

//Is a C method/function used to read in values //(either numerical or characters) from the command line.

scanf("%i", &count); 

if you tried the same thing, without the "&" (address of) you would get either a compile time error, or runtime error, (depending on what system your working on Win/Linux/OSX as each compiler will do something a little different.[GCC, MingW etc..])

Variables use memory, and stay in memory until the program has finished, and you cannot recover that memory until then.

Where as a "pointer" can be released from memory by using the dealloc method while your program is running, and that memory is freed up during execution of your applicaiton.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜