Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从char获取char***_C_String_Pointers_Char_Printf - Fatal编程技术网

如何从char获取char***

如何从char获取char***,c,string,pointers,char,printf,C,String,Pointers,Char,Printf,我理解&用于引用对象的地址,因此&char*=char**。是否有任何方法可以将其反转,以便我可以从char**获取char* 因此,我: char** str; //assigned to and memory allocated somewhere printf ("%s", str); //here I want to print the string. 我该怎么做呢?您可以使用 解引用运算符对指针变量进行操作,并返回与指针地址处的值等效的l值。这称为“取消引用”指针 char** s

我理解&用于引用对象的地址,因此
&char*=char**
。是否有任何方法可以将其反转,以便我可以从
char**
获取
char*

因此,我:

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

printf ("%s", str); //here I want to print the string.
我该怎么做呢?

您可以使用

解引用运算符对指针变量进行操作,并返回与指针地址处的值等效的l值。这称为“取消引用”指针

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

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

取消引用
str

print ("%s", *str); /* assuming *str is null-terminated */

如果您有一个
T*
(称为指向
T
类型的对象的指针)并希望获得
T
(T类型的对象),则可以使用操作符
*
。它返回指针指向的对象

在本例中,您有一个指向类型为
char*
(即:
(char*)*
)的对象的指针,因此可以使用
*

另一种方法可以是使用操作符
[]
,即您用来访问阵列的方法
*s
等于
s[0]
,而
s[n]
等于
*(s+n)

如果您的
char**s
是一个
char*
数组,则使用
printf(“%s”,*str)
只打印第一个。在这种情况下,如果使用
[]
,可能更容易阅读:

for( i = 0; i < N; ++ i ) print( "%s\n", str[i] );
用于(i=0;i
尽管它在语义上等同于:

 for( i = 0; i < N; ++ i ) print( "%s\n", *(str+i) );
用于(i=0;i