Pointer Help - Confusion on usage

Hi everyone,
I have a few questions about pointers and how they are used and when passing either their content or address to a function.

So I guess the first question is - when creating a new pointer i.e..
1
2
3
int *Ptr;
// OR This
int *Ptr = 0;

No memory address is actually assigned yet? Correct? So to use a Pointer correctly I would always need to initialize it with another variable like this? So would that mean a non-pointer variable will always be required to use a pointer? That is so a memory location can then be used with the pointer, since just creating the pointer doesn't provide it one.
1
2
3
4
int A;
int *Ptr1 = A;   // Valid

int *Ptr2 = 123; // Not Valid? 



The next part where I'm confused is when using a pointer variable with a function that takes a pointer. How do you know what variable + pointer to use with the function? A, *A, or &A? Is it correct in saying if a function takes an address operator then I should always use a &. Or a dereference operator * if the functions requires that?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int A;
int *Ptr = A;

int main(){
square1(A);
//OR
square1(&A);
//OR
square1(*A);

square2(A);
//OR
square2(&A);
//OR
square2(*A);
}

void square1(int & rNumber) { 
   cout <<  "In square(): " << &rNumber << endl;  // 0x22ff1c my memory location
}

void square2(int * rNumber) { 
   cout <<  "In square(): " << *rNumber << endl;  
}
int *Ptr; This is an uninitialized pointer. You need to assign value to it before you can use it.

int *Ptr = 0; This is a null pointer. It doesn't point to anything yet but at least you can check this by comparing it to 0 or nullptr.

Before you dereference the pointer (i.e. access what the pointer points to), using the * operator, you need to make sure that it is pointing to a valid object. To get a pointer to an object you use the address-of operator &.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int* ptr1;
if (ptr1 == nullptr) // This is not allowed because ptr1 has not been initialized
{
	// ...
}

int* ptr2 = nullptr;
if (ptr2 == nullptr) // OK
{
	// ptr2 does not point to anything
}

cout << *ptr2; // This is not allowed because ptr2 doesn't point to anything yet

int a;
int* ptr3 = &a; // The address-of operator is used to get a pointer to a

*ptr3 = 2; // OK
cout << a; // OK, prints 2 
int& rNumber This is not a pointer and has nothing to do with the address-of operator. rNumber is a reference to an int.

A reference always refers to an object (it can't be null), so it must be initialized, and it is not possible to change what the reference is referring to. You can think of a reference as an alternative name for an object. You use it just like a regular variable, no need to use a dereference operator or anything.

1
2
3
4
int a;
int& ref = a;
ref = 2;
cout << a; // prints 2  

Everything that you can do with a reference could also be accomplished using pointers, but reference are often preferred over pointers because they make the code cleaner (less use of operators) and are less error-prone.

square1(A) This is the correct way of passing A to the square1 function. This function prints the memory address of A. If you rather want to it to print the value of A you should remove & in front of the reference.

 
cout <<  "In square(): " << rNumber << endl;


square2(&A); This is the correct way of passing a pointer to A to the square2 function, because &A gives you a pointer that points to A.
Last edited on
Topic archived. No new replies allowed.