如何从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