I'm sorry if this is a RTFM question, but if I create a C pointer
int *pointer = (int *) address;
when I reference *pointer, shouldn't that be the same as pointer[0]? I mean, I always thought that *pointer was essentially the same thing, but one of my programs generated crap with *pointer but not with pointer[0].
Quick pointer question...
Re:Quick pointer question...
Pointers store an address, but in that address is the kind of information which the pointer refers to. So an int * holds an address from memory in which is an integer. So, your example would say this:
int *pointer=(int *)address;
pointer holds the number address, while *pointer would recover the information stored in address. Run this test program to better understand this:
I hope this helps.
int *pointer=(int *)address;
pointer holds the number address, while *pointer would recover the information stored in address. Run this test program to better understand this:
Code: Select all
/* Pointer test program */
#include <stdio.h>
int main(int argc, char **argv)
{
int someNumber=122;
int *somePointer=&someNumber; /* & returns the address of whatever follows it. */
printf("&someNumber = %d\n",(int)&someNumber);
printf("someNumber = %d\n",someNumber);
printf("somePointer = %d\n",(int)somePointer);
printf("*somePointer = %d\n",*somePointer);
return 0;
}
Re:Quick pointer question...
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,
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.
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);
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.
Re:Quick pointer question...
Perhaps I should've posted in the OS section to put this in the right context: Honestly, I do want pointers to point to addresses. I'm building structures within unregulated RAM.
And yes, I'm quite aware that
will make pointer point to 0x9c000 in memory, not to address' address.
so your
is exactly what I wanted to do.
Amongst the extra stuff that was (though useful to others) unecessary. I still don't have an answer. But I'll try to clarify.
So let's say I have a pointer pointing to 0x9c000 like in the first code snippet. *Pointer should refer to the value stored in the first four bytes at 0x9c000, but shouldn't pointer[[0]] do the same because it's essentially like saying pointer += 0; and then getting *pointer, right?
And yes, I'm quite aware that
Code: Select all
int address = 0x9c000;
int *pointer = (int *) address;
so your
Code: Select all
int *pointer;
pointer = (int *) address;
Amongst the extra stuff that was (though useful to others) unecessary. I still don't have an answer. But I'll try to clarify.
So let's say I have a pointer pointing to 0x9c000 like in the first code snippet. *Pointer should refer to the value stored in the first four bytes at 0x9c000, but shouldn't pointer[[0]] do the same because it's essentially like saying pointer += 0; and then getting *pointer, right?
Re:Quick pointer question...
Yep. *Pointer = Pointer[[0]]. Sorry for the confusion, but it sounded like a beginner's question. ^^
Re:Quick pointer question...
That's all right, I'd prefer someone talking below my level of understanding than above it . I mean if someone had said "Well, if you're dereferencing the quantum foils of the pickle matrix" it would've been pointless, instead of making sure that I had the basics.
Thanks.
Thanks.
Re:Quick pointer question...
In general, *(pointer + x) is the same as pointer[x].
In fact, the array notation (with []) is just a shortcut. This is valid: 3["abcdef"] == 'c'.
In fact, the array notation (with []) is just a shortcut. This is valid: 3["abcdef"] == 'c'.