1

我正在将 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)为什么 sum 没有星号,我认为 sum 被声明为指针。
3) 为什么可以将 y 分配给 z?

我已经学会了这些,但是它们已经很长时间了,有人能给我提示吗?

4

6 回答 6

0

基本指针概念:

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
于 2012-11-20T22:36:00.937 回答
0

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

于 2012-11-20T22:39:45.897 回答
0
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...
}
于 2012-11-20T22:52:55.087 回答
0
  1. 值 y 和 z 不需要星号,因为添加星号会取消引用它们,并且您希望传递指针而不是值。
  2. 同样,您需要指针而不是值。虽然从代码看来你确实想要这个值,所以那里可能应该有一颗星。
  3. 您可以将一个指针分配给另一个指针,这意味着更改指针指向的地址。所以当y=zy现在点哪里z点。
于 2012-11-20T22:28:15.467 回答
0
  1. 它们不需要星号,因为您不会取消引用它们,而是作为指针传递。
  2. 您可以比较指针,但这可能不是您想要的。
  3. 分配指针是可以的,但请注意,它与复制值不同。
于 2012-11-20T22:28:22.763 回答
0

对 Q1 的回答:您正在传递y并传递z给另一个以指针作为参数的函数,为什么要传递指针的值?

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

于 2012-11-20T22:28:37.933 回答