在C语言中,什么是变量声明带有两个星号(**)是什么意思? [英] In C, what does a variable declaration with two asterisks (**) mean?

查看:4076
本文介绍了在C语言中,什么是变量声明带有两个星号(**)是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用C工作,我有点生疏了。据我所知, * 有三种用途:

I am working with C and I'm a bit rusty. I am aware that * has three uses:


  1. 声明一个指针。

  2. 解引用指针。


不过,这是什么意思时,有两个星号( ** )的变量声明之前:

However, what does it mean when there are two asterisks (**) before a variable declaration:

char **aPointer = ...

谢谢,

斯科特

推荐答案

它声明的指针字符指针

It declares a pointer to a char pointer.

这样一个指针的使用会做这样的事情,如:

The usage of such a pointer would be to do such things like:

void setCharPointerToX(char ** character) {
   *character = "x"; //using the dereference operator (*) to get the value that character points to (in this case a char pointer
}
char *y;
setCharPointerToX(&y); //using the address-of (&) operator here
printf("%s", y); //x

下面是另外一个例子:

char *original = "awesomeness";
char **pointer_to_original = &original;
(*pointer_to_original) = "is awesome";
printf("%s", original); //is awesome

使用 ** 使用数组:

char** array = malloc(sizeof(*array) * 2); //2 elements

(*array) = "Hey"; //equivalent to array[0]
*(array + 1) = "There";  //array[1]

printf("%s", array[1]); //outputs There

在阵列中的 [] 操作者基本指针放在前面指针运算,因此,该方法数组[1] 将被评估如下:

The [] operator on arrays does essentially pointer arithmetic on the front pointer, so, the way array[1] would be evaluated is as follows:

array[1] == *(array + 1);

这就是为什么数组索引从 0 启动的原因之一,是因为:

This is one of the reasons why array indices start from 0, because:

array[0] == *(array + 0) == *(array);

这篇关于在C语言中,什么是变量声明带有两个星号(**)是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆