0

I have difficulties in determining the types and usage of variables in terms of reference and pointers:

1. | int a = 10;
2. | int& b = a;  // b is a "alias" of a or say, b is a reference of a
3. | int* c = &a; // c is a pointer to the mem location where it stores a
  1. pointer and reference are different, so I read here;

  2. why line 3 a pointer can be assigned with a reference?

    2.1. or &a is not a reference at all?

  3. if &a is not a reference, what makes &b a reference?

    3.1. And why &b == &a is true?

    3.2. And why do we need * to dereference (since pointer is not reference) in order to use it's value?

  4. if &a is a reference, who do it refer to? a?

    4.1. if &a is a reference, does it mean a reference is essentially a pointer (or vise versa)?

    4.2. if not, why a pointer can be assigned with a reference since they are different types?

    4.3. if 4.1 is true, what are the particular situations that we do need both reference and pointer? Isn't a dereferenced pointer a reference to the variable it points to, that is:

 *c == b;   //true, the value of the vaiable
  c == &b;  //true, the address that stores the variable
*&b == a;   //true, dereference a pointer gives the value

and

*c == &a; //illegal, why? Since at line 3 *c just be assigned with &a

I can keep going but I think the reset of my questions will be answered if I can get my head around the above questions.

Simeng Fu
  • 43
  • 6
  • 1
    Dupe(?): [Address-of operator & vs reference operator&](https://stackoverflow.com/q/35594378) – Ch3steR Jul 09 '22 at 05:44
  • 1
    `&a` is not a reference. Also, `&b` isn't a reference either. Refer to a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to know about the syntax and what they mean in different contexts. – Jason Jul 09 '22 at 05:45
  • C does not have references, do you mean to have this tagged as C++ only? – kopecs Jul 09 '22 at 05:45
  • Related: [Meaning of references, address-of, dereference and pointer](https://stackoverflow.com/q/63330857), [What are the differences between a pointer variable and a reference variable?](https://stackoverflow.com/q/57483) – Ch3steR Jul 09 '22 at 05:47
  • 1
    @SimengFu The syntax and their meaning is explained in any [introductory C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and also in numerous SO posts. You seem to be learning C++ using *trial and error* method which is not suited for C++. For learning the fundamentals a book must be used first. Additionally, if possible try to ask one question per SO post. – Jason Jul 09 '22 at 05:52
  • As a rule of thumb I use pointers only if something intentionally can be null I use a pointer otherwise a reference. And if something can be null consider looking at std::unique_ptr first. Naked pointer should only be used in non owning situation is (where you are very sure of the lifetime of the objects pointed to) – Pepijn Kramer Jul 09 '22 at 05:56
  • Thanks all, the articles you provided are great, however, it does not answer my questions while creating more confusion. I am not that bright. I appritiate if some one can point my wrongs and tell me the right directly. It's like you send me to jail and say "google the law, about crime". But I do appritiate that you guys spent the time and collcted so many relative resources for my questions. Thanks a lot. – Simeng Fu Jul 09 '22 at 06:03
  • @Pepijn Kramer Thanks! I will keep that in mind for it! – Simeng Fu Jul 09 '22 at 06:05
  • @Anoop Rana I have read several books but never the one you suggested. I will for sure to read it. However, don't you think that practice is the only way to learn programming? I get this experience from my 25 years commercial programming, not in C++ of course. – Simeng Fu Jul 09 '22 at 06:10
  • @SimengFu Your main issue I think is that `&` is used for **two different reasons**. In an *expression* `&` means *address of* and results in a pointer, so in `int* c = &a;` `&a` is a pointer. But in a *declaration* `&` means reference, so `int& b = a;` `b` is declared as a reference. Of course you need to know the difference between a declaration and an expression. If you don't know the difference between declarations and expressions, then you should try to understand that before you try to understand references and pointers. – john Jul 09 '22 at 06:11
  • @SimengFu *25 years commercial programming*, OK I suppose you do know the difference between an expression and a declaration. What languages do you know? – john Jul 09 '22 at 06:17
  • @john I come from PHP and then JS since nodejs was out. I mainly build network services (not UI's or websites) with JS. I am now in uC area with my work where I need to work with C or C++. Since I am 'formed' with OOP, C++ is the one I am working with. It's not I cannot programme with C++, it's I am uncomfortable to write something that I have doubts. trial error may not the best way to do it, or say the worst way to do it, but it works so far, only at a high cost of extremely low effeciency and unreliability. – Simeng Fu Jul 09 '22 at 06:37
  • @SimengFu You are going to find C++ very different from either of those languages. I know a little JS. I think the main contrasts with JS are that C++ has strong typing, value semantics and no reflection. And it has templates for which there is no equivalent in JS. – john Jul 09 '22 at 06:56
  • @john I noticed that since I was working with Java and Objective-C and Swift (I programmed with these languages but I would not say that I know them). I like strong typing and very keen on distinguishing variable types. I would put the type as part of the variable name and I use "const" a lot. I like C++ (although I know far less than a professional does about it) while I can hardly say the same with Java, but that's just me. – Simeng Fu Jul 09 '22 at 07:16
  • @SimengFu The important thing here is that [C++ is a context sensitive language](https://stackoverflow.com/a/72153563/12002570). So the *"meaning of a construct depends on the context in which it is used*". And the best way to learn C++(IMO) would be by using a [good C++ book](https://stackoverflow.com/a/72153563/12002570). It's good that you're asking questions but **the problem is** that you're using sites like geeksforgeeks to learn C++. Those sites have many wrong articles and so a book should be preferred over them. – Jason Jul 09 '22 at 07:53
  • @Anoop Rana Ah! I get your point! I will purchase that book right away. Thanks! – Simeng Fu Jul 09 '22 at 08:24
  • @SimengFu As you can see [this is a list](https://stackoverflow.com/a/72153563/12002570) of books instead of a single book. That is, in that list you'll find many books from beginner to intermediate to advanced level. Also, most of the books listed there are also available in *pdf* form(which i prefer over hardcopies). If you're wondering which one to choose then you can first read the pdf and then if you like that then you can purchase that particular book. Btw my recommendation for a beginner level book is *C++ Primer 6th edition by Lippman* which is one of the best beginner level C++ book. – Jason Jul 09 '22 at 08:29
  • @Anoop Rana I have got the PDF version of the book, however, it's the 5th edition, the 6th edition is with the title and with a different author. I shall try to finish this book first. I am truely grateful that you have taken the time for me. And to all others who have commented, thank you guys. – Simeng Fu Jul 09 '22 at 08:43

0 Answers0