简体   繁体   English

关于*的简单C问题

[英]Simple C question regarding *

I was wondering what is the difference between these two lines of code? 我想知道这两行代码有什么区别?

int hi;
int *hi;

In the C programming language? 使用C编程语言?

Thanks! 谢谢! Amit 阿米特

int hi;

reserves the space for an int in memory, and each time you reference hi , you either read or write directly that int in memory space. 在内存中保留一个int的空间,并且每次您引用hi ,您都可以在内存空间中直接读取或写入该int

int *hi;

reserves the space for a pointer to an int in memory, each time hi is used, the pointer is either read or written. 每次使用hi ,都会为内存中的int pointer保留空间,该指针将被读取或写入。 Meaning that you are not working with an int , only a pointer to an int - there must exist an int somewhere for the pointer to reference something workable. 这意味着你没有与工作int ,只有一个指向int -必须存在一个int某处为指针引用一些可行的。 For instance 例如

 int hi;
 int *phi;
 phi = &hi; // phi references the int hi
 *phi = 3;  // set `hi` to 3

int hi declares the variable hi to be an integer . int hi声明变量hi为整数 int *hi declares the variable hi to be a pointer to an integer. int *hi声明变量hi为指向整数的指针

hi stores the integer type value in a particular location, but hi将整数类型值存储在特定位置,但是
*hi stores the address of any int type variable. *hi存储任何int类型变量的地址。

Example : 范例:

int hi = 10;
int *hello = &hi;

The first declares an integer variable, while the second declares a pointer to an integer. 第一个声明一个整数变量,而第二个声明一个指向整数的指针。

Pointers are beyond the scope of a StackOverflow post, but this Wikipedia article is a starting point, and there should be at least a chapter on pointers in whatever book you're using to learn C. 指针不在StackOverflow文章的讨论范围之内,但是这篇Wikipedia文章是一个起点,无论您使用什么学习C的书,都应该至少有一个关于指针的章节。

int hi------ indicates that hi is the integer which allocates 2 bytes for it. int hi ------指示hi是为其分配2个字节的整数。 int *hi------ * indicates the pointer which holds the address of the variable and that variable is an integer. int * hi ------ *表示保存变量地址的指针,并且该变量是整数。 both are different.one indicates the integer and the other indicates the address of the integer. 两者是不同的。一个表示整数,另一个表示整数的地址。

int hi; reserve a place in the memory for an integer variable while int *ptr; 在int * ptr中为整数变量在内存中保留一个位置; reserve a place in the memory for a pointer which contain the memory address of other variable. 在内存中为包含其他变量的内存地址的指针保留一个位置。 you can use the pointers in different ways. 您可以通过不同的方式使用指针。

int *ptr = hi;
int *ptr;
ptr = &hi; 

when you change the value of the ptr you change the address where it is pointing for but if you changed the value after de-referencing the address you are changing the value of the other variable. 当更改ptr的值时,将更改其指向的地址,但是如果在取消引用地址后更改了值,则将更改另一个变量的值。

*ptr = 3;

leads to change the value of hi; 导致改变hi的值;

First - int hi ; 先入为主 ; here you declaring a integer variable named "hi" 在这里,您声明一个名为“ hi”的整数变量

Then - int *hi ; 然后-int * hi ; here "hi" is a pointer which can point to a integer value 这里“ hi”是一个可以指向整数值的指针

Note: int* hi and int *hi are syntactically same 注意: int * hi和int * hi在语法上是相同的

a. 一种。 int i;
b. b。 int *address;
c. C。 address = &i;

In line a an integer variable named i is declared. 在此声明了一个名为i的整数变量。 When this is done the compiler reserves a memory space of size sizeof(int) (it's 4 byte on my computer). 完成此操作后,编译器将保留大小为sizeof(int)的内存空间sizeof(int)在我的计算机上为4字节)。 If you want to refer to this memory space then you have to use pointers. 如果要引用此内存空间,则必须使用指针。

Line b declares a variable named address which has a special property. b行声明了一个名为address的变量,它具有特殊的属性。 This variable doesn't holds an int but it stores the address of a variable that is of type int . 该变量不包含int但存储的是int类型的变量的地址。 Therefore, whatever value address has, it should be interpreted as the address of a variable which is of type int . 因此,无论值address具有什么值,都应将其解释为int类型的变量的地址。 Currently, the variable address doesn't hold any memory address in it as we haven't yet defined which variable's memory address it has to hold. 当前,变量address不包含任何内存地址,因为我们尚未定义它必须保留哪个变量的内存地址。

Line c can be read as "address is equal to the memory address of variable i ". 可以将c行读作“地址等于变量i的内存地址”。 Now, the variable address stores the memory address of the int variable i . 现在,变量地址存储int变量i的内存地址。

int main(){
  int a;
  int &b;
  b=&a;
  a=10;
  return 0;
}

When this code is run using a debugger I see: 当使用调试器运行此代码时,我看到:

a = 10 // the variable's value  
b = 0x7fffffffe2fc // this is the address at which 'a' is stored.  

Pointers are very powerful and you will start to use it more often once you understand it. 指针功能非常强大,一旦您了解了它,便会开始更多地使用它。 Apart from the materials that others suggested for you to read I suggest use a debugger(gdb) and run a few programs with pointers in it and check every variable that you declared in the code. 除了其他人建议您阅读的材料之外,我建议使用调试器(gdb)并运行一些带有指针的程序,并检查您在代码中声明的每个变量。 I understand things better when I have a visual picture of any problem and I think it might as well speed up your understanding of pointers. 当我对任何问题有直观的了解时,我会更好地理解事情,并且我认为这可能会加快您对指针的理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM