Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C - initialization of pointers, asterisk position [duplicate]

Tags:

What is the most proper way to place asterisk? Why?

1)    type*     var;
2)    type      *var;
like image 325
psihodelia Avatar asked Nov 17 '10 09:11

psihodelia


People also ask

Where should the asterisk go in a pointer?

When declaring a pointer type, place the asterisk next to the type name. Although you generally should not declare multiple variables on a single line, if you do, the asterisk has to be included with each variable.

What does * and& indicate in pointer?

An int* is a pointer to an int , so int*& must be a reference to a pointer to an int . Similarly, int** is a pointer to a pointer to an int , so int**& must be a reference to a pointer to a pointer to an int .

What is initialization of pointer in C?

The initializer is an = (equal sign) followed by the expression that represents the address that the pointer is to contain. The following example defines the variables time and speed as having type double and amount as having type pointer to a double .

What is double asterisk in pointer?

It means that aPointer points to a char pointer.


1 Answers

It does not matter as far as you are declaring only one pointer. It is usually writen like in the second example (in the code I usually read/write) but for the compiler it's the same.

The trouble can come out if you are declaring more than one pointer. For example, this is not declaring two pointer, instead it declares one pointer and one var of type type.

type* var1, var2;

You need to do instead:

type* var1, *var2;

I prefer to use the * by the var always.

like image 159
SubniC Avatar answered Oct 30 '22 16:10

SubniC