Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cpp / c++ get pointer value or depointerize pointer

Tags:

c++

pointers

I was wondering if it's possible to make a pointer not a pointer..

The problem is I have a function that accepts a pointer for an paramater for me to easily get a value to that pointer. It's a simple int so I was wondering if I could just get that value without needing to send around a pointer wherever I want the value to land.

I don't want the function to return the value as an int as it's giving a value to 2 pointers!

like image 937
Rasmus Avatar asked Jan 19 '13 23:01

Rasmus


People also ask

How do I get the value of a pointer in CPP?

To get the value pointed to by a pointer, you need to use the dereferencing operator * (e.g., if pNumber is a int pointer, *pNumber returns the value pointed to by pNumber . It is called dereferencing or indirection).

Does pointer type matter in C?

From the memory-allocation point-of-view, you're right. A pointer variable on a 64-bit architecture occupies 8 bytes, no matter what type of pointer it is. But the C compiler needs to know more about a variable than its size.

What does * and &indicate in pointer?

The fundamental rules of pointer operators are: The * operator turns a value of type pointer to T into a variable of type T . The & operator turns a variable of type T into a value of type pointer to T .


1 Answers

To get the value of a pointer, just de-reference the pointer.

int *ptr; int value; *ptr = 9;  value = *ptr; 

value is now 9.

I suggest you read more about pointers, this is their base functionality.

like image 168
James McDonnell Avatar answered Sep 22 '22 15:09

James McDonnell