**在C语言中有什么作用? [英] What does ** do in C language?

查看:48
本文介绍了**在C语言中有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 C 新手,具有良好的 Java 背景,我正在尝试理解指针和数组.

I'm new to C with a good background in java and I'm trying to understand pointers and arrays.

我知道下标 operator[] 是数组定义的一部分,所以:

I know that subscript operator[] is part of an array definition, so:

int numbers[] = {1,3,4,5};

将创建一个整数数组,它将在内存中表示为 16 个字节,4 个 4 个字节:

would create a integer array, which would be represented in memory as 16 bytes, 4 lots of 4 bytes:

numbers[0] = 1, address 0061FF1C
numbers[1] = 3, address 0061FF20
numbers[2] = 4, address 0061FF24
numbers[3] = 5, address 0061FF28

然而,当涉及到指针时,我的知识开始崩溃,所以如果我要创建一个指向数组数字的指针,我会执行以下操作:

However, when it comes to pointers my knowledge starts to break down, so if I was to create a pointer to the array numbers I would do the following:

int *pNumbers = &numbers[0];

看起来像这样:

我猜它的大小是 4 个字节?

And I'm guessing it would be of size 4 bytes?

但是 ** 我读作指向指针的指针"这对我来说毫无意义,为什么有人想要一个指向指针的指针,当然如果 a->b->c 那么 a->c 就足够了?我知道我遗漏了一些东西,它必须与数组有关,因为 argv 可以是 char[ ]char ** 类型> 如下图所示:

However the ** I read as "pointer to a pointer" which makes no sense to me, why would anyone want a pointer to a pointer, surely if a->b->c then a->c would suffice? I know I'm missing something, and it must have something to do with arrays as argv can be of type char[ ] or char ** as seen bellow:

int main(int argc, char **argv){}

所以:

  • 这是什么 (**)?
  • 它有什么用?
  • 它在内存中是如何表示的?

推荐答案

在 C 中,参数是按值传递的.例如,如果您在 main

In C arguments are passed by values. For example if you have an integer varaible in main

int main( void )
{
    int x = 10;
    //...

和下面的函数

void f( int x )
{
    x = 20;
    printf( "x = %d\n", x );
} 

那么如果你像这样在 main 中调用函数

then if you call the function in main like this

f( x );

然后参数获取main中变量x的值.然而,参数本身在内存中所占的空间与参数不同.所以函数中参数的任何变化都不会影响到main中的原始变量,因为这些变化发生在不同的内存范围.

then the parameter gets the value of variable x in main. However the parameter itself occupies a different extent in memory than the argument. So any changes of the parameter in the function do not influence to the original variable in main because these changes occur in different memory extent.

那么如何改变函数中main中的变量呢?

So how to change the varible in main in the function?

您需要使用指针传递对变量的引用.

You need to pass a reference to the variable using pointers.

在这种情况下,函数声明看起来像

In this case the function declaration will look like

void f( int *px );

函数定义为

void f( int *px )
{
    *px = 20;
    printf( "*px = %d\n", *px );
} 

在这种情况下,原始变量 x 占用的内存范围发生了变化,因为在函数中我们可以使用指针访问该范围

In this case it is the memory extent occupied by the original variable x is changed because within the function we get access to this extent using the pointer

    *px = 20;

自然必须在 main 中调用该函数,例如

Naturally the function must be called in main like

f( &x );

考虑到作为指针的参数本身px通常是函数的局部变量.那就是函数创建这个变量并用变量x的地址初始化它.

Take into account that the parameter itself that is the pointer px is as usual a local variable of the function. That is the function creates this variable and initializes it with the address of variable x.

现在假设您在 main 中声明了一个指针,例如以下方式

Now let's assume that in main you declared a pointer for example the following way

int main( void )
{
   int *px = malloc( sizeof( int ) );
   //..

和定义的函数

void f( int *px )
{
    px = malloc( sizeof( int ) );

    printf( "px = %p\n", px );
}

作为参数 px 是一个局部变量,分配给它的任何值都不会影响原始指针.该函数更改了与main中原始指针px所占用的范围不同的内存范围.

As parameter px is a local variable assigning to it any value does not influence to the original pointer. The function changes a different extent of memory than the extent occupied by the original pointer px in main.

如何改变函数中的原始指针?只需要通过引用传递即可!

How to change the original pointer in the function? Just pass it by reference!

例如

f( &px );
//...

void f( int **px )
{
    *px = malloc( sizeof( int ) );

    printf( "*px = %p\n", *px );
}

在这种情况下,存储在原始指针中的值将在函数内更改,因为使用解引用的函数访问与定义原始指针相同的内存范围.

In this case the value stored in the original pointer will be changed within the function because the function using dereferencing access the same memory extent where the original pointer was defined.

这篇关于**在C语言中有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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