English 中文(简体)
指向指针的简单指针
原标题:simple pointers to pointers

我知道这为什么管用:

#include <stdio.h>

void cool_number(int **number) {
  int value = 42;
  int *p = &value;
  *number = p;
}

int main () {
  int *number;
  cool_number(&number);
  printf("number is %d
", *number);
  return 0;
}

我不明白的是,为什么会这样(在我的机器中,它印着3700或类似的东西)。

#include <stdio.h>

void cool_number(int **number) {
  int value = 42;
  int *p = &value;
  int **x = &p;
  number = x;
}

int main () {
  int *number;
  cool_number(&number);
  printf("number is %d
", *number);
  return 0;
}

为什么两者不相等?

最佳回答

我假设它们不是等同的,因为 number 和函数参数的标准一样,在堆栈上以数值通过 编号 。任何您直接对 number cool_ number () 中的 进行的任何修改都正在修改堆栈上的本地副本,并且没有反映在 number main () 中的数值中。

在第一个示例中,您通过脱用 number 来绕过这个例子,该示例告诉计算机修改记忆中某些特定位置,即您在 main () 中也有一个指针。第二个示例中没有这个指针,所以发生的一切就是将本地的 number 指针指针指向别处,而没有实际更新在 main () 中引用的任何内存位置。因此,一旦您返回 main () , 中,您没有显示任何信息。

由于 value is local to the cool_ number () 函数, 设置一个在 cool_ number () 返回后访问的引用无法保证工作, 并且肯定不应该在小/ 玩具示例以外的任何代码中使用。 但在此特定情况下, 它并不与您为什么看到两个代码之间不同的结果有关。

问题回答

两者都是邪恶的,因为它们捕捉到堆叠变量的地址。

第二个参数不做你所期望的,因为您直接指定参数编号,而参数编号只是暂时的,第一个参数改变参数编号指示器的某个东西,这是与主点数字相同的东西。

据我所知 在这两种情况下 你的代码都是错误的

  1. 在第一种情况下,您正在返回堆叠上分配的变量的地址, 函数返回后, 就会立即进行交易 。

  2. 在第二种情况下,第一个大小写有错误存在,加上您是按数值传递的号码,因此更新编号不会在调用函数中得到反映。

在 C 中,参数总是以数值传递。因此,您无法按原样更新所通过参数。例如:

int func(int a)
{
  a = 5; // In this case the value 5 will not be reflected in the caller as what is updated is the local copy of a on the stack
}

int func(int *a)
{

  *a = 5; // This update will show in caller as you are directly updating the memory pointed to by a
   a = malloc(4); //This update will not show in caller as again you are updating the local copy of stack       
}
#include <stdio.h>

void cool_number(int **number) {
  int value = 42; /* this "value" hold 42 value, 
                     and it lifetime is only in this function */
  int *p = &value; /* here we get the address of "value" in memory */
  *number = p;
}

int main () {
  int *number;
  cool_number(&number);  /* after this function the "value" in memory had been recyled
                            and may be used by other program */
  printf("number is %d
", *number); /* so when we print value here it will be 
                                 a unpredictable value, somehow may be crash */
  return 0;
}

同一原则





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签