Advertisement

Pointers pointers pointers

Started by February 13, 2003 05:11 PM
5 comments, last by SSJCORY 21 years, 9 months ago
I need help with pointers. Declaration of, use of, and how to create a pointer to a function. Plz help. Favorite Quotes:Gandalf: You shall not pass!|Gollum: My Precious!|Smeagol: My little hobbitses.| My website!
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
www.google.com

Welcome to the internet!
Advertisement
A pointer is actually a DWORD variable that holds the memory address to a variable/function.

Here''s how to use a pointer:
01: int nVariable = 5;02:03: Increment( &nVariable, 5 );  // Increment nVariable by 504:05: void Increment( int *nPointer, int IncrementValue )06: {07:    *Pointer += IncrementValue;08: } 


Line 01: Declare an int variable

Line 03: Call the function ''Increment()'' and pass as arguments the address in the memory of
the ''nVariable'' variable by using the ''& '' character, and the value to increment.
Whenever you want to pass the address of a variable you use the ''& '' character. i.e.
int *nPointer;  // Declare a pointer to an int   variable.nPointer = &nVariable  // the ''nPointer'' will have the memory address of ''nVariable'' 


Line 07: Increment the variable the pointer is pointing to.
WARNING: do not increment the pointer like ''Pointer += IncrementValue'', this is WRONG
because if you do that you''ll change the memory address the pointer is pointing to.
When you have a pointer and want to modify the variable it is pointing to use the ''*
character before the pointer like shown in the code above.


I''m not good at explaining things but I guess you''ll crack it soon

KaM1KaZ3
int (*nPointerToAFunctionThatReturnsAnInt)();

//Parentheses are necessary otherwise the compiler will treat it
//as a standard function declaration.

*nPointerToAFunctionThatReturnsAnInt = &ThisReturnsAnInt

//The pointer now ''points'' to a function that returns an int.
//If memory serves me correct, you don''t have to have the address
//operator(&) present in the assignment.

int ThisReturnsAnInt()
{
return 1;
}

I don''t know if this is totally accurate. Outside of school, I''ve rarely used pointers to functions.

Quaggy

This should some pointers to functions up:

void func(int s) { cout << s; }

cout << func; // Prove that func is a pointer itself

/* Create a Pointer to func */

void (*pFuncPtr)(int); // Take care that return vals and
// arg lists match

pFunctPtr = func; // pFuncPtr points to func

/* prove it */
pFuncPtr(5);
func(5);

not too tough, just make sure you return tpes and arg lists match when creating a pointer to a func

hope this helped!
For information about function pointers I haven't been able to find a better resource than The Function Pointer Tutorials. This is very comprehensive list of tutorials and covers C and C++ syntax and usage. It really helped to distill some ideas about callbacks and the like in my mind.

Enjoy!

[edited by - Fingolfin on February 14, 2003 6:26:45 PM]
Advertisement
quote:
void func(int s) { cout << s; }

cout << func; // Prove that func is a pointer itself


Just to be clear on this func is not a pointer to func() it is a function designator . It is just a name that specifies, in this case, the function defined above, and has a function type. In the case above it is true however that func decays to an rvalue of type pointer to function and so returns the address of func().

Small point perhaps, but then there are a very few contexts where this isn't the case and func as a function pointer doesn't make sense e.g. if used as an argument to sizeof().

i.e.

sizeof( func )

is an error.

Cheers.

P.S. Here is a link to a lengthy thread on alt.comp.lang.learn.c-c++ and comp.lang.c that is quite informative on this topic.

[edited by - Fingolfin on February 14, 2003 7:17:53 PM]

This topic is closed to new replies.

Advertisement