C语言 指针:引用第一个元素是否有助于定位整个数组



我正在将C代码翻译成另一种语言。 我对指针感到困惑。假设我有此代码。 函数 foo 调用函数 bob,它们都是指针。

double
*foo(double *x, double *init, double *a){
   double *y = (double*)malloc(5*sizeof(double));
   double *z = (double*)malloc(5*sizeof(double));
   double *sum, *update;
   sum = (double*)bob(y, z)    //<---Q1: why y and z don't need stars in front of them? 
                               //I thought they are pointers?
   for (i<0; i<5; i++){
       z[i]=y[i]               //Q2: howcome it's ok to assign y to z? 
   }                           //aren't they pointer?(i.e.hold memory address)
}
double
*bob(*arg1, *arg2){
   something...
}

所以
1)为什么y和z前面不需要星星,y和z不就是地址吗?
2)为什么总和没有星号,我认为总和被声明为指针。
3) 为什么可以将 y 分配给 z?

我已经学会了这些,但是它们已经太久了,有人可以给我一个提示吗?

double
*foo(double *x, double *init, double *a){
// x is a pointer to a double, init is a pointer to a double, a is a pointer to a double
// foo is a function returning a pointer to a double
//   and taking three pointers to doubles as arguments
   double *y = (double*)malloc(5*sizeof(double));
   // y is a pointer to a double. It is assigned a pointer returned by malloc.
   //   That pointer returned by malloc points to memory space for 5 doubles.
   double *z = (double*)malloc(5*sizeof(double));
   // z is a pointer to a double. It is assigned a pointer returned by malloc.
   //   That pointer returned by malloc points to memory space for 5 doubles.
   double *sum, *update;
   // sum and update are pointers to doubles    
   sum = (double*)bob(y, z)    //<---Q1: why y and z don't need stars in front of them? 
                               //I thought they are pointers?
   // sum (whose type is pointer to double) 
   //   is assigned a pointer to double returned by bob
   for (i<0; i<5; i++){
       y[i] = z[i]       //and are z and y pointers?!?!
       // y[i] === *(y+i)
       // (y + i) points to the i-th element in the space previously allocated by malloc
       // *(y + i) dereferences that pointer
       // equivalently, y[i] accesses the i-th element from an array
   }
   if(sum<0)
   z=y //Q2: howcome it's ok to assign y to z? 
       //aren't they pointer?(i.e.hold memory address)
   // after the assignment, z contains the same address as y (points to the same memory)
}
double
*bob(*arg1, *arg2){
   something...
}
  1. 值 y 和 z 不需要星号,因为添加星号会取消引用它们,并且您希望传递指针而不是值。
  2. 同样,您需要指针而不是值。尽管从代码来看,您确实想要该值,因此那里可能应该有一个星星。
  3. 您可以将指针分配给另一个指针,这意味着更改指针指向的地址。所以当y=z时,y现在指向z点的位置。
    它们
  1. 不需要星号,因为您不会取消引用它们,而是作为指针传递。
  2. 您可以比较指针,但这可能不是您想要的。
  3. 分配指针是可以的,但请注意,它与复制值不同。

Q1 的 Ans:您正在传递yz到另一个将指针作为参数的函数,为什么要传递指针的值?

答2:它们是指针,您正在将一个分配给另一个

基本指针概念:

double a = 42.0;   // a is a double
double b;          // b is a double
double *x;         // x is a pointer to double
x = &a;  // x is the pointer, we store the address of a in pointer x
b = *x;  // *x is the pointee (a double), we store the value pointed by x in b
// now b value is 42.0

关于 2) 以及为什么 z[i] = y[i] 是可以接受的,我认为最好将您指向此页面,该页面详细描述了数组和指针,尤其是 2.1 到 2.8。 该特定表达式中发生的事情(不一定按顺序)是从位置 y 开始,获取指针,将 i 添加到指针,然后获取指向该位置的值。 从 z 开始,获取指针,将 i 添加到指针,并将值 (y[i]) 分配给该位置。

最新更新