C语言 指针:C语言中的*p和&p

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27655272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:38:59  来源:igfitidea点击:

pointer: *p and &p in C programming language

cpointers

提问by mercedes

Could you please tell me the difference between *p and &p in C programming language? cause I really have problem with this and I don't know whether *p or &p is ok!!!!

你能告诉我 C 语言中 *p 和 &p 的区别吗?因为我真的有这个问题,我不知道 *p 或 &p 是否可以!!!!

回答by Avnish Mehta

A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before you can use it to store any variable address. The general form of a pointer variable declaration is:

指针是一个变量,其值是另一个变量的地址,即内存位置的直接地址。与任何变量或常量一样,您必须先声明一个指针,然后才能使用它来存储任何变量地址。指针变量声明的一般形式是:

type *var-name;

eg:

例如:

int    *ip;    /* pointer to an integer */
double *dp;    /* pointer to a double */
float  *fp;    /* pointer to a float */
char   *ch     /* pointer to a character */

Look at this program :

看看这个程序:

#include <stdio.h>

int main ()
{
   int  var = 20;   /* actual variable declaration */
   int  *ip;        /* pointer variable declaration */

   ip = &var;  /* store address of var in pointer variable*/

   printf("Address of var variable: %x\n", &var  );

   /* address stored in pointer variable */
   printf("Address stored in ip variable: %x\n", ip );

   /* access the value using the pointer */
   printf("Value of *ip variable: %d\n", *ip );

   return 0;
}

https://www.tutorialspoint.com/cprogramming/c_pointers.htm

https://www.tutorialspoint.com/cprogramming/c_pointers.htm

回答by Gopi

Just take

就拿

int a =10;
int *p = &a;

ais a variable which holds value 10. The address in which this value is stored is given by &a.

a是一个保存值 10 的变量。存储该值的地址由 给出&a

Now we have a pointer p, basically pointer points to some memory location and in this case it is pointing to memory location &a.

现在我们有一个指针p,基本上是指向某个内存位置的指针,在这种情况下它指向内存位置&a

*pgives you 10

*p给你 10

This is called dereferencing a pointer.

这称为取消引用指针。

p = &a /* Gives address of variable a */

Now let's consider

现在让我们考虑

&p

&p

Pointer is a also a data-type and the location in which p is stored is given by &p

指针也是一种数据类型,存储 p 的位置由下式给出 &p