Hi,
WHen i finished declaring a pointer, for example :
Do i have to place an asterisk before the variable whenever i use it? But i saw in some programs that '*' is not needed ?Code:char *strPtr;
Hi,
WHen i finished declaring a pointer, for example :
Do i have to place an asterisk before the variable whenever i use it? But i saw in some programs that '*' is not needed ?Code:char *strPtr;
it completely depends on what you intend to do...
In most cases, using the asterik in the program points to the MEMORY ADDRESS of that variable, and withotu an asterik it points to what the memory address contains.
There are exceptions and I really can't remember.. pointer rules suck.
Should be the other way around, if strPtr is a properly initialized pointer, 'strPtr' will contain some memory address, '*strPtr' dereferences the pointer making its value whatever value is at that address.
Pointer rules don't suck, they just take time to fully understand
what helps me to understand pointers is
when you declare a ptr (note the lack of a '*' cos i'm talkin about it)
char str[10];
char *ptr; /* iv'e just declared a pointer & '*' is the address of it*/
ptr = str; /*when you assign a var to it-no need for the '*' cos your telling the compiler to attach the address of the first element in str to ptr */
if you want your compiler to look at where the pointer is pointing to, then that's what you state
while(*ptr) (for while at the address of ptr)
or if(*ptr == 'A') (if at the address of pointer equals letter'A')
what you need to remember is that a ptr will only access a single element within an array.
hope it helps geez
Before you judge a man, walk a mile in his
shoes. After that, who cares.. He's a mile away and you've got
his shoes.
************William Connoly
that's why they suck.. I can never remember them..Originally posted by *ClownPimp*
Should be the other way around, if strPtr is a properly initialized pointer, 'strPtr' will contain some memory address, '*strPtr' dereferences the pointer making its value whatever value is at that address.
Pointer rules don't suck, they just take time to fully understand
You only need the * if you must access a single character. To refer to the character array, just use strPtr.