**用C语言做什么? [英] What does ** do in C language?

查看:88
本文介绍了**用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){}

所以:

  • 这是什么(**)?
  • 它有什么用?
  • 它在内存中如何表示?
  • what is this (**)?
  • what use does it have?
  • how is it represented in memory?

推荐答案

在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.

现在让我们假设您主要通过以下方式声明了一个指针

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天全站免登陆