First off, unless you are defining a constant using the C++ [tt]const[/tt] type modifier, it is a Bad Thing to initialize a pointer with any constant address other than NULL (which is usually [tt]#define[/tt]d as [tt](void *) 0[/tt] in modern C headers). I suspect that this wasn't what you meant to do in any case; it looks to me like what you really wanted was to initialize the address pointed to, which (as I'll explain) doesn't work this way.
A cast to a pointer type, such as [tt](int *)address;[/tt], does not return the address of the variable; rather, it converts the
value of the variable to a pointer. So,
[tt]int* pointer;
*pointer = (int *)address;[/tt]
is invalid, as you assigning an int variable to an int* value. To assign an address to a pointer, you need to use the reference operator, '&', like so:
[tt]pointer = &address;[/tt]
Note that the indirection operator, '*' is missing; this because you are assigning the value to the pointer itself, not the address it points to. After the assignment,
[tt]
pointer == &address; /* the address of 'address' */
*pointer == address /* the value of 'address' */[/tt]
and if you assign
[tt]address = 5;
*pointer = 10;[/tt]
the result is address == 10.
You also seem confused about the relation between pointers to a type, and arrays of the type. While C does provide a lot of syntactic sugar that allows you to use them interchangebly in many places, they are not the same thing. A pointer is a variable that can hold an address to another variable of a given type, whereas an array is a block of variables. Among important differences between them is that whereas a array declaration allocates the local memory for the array, a pointer declaration only allocates memory for the pointer itself, not for the variable it points to. Thus, when you declare
[tt]int array[10];[/tt]
there is allocated local memory for the variable [tt]array[[0]][/tt]; whereas if you declare
[tt]int* pointer;[/tt]
there is allocated local memory for [tt]pointer[/tt] itself, but not for the value that [tt]*pointer[/tt] refers to. A pointer can be assigned to pointer to any variable of the type it points to; thus,
Code: Select all
int var, array[10], *pointer;
var = 10;
array[0] = 15;
*pointer = &var;
printf("%d\n", *pointer);
*pointer = array; /* note the syntactic sugar; this would otherwise be &array */
printf("%d"\n, *pointer);
printf("%d\n", (int) pointer);
Prints 10, then 15, then the address of [tt]array[/tt]. whatever that is.
You also seem to have misubderstood the use of the initialization idiom. You have to remember that in initializing a pointer, the value initialized is that of the pointer itself, not of any pointed to item. Thus,
[tt]int *pointer = (int *)address;[/tt]
is equivalent to
[tt] int *pointer;
pointer = (int *) address;
[/tt]
not
[tt] int *pointer;
*pointer = (int *) address;
[/tt]
Thus, many find it helpful to declare pointer with the asterisk near the type name rather than the variable name, to emphasize that it is a pointer
type, like so:
[tt]int* pointer;[/tt]
One last note: in BBcode, [[0]] (a zero betwen two brackets) prints a hollow bullet, [0] like this.
Thus, if you are referring to an array subscript of zero ([[0]]) in text (as opposed to in a code block), you need to escape the brackets by doubling them, like so:
[[[[0]]]]
HTH.
Sorry if this is a bit sloppy or rude; it's rather early in the morniing for me. Comments and corrections are, as always, welcome, especially with a tricky subject like this.