开发者

Simple C question regarding *

开发者 https://www.devze.com 2023-02-03 16:53 出处:网络
I was wondering what is the difference between these two lines of code? int hi; int *hi; In the C programming language?

I was wondering what is the difference between these two lines of code?

int hi;
int *hi;

In the C programming language?

开发者_开发百科

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;

reserves the space for a pointer to an int in memory, each time hi is used, the pointer is either read or written. 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. 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 declares the variable hi to be a pointer to an integer.


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.


hi stores the integer type value in a particular location, but
*hi stores the address of any int type variable.

Example :

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


int hi------ indicates that hi is the integer which allocates 2 bytes for it. int *hi------ * indicates the pointer which holds the address of the variable and that variable is an integer. 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; 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 = 3;

leads to change the value of hi;


a. int i;
b. int *address;
c. address = &i;

In line a an integer variable named i is declared. When this is done the compiler reserves a memory space of size sizeof(int) (it's 4 byte on my computer). 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. This variable doesn't holds an int but it stores the address of a variable that is of type int. Therefore, whatever value address has, it should be interpreted as the address of a variable which is of type 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.

Line c can be read as "address is equal to the memory address of variable i". Now, the variable address stores the memory address of the int variable 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. 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.


First -int hi; here you declaring a integer variable named "hi"

Then -int *hi; here "hi" is a pointer which can point to a integer value

Note:int* hi and int *hi are syntactically same

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号