Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get char* from char**

I understand that & is used to reference the address of object so &char* = char**. Is there anyway to reverse this so that I can get char* from char**?

So I have:

char** str; //assigned to and memory allocated somewhere

printf ("%s", str); //here I want to print the string.

How would I go about doing this?

like image 736
Cheetah Avatar asked Nov 28 '22 04:11

Cheetah


2 Answers

You can use the dereference operator.

The dereference operator operates on a pointer variable, and returns an l-value equivalent to the value at the pointer address. This is called "dereferencing" the pointer.

char** str; //assigned to and memory allocated somewhere

printf ("%s", *str); //here I want to print the string.
like image 121
Jacob Relkin Avatar answered Dec 05 '22 01:12

Jacob Relkin


Dereference str:

print ("%s", *str); /* assuming *str is null-terminated */
like image 27
Alex Reynolds Avatar answered Dec 05 '22 01:12

Alex Reynolds