• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

noob c++ pointer question

eon

2[H]4U
Joined
Oct 11, 2003
Messages
2,218
been a long summer so im a bit rusty

have yet to figure out how to use the dev c++ debugger with an allegro graphic program so im kind of winging it and here is where i problem comes when i use a class function that uses a pointer parameter and then uses that pointer parameter to as the parameter for a second mutator function. Somehow when i attempt to use function it, something goes wrong (i think it doesnt change the pointer passed in).

here is a general idea of what im talking about

Code:
void func1(pointer* p)
{
....
func2(p);
}

void func2(pointer*p)
{
....
}

void func3()
{
pointer* ptr;
func1(ptr);
}

shouldnt this code work in properly changing the ptr based on the code within func1 and func2?
 
The code doesn't change the pointer because there's never an assignment to the pointer.
 
shouldnt this code work in properly changing the ptr based on the code within func1 and func2?

No. When you pass a pointer you are passing its value, not the pointer itself. The pointers ptr, p in func1 and p in func2 are all different pointers, they just have the same value. Try this instead:

Code:
#include <iostream>

int val = 10;

void func2(int** p)
{
    *p = &val;
}

void func1(int** p)
{
   func2(p);
}

int main()
{
   int* ptr = 0;
   func1(&ptr);

   if (ptr) std::cout << "Jessica Alba is a " << *ptr << "!!!" << std::endl;
   return 0;
}
 
ok thanks, i vaguely remember this now, we actually did not get direct instruction about this in any class afaik

the double pointer is the address of a pointer, so if int* p points to some address that holds the value (*p), int** p points to the address that holds the value p. (lol correct me if im wrong :p)

i have a second question,
isnt a function that uses a pointer parameter like this: void func(int* p) the same as passing by reference: void func(int &p)? except func(int& p) automatically dereferences p in the function?
Code:
void func1(int* p)
{
(*p) = 10;
}
void func2(int& p)
p = 10;

main()
{
int* x = new int;
func1(x);

int y;
func2(y);
}
so will (*x) and y both be 10?



if so, couldnt i do the same with void func(int&& p) and void func(int** p)?
Code:
int a = 10;
void func1(int&& p)
{
p = &a;
}
void func2(int** p)
{
*p = &a;
}
main()
{
int x;
func1(x);   //or is it func1(&x)?
int* y = new int;
func2(&y);

}
&x and y both point to a?
hmm i kind of confusing myself now
 
i have a second question,
isnt a function that uses a pointer parameter like this: void func(int* p) the same as passing by reference: void func(int &p)? except func(int& p) automatically dereferences p in the function?
Code:
void func1(int* p)
{
(*p) = 10;
}
void func2(int& p)
p = 10;

main()
{
int* x;
func1(x);

int y;
func2(2);
}
so will (*x) and y both be 10?
Well, first of all your program won't work at all. You create a pointer x that is not pointing to anything and then you try to set it equal to 10. You have to first create an integer, then create the pointer and make the pointer point to that integer. Then you can use the pointer. Also, when you call func2, you should pass it y, not 2. If those are taken care of, then yes, both (*x) and y will be equal to 10.
if so, couldnt i do the same with void func(int&& p) and void func(int** p)?
Code:
int a = 10;
void func1(int&& p)
{
p = &a;
}
void func2(int** p)
{
*p = &a;
}
main()
{
int x;
func1(x);   //or is it func1(&x)?
int* y;
func2(&y);

}
&x and y both point to a?
hmm i kind of confusing myself now

The && doesn't work. If you want to do this, I think you have to use **. When you run your code above, again y points to nothing, but assuming it did, (*y) would be equal to 10, so y contains the address of a.
 
ya i forgot to allocate the initialized pointers with a new, ill change it

are you saying a double reference never works?
 
You can't have a reference to a reference.
You can have a reference to a pointer.
You can't have a pointer to a reference.
You can have a pointer to a pointer.

In fact, you can have as much dereference to pointers as you dare:

Code:
int _tmain(int argc, _TCHAR* argv[])
{
	int n = 35;
	int *pn = &n;
	int **ppn = &pn;
	int ***pppn = &ppn;
	int ****ppppn = &pppn;
	int *****pppppn = &ppppn;
	int ******ppppppn = &pppppn;
	int *******pppppppn = &ppppppn;
	int ********ppppppppn = &pppppppn;
	int *********pppppppppn = &ppppppppn;
	int **********ppppppppppn = &pppppppppn;
	int ***********pppppppppppn = &ppppppppppn;
	int ************ppppppppppppn = &pppppppppppn;
	int *************pppppppppppppn = &ppppppppppppn;
	
	printf( "n is %d\n", *************pppppppppppppn );
	return 0;
}
 
You can't have a reference to a reference.
You can have a reference to a pointer.
You can't have a pointer to a reference.
You can have a pointer to a pointer.

In fact, you can have as much dereference to pointers as you dare:

Code:
int _tmain(int argc, _TCHAR* argv[])
{
    int n = 35;
    int *pn = &n;
    int **ppn = &pn;
    int ***pppn = &ppn;
    int ****ppppn = &pppn;
    int *****pppppn = &ppppn;
    int ******ppppppn = &pppppn;
    int *******pppppppn = &ppppppn;
    int ********ppppppppn = &pppppppn;
    int *********pppppppppn = &ppppppppn;
    int **********ppppppppppn = &pppppppppn;
    int ***********pppppppppppn = &ppppppppppn;
    int ************ppppppppppppn = &pppppppppppn;
    int *************pppppppppppppn = &ppppppppppppn;
    
    printf( "n is %d\n", *************pppppppppppppn );
    return 0;
}
Mike, internally, do ALL of those pointers point to the address of the integer n or am I looking at this wrong? If that's the case, what's the memory usage versus just using the original integer without any of the pointers?

Doing Data Structures in C++ this semester and I've been stuck in Java and Delphi the last year and a half, so my pointer arithmetic is rusty as all hell, so this is a good refresher.

Sorry again for the threadjack!
 
Nope, only pn points to n. ppn points to pn, which points to n. And so on.

Pointer questions are very common in September and October.
 
I hate to threadjack here but what does using double pointer (int** p) operators do?

BTW, I don't believe calling ** an operator is correct for my usage above, I think it is simply declaration syntax. A usage like "int x = **ptr;" would be considered an operator.
 
If that's the case, what's the memory usage versus just using the original integer without any of the pointers?

Pointers have size, so each pointer declared will consume some memory in addition to the integer. So using just the integer with no pointers would consume less memory.
 
Though it seems a little unintuitive, the standard does call * and & operators in this context; see for example paragraph 2 in section 8. ptr-operator is the grammar identifier used for & and * (and their scopings).
 
I see...I've come to call any "symbol character that makes the language do something" an operator which is probably a horrible habit.

It'll do my soul good to work with low(er) level stuff again for a while I think. Real interesting stuff once you get your bearing on it.
 
Yeah, that's a pretty sloppy definition. An operator can be more than one character (like ++ in C++), or even a couple of words (like UNION ALL in SQL).

Here, * is making the langauge do something different, so I'd be tempted to call it a modifier instead of an operator, though. For me, that's because an operator takes operands; if "*" is an operator in

Code:
int *p;

then what are the operands? And, in

Code:
int p;
where's the operator?
 
Back
Top