-7

I just have a basic idea about pointers. What is the difference between int *v; and int **v; and is there any difference between int* v; and int *v; Please do let me know.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • 4
    You should really read an introductory C tutorial. The content of your question belies the statement "I just have a basic idea about pointers". – Mad Physicist Aug 01 '16 at 18:06
  • 2
    There's no difference between `int* a`, `int * a` and `int *a`. Then, when dereferencing a pointer, you may end up with another pointer, which you'll need to dereference in order to get the value it's pointing to (if any). This process may be repeated again and again, resulting in some nightmare like `*****a;` or `*********b`. – ForceBru Aug 01 '16 at 18:10
  • Possible duplicate of [What makes more sense - char\* string or char \*string?](http://stackoverflow.com/questions/558474/what-makes-more-sense-char-string-or-char-string) – Cloud Aug 08 '16 at 15:45

1 Answers1

12

The * in a variable declaration does not mean the same thing as * after the variable has been declared. In a declaration, the * states that the variable being declared is a pointer to a value of the type you are declaring. In your example,

int *v;

is a declaration of a pointer to an int value, while

int **v;

is a declaration of a pointer to a pointer to an int value.

However, later in your code after declaration, you could then call

*v

to dereference that pointer, and get the value that is stored at the memory location that the pointer points to.

There is no difference between int* v and int *v in a declaration, though it is more clear to use int *v when declaring multiple variables in the same line. In

int* v, u

v is a pointer to an int while u is just an int, which could be unclear from that declaration, while writing

int *v, u

ends up being slightly more clear about your intention (making v an int pointer and u just an int).

callyalater
  • 3,102
  • 8
  • 20
  • 27
ingleback
  • 306
  • 1
  • 8