Basic C/C++ question

Started by
4 comments, last by DevFred 15 years, 4 months ago
Can anyone tell me what the difference is between * and & in C/C++? I thought they were both used for referencing a memory position instead of data. I'm also wondering why you would declare some variable representing a structure as * so you have to access its members with ->. For example:

Address *addr;
Address addr2;

addr->street = "street";
addr->zip = 12345;
addr->state = "PA";
addr->city = "city";

vs

addr.street = "street";
addr.zip = 12345;
addr.state = "PA";
addr.city = "city";

Advertisement
Quote:Original post by homer_3
Can anyone tell me what the difference is between * and & in C/C++? I thought they were both used for referencing a memory position instead of data. I'm also wondering why you would declare some variable representing a structure as * so you have to access its members with ->. For example:


First, '*' :
  • Is the multiplication binary operator in both C and C++.
  • Is the unary "get object pointed to by this pointer" operator in both C and C++.
  • Because of this, it appears in the C (and therefore, C++) type naming scheme to represent pointer types.
  • Has become the universal "get data referenced by this iterator" operator in C++.


Then, '&' :

  • Is the bitwise-and binary operator in both C and C++.
  • Is the unary "create a pointer value pointing to this value" operator in both C and C++.
  • In C++, it has been added to type definitions to represent a reference type.


Pointers are useful in C because they are the only way of achieving reference semantics (that is, letting another function change a value you own) in that language. C++ provides references, and smart pointer classes are also available, which makes naked pointers less useful but still used in certain edge cases.

More about pointers.

Quote:Original post by homer_3
Can anyone tell me what the difference is between * and & in C/C++? I thought they were both used for referencing a memory position instead of data. I'm also wondering why you would declare some variable representing a structure as * so you have to access its members with ->. For example:

*** Source Snippet Removed ***
The first one will crash, because addr1 isn't initialized to point at any object.

As for the differences:

Declare a pointer to an Address struct, called foo, not initialized to anything:
Address* foo;
Declare a pointer to an Address struct, called foo, initialized to point at bar (so foo = the address of bar):
Address* foo = &bar
Declare a reference to an Address struct, called foo, and make it refer to bar
Address& foo = bar;
The main differences between pointers and C++ references are:

Pointers:
- are reseatable.
- can have a "null" state
- can undergo pointer arithmetic.
- have different syntax for when you want to manipulate the pointer and pointee.

Why you use them is harder to answer. The main issue in C++ is that it defaults to value semantics. This means that when you pass things to functions and return them from functions that you copy the values involved. By using references instead of pointers you can omit copies of complex data types which improves performance. You can also make changes to the values in the calling code which is also desirable sometimes.
In simplistic terms, references are pointers that can't be null and can't be accidentally reseated. They are treated as if they were the data itself. Here is some good reading on references.
Quote:Original post by homer_3
I thought they were both used for referencing a memory position instead of data.

When used as unary operators in expressions:
& is for referencing (&x is the address of the lvalue x)
* is for dereferencing (*p is the lvalue at address p)

When used as type constructors inside declarations:
Type& a = b; means that a is an alias for b.
Type* a = &b means that a is a pointer to b.

This topic is closed to new replies.

Advertisement