Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing a pointer to a built-in type defined on the fly in c++

I want to call a function of a library (that I can not modify)

void function(int* i, char* c);

Is there a way to call the function defining the int and the char on the fly?

i.e. doing something like

function(&1, &'v');

instead of

int int_1 = 1;
char char_1 = 'v';
function(&int_1, &char_v);

This would enormously decrease the length of my code while increasing readability.

like image 228
dPol Avatar asked Dec 15 '22 15:12

dPol


2 Answers

As others have noted, the answer is no...

You could simulate it by overloading function :

void function(int i, char c)
{
  function(&i, &c);
}

So now you can write function(1, 'v')

like image 190
Sean Avatar answered Dec 29 '22 00:12

Sean


Why would you want to do so? Passing a variable as a non const pointer indicates that the callee intends to modify the parameter that is passed therefore it cannot be an rvalue.

So passing a pointer to a literal would be meaningless (unless it is a string literal which is different). Moreover as it is obvious to you, you cannot determine an address of a literal as it is not addressable. The only constant that could be meaningful is a null pointer or some absolute address to an addressable memory

like image 30
Abhijit Avatar answered Dec 29 '22 01:12

Abhijit