I need some pointers with pointers.
What is the difference between a variable preceded by an "*" and an "&"?
Some of my sample code uses one and some the other. I'm getting compiler errors trying both saying the variable is not defined. First use this function.
kent.johnstone_1 0 Light Poster
Recommended Answers
Jump to PostWhen declaring a variable, preceding the variable name with a
*
makes it a pointer. Preceding it with a&
is illegal (in C - in C++ it makes the variable a reference).In an expression
&
takes a variable and produces a pointer to that variable, whereas*
takes …
Jump to PostWhat sepp2k said is quite correct, but difficult to understand for noobs. Here is possibly a more understandable example:
int anInt = 5; int* ptrToAnInt = &anInt; anInt += 2; printf("anInt and ptrToAnInt == %d (%d)\n", anInt, *ptrToAnInt);
This should result in the output "anInt and …
All 15 Replies
sepp2k 378 Practically a Master Poster
rubberman 1,355 Nearly a Posting Virtuoso Featured Poster
kent.johnstone_1 0 Light Poster
kent.johnstone_1 0 Light Poster
ddanbe 2,724 Professional Procrastinator Featured Poster
Moschops 683 Practically a Master Poster Featured Poster
ddanbe commented: Quite right! +15
kent.johnstone_1 0 Light Poster
kent.johnstone_1 0 Light Poster
ddanbe 2,724 Professional Procrastinator Featured Poster
kent.johnstone_1 0 Light Poster
ddanbe 2,724 Professional Procrastinator Featured Poster
kent.johnstone_1 0 Light Poster
kent.johnstone_1 0 Light Poster
ddanbe 2,724 Professional Procrastinator Featured Poster
kent.johnstone_1 0 Light Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.