thelamp
thelamp

Reputation: 89

Pointer Declaration

I was working on a coding exercise, where the user enters two integers, and the code displays the integers via pointers. When I declared the pointers and then pointed them to values a and b later on in the code, the code didn't compile. However, telling the pointers where to point right off the bat (when they are declared) works. I was wondering why the first way did not work.

Code That Did Not Work

// variables
int a; 
int b; 


// Ask for int a 
cout << "Enter integer a : "; 
cin >> a; 

// Ask for int b
cout << "Enter integer b : "; 
cin >> b; 

// Pointing pointers to a and b; 
*ptrA = &a; 
*ptrB = &b; 

// Print values a and b 
cout << "a: " << *ptrA << endl; 
cout << "b: " << *ptrB << endl; 

Code That Worked

// variables
int a; 
int b; 
int *ptrA = &a; 
int *ptrB = &b; 

// Ask for int a 
cout << "Enter integer a : "; 
cin >> a; 

// Ask for int b
cout << "Enter integer b : "; 
cin >> b; 


// Print values a and b 
cout << "a: " << *ptrA << endl; 
cout << "b: " << *ptrB << endl; 

Upvotes: 0

Views: 112

Answers (2)

Silvio Mayolo
Silvio Mayolo

Reputation: 70277

There's a difference between the two syntaxes that isn't immediately clear.

int *ptrA = &a;

This is declaring a variable called ptrA whose type is int*. Its value is being set to the address of a. This is the behavior you're looking for.

int *ptrA;
...
*ptrA = &a;

This, again, declares a variable called ptrA whose type is int*. Then it makes an assignment. The *ptrA on the second line means to assign the value of the thing ptrA is pointing to. The type of *ptrA is int, since ptrA points to an integer and we're dereferencing it, and &a is an int* since we're taking the address of an integer, so you're trying to assign an int* to an int*. Hence, the problem. To do what you intend to do, you need to assign the pointer, not its value.

int *ptrA;
...
ptrA = &a;

The misunderstanding arises from the fact that there's a * in a confusing place in the declaration. Many people write pointer declarations like so.

int* ptrA = ...;

This makes it more clear that the * is part of the type and not a dereference operator. But this also opens you up to a different pitfall. Can you spot the likely error in this line of code?

int* ptrA, ptrB;

Upvotes: 1

Snigdh
Snigdh

Reputation: 61

Pointers are needed to be declared before using. In first code you didn't​ declared pointersptrA andptrB. To declare pointers, say int pointer, we need to do this

int x=0;
int *iptr;
iptr=&x;

One line code:

int *iptr=&x;

It initialised when declared.

We need to declare a pointer everytime we use it because specific pointer can point to that specific data type. To make sure that pointer points to required data type and protect information loss. Also pointers are also a type variables that stores a memory address, as we need to declare other variables, in that way pointers are also needed to be declared before using.

Treat pointers as you treat other variables keeping in mind that pointers can store only memory address

Upvotes: 1

Related Questions