Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we use "&" before the integer variable when initializing a pointer that points to the integer?

Tags:

c

syntax

pointers

I am currently learning pointers in C. I feel confused about the initialization of a pointer. I might be asking silly questions, but I just want to make sure I understand and learn things right.

So, to initialize a pointer:

int a = 3;
int *pointer = &a;
*pointer = 10;

OR

int a = 3;
int *pointer;
pointer = &a;
*pointer = 10;

So far, I knew that " * " declares a pointer type.

*pointer is the value of whatever value in the address that the pointer points to.

& is the memory address of something.

I can understand 'pointer = &a' in the second case.

But, why do we set *pointer = &a in the first case above since *pointer represents the value in that address?

Why do we make the value in that pointer equals to the address of the variable in the first case when initializing the pointer?

like image 402
Hill Tezk Avatar asked Oct 29 '17 08:10

Hill Tezk


People also ask

Why shouldou use WWW for your website?

ou should use www because today you have a small web site, and tomorrow you want a big web site. Really big.

What is the meaning of the word why?

Why is a wh -word. We use why to talk about reasons and explanations. Why did he leave home when he was 16? Why didn’t you tell Gemma? Why is the Earth round? He asked me why I wanted to leave the job. I wonder why he told nobody he was getting married.

How do you use why in a sentence?

We use why to talk about reasons and explanations. Why did he leave home when he was 16? Why didn’t you tell Gemma? Why is the Earth round? He asked me why I wanted to leave the job. I wonder why he told nobody he was getting married. I’m going home now. Why? When we reply to a negative statement, we usually say why not ?: I don’t like it here.

When to use why ever or why on Earth?

We can use why ever or why on earth to add emphasis and to show shock or surprise. We usually stress ever and earth: Beth has decided to go on holiday by herself this year. Why ever would anyone want to go on holiday alone? Why on earth has Julie bought me this expensive present?


Video Answer


3 Answers

Confusingly, the asterisk in *pointer = 10 and the asterisk in int *pointer = &a mean two different things.

  1. *pointer = 10 Dereference the variable pointer and assign the value 10 to the result of the dereference operation.
  2. int *pointer = &a Declare the variable pointer to be of type int *, and initialise it with the value &a. No dereference takes place here. The asterisk is here to remind you that when dereferencing pointer you will get an int. In other words, this declares pointer such that *pointer is int.
like image 142
n. 1.8e9-where's-my-share m. Avatar answered Nov 15 '22 08:11

n. 1.8e9-where's-my-share m.


This:

int *pointer = &a;

is actually a shorthand, which is equivalent to this:

int *pointer;
pointer = &a;
like image 21
gsamaras Avatar answered Nov 15 '22 07:11

gsamaras


your question can be answered in two ways.

  1. when you declare a pointer of type, say int, you do this "int *pointer" so this means you are declaring a pointer of type integer, but when you do this "int *pointer = a", this means you are initializing the pointer variable(pointer) by making it point to the address of a.

note that:- "int *pointer = a" this does not means you are dereferencing the pointer variable, this means you are initializing the pointer variable by making it point to "&a"

  1. int *pointer = &a;

    and

    int *pointer; pointer = &a;

means the same thing int *pointer = &a; this can be considered as the shortcut for writing this

int *pointer;

pointer = &a;

like image 31
aditya rawat Avatar answered Nov 15 '22 08:11

aditya rawat