3

I'm not sure if I can ask basic questions here, but I am starting to learn C++ and do not understand one little thing in pointers syntax.

Here is the sample of my code:

using namespace std;

int randomname(int *x);

int main(){

    int a = 1;

    int *ab;

    ab = &a;

    randomname(&a);

}


int randomname(int *x){

    *x = 9001;

}

My question is about the * symbol. Why in the main function on line ab = &a; I don't need the *, but on line *x = 9001; I need it? I think syntax should be same in both functions, but it isn't. Can someone please explain why?

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • 1
    Basic questions are fine as long as they fit the guidelines, such as showing that you've put effort into it and providing an [MCVE](http://stackoverflow.com/help/mcve) where applicable. In this case, every single C++ book meant to teach the basics of the language will go into depth on when and why you need the asterisk. – chris Jun 09 '14 at 18:30
  • Regardless of the difficulty of your question, you must always research it yourself. You can find this answer easily here on SO or with a quick google search. – Daniel Jun 09 '14 at 18:31
  • 1
    Here's a list of [good C++ books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Everything you are asking should be covered in an introductory one. – juanchopanza Jun 09 '14 at 18:31
  • 4
    This is a trivial question that can easily be answered by any text book/google search – Ed Heal Jun 09 '14 at 18:32
  • Well I tried to search for it. Unfortunately I am not native english speaker so I did not find the answer. ("pointers as function arguments in c++" did not gave me rezults that I was looking for :( ) P.S. I am learnig C++ with videos on youtube, and this question was not covered somehow –  Jun 09 '14 at 18:35
  • 2
    I think it is a fair question from someone who is starting with the language, because the meaning of both symbols changes depending on the context. – Sergey Kalinichenko Jun 09 '14 at 18:40

2 Answers2

3

The meaning of both the asterisk * and the ampersand & changes depending on the context. Their meanings in expressions and in declarations are different:

  • When * is used in a declaration, it designates a pointer
  • When & is used in a declaration, it designates a reference
  • When * is used in an expression, it performs a pointer dereference of its operand
  • When & is used in an expression, it obtains a pointer of its operand

Once you understand these distinctions, you can tell that

  1. Line int *ab is a declaration. Asterisk designates ab as a pointer.
  2. Line ab = &a; has an expression. & takes a's pointer, and assigns it to ab, which has a pointer type specified at the time of its declaration (above).
  3. Line *x = 9001 is also an expression, making the asterisk a dereference operator. You use the asterisk to tell the compiler that the target of the assignment is whatever is pointed to by x, not x itself.
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Thank you, I think I understand it now. Somehow I can only accept your answer only after 1 minute :/ –  Jun 09 '14 at 18:43
1

You need the asterisk when you are declaring a pointer, or when you are dereferencing the pointer. The syntax actually is the same in both functions, but what is being assigned in each of the assignment statements is different.

An int is a value. An int variable is stored in a storage location. A pointer to an int is also a value, but its value refers to a storage location. When you are performing an assignment with a pointer, you could assign the value of the pointer itself (the address of the storage location it refers to), or you could assign a value to the storage location it refers to.

In the case of int *x in the formal parameters of the function, or in the declaration int *ab that defines the ab variable, you need the asterisk because you are declaring a variable or a parameter to be of type "pointer to int".

In the assignment ab = &a you do not need the asterisk because you are assigning the address of variable a to the pointer ab -- you are assigning the value of the pointer itself, not what it points to.

In the assignment *x = 9001, you need the asterisk to get the storage location that the pointer refers to. This is called "dereferencing" the pointer. You are not assigning a value to the pointer itself, but instead you are assigning a value to the storage location to which the pointer refers, which is the storage allocated for variable a in function main.

This article by Eric Lippert may be of considerable help to you:

What are the fundamental rules of pointers?

David Conrad
  • 15,432
  • 2
  • 42
  • 54