Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an int pointer on the stack?

Tags:

c++

void func(int* param){};
func(&123); //error: '&' on constant
funct(&int(123)) //error
like image 547
lovespring Avatar asked Dec 11 '10 17:12

lovespring


People also ask

How do you create an int pointer?

The syntax of declaring a pointer is to place a * in front of the name. A pointer is associated with a type (such as int and double ) too. Naming Convention of Pointers: Include a " p " or " ptr " as prefix or suffix, e.g., iPtr , numberPtr , pNumber , pStudent .

Can you have a pointer to the stack?

The Stack Pointer (SP) register is used to indicate the location of the last item put onto the stack. When you PUT something ONTO the stack (PUSH onto the stack), the SP is decremented before the item is placed on the stack.

Can you add an integer to a pointer?

Pointer arithmetic and arrays. Add an integer to a pointer or subtract an integer from a pointer. The effect of p+n where p is a pointer and n is an integer is to compute the address equal to p plus n times the size of whatever p points to (this is why int * pointers and char * pointers aren't the same).


1 Answers

That's not how pointers work.

You must first allocate memory for your 123, like this:

int x = 123;
func(&x);
like image 101
F. P. Avatar answered Oct 27 '22 05:10

F. P.