Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
logo
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

Pointers

A pointer is a special kind of variable that stores the address in memory of another variable and can be used to manipulate that variable.

In general, whenever a variable is used in C++, it must exist somewhere in the host computer’s memory and pointers can store the location of a particular variable.

Pointers associate two pieces of information:

  • The memory address, which is the "value" of the pointer itself.
  • The data type of the variable pointed to, which is the kind of variable located at that address.

Declaring pointers

Declaring a pointer is similar to declaring a regular variable, although the name is preceded by an asterisk:

int *ptr;
void *vp;

In the example above

  • ptr is a pointer to an integer.
  • vp is a void pointer, which does not require a specific data type.

Assigning Addresses to Pointers

int* ptr, var;
var = 5;
// assign address of var to ptr pointer
ptr = &var;

Here, 5 is assigned to the variable var. And, the address of var is assigned to the ptr pointer with the code ptr = &var.

Dereferencing Pointers

Pointers can be dereferenced to access the value of the variable at the pointer’s address​.

void f(int* p)
{
// The code "*p" takes the value of the data at location stored in p
int n = *p;
}

Note: Unlike references, pointers are not guaranteed to be initialized. As such, they should only be used when they are known to point to an existing object.

Malloc() and Free()

The malloc() function in C++ allocates a block of uninitialized memory and returns a void pointer to the first byte of the allocated memory block if the allocation succeeds.

If the size is zero, the value returned depends on the implementation of the library. It may or may not be a null pointer.

void* malloc(size_t size);

The free() function in C++ deallocates a block of memory previously allocated, making it available for further allocations.

The free() function does not change the value of the pointer, that is it still points to the same memory location.

void free(void *ptr);

Example

int main()
{
int *ptr;
ptr = (int*) malloc(5*sizeof(int));

if(!ptr){
cout << "Memory Allocation Failed";
return 0;
}
// Initialize values
for (int i=0; i<5; i++){
ptr[i] = i+i;
}

// Initialized values"
for (int i=0; i<5; i++){
/* ptr[i] and *(ptr+i) can be used interchangeably */
cout << *(ptr+i) << " ";
}
cout<<endl;
// output: 0 2 4 6 8

free(ptr);
// If we again prints the value it will print some garbage value

for (int i=0; i<5; i++){
cout << *(ptr+i) << " ";
}
// This will not give any error but prints some garbage value
return 0;
}

Common mistakes when working with pointers

Suppose, we want a pointer varPoint to point to the address of var. Then,

int var, *varPoint;

// Wrong!
// varPoint is an address but var is not
varPoint = var;

// Wrong!
// &var is an address
// *varPoint is the value stored in &var
*varPoint = &var;

// Correct!
// varPoint is an address and so is &var
varPoint = &var;

// Correct!
// both *varPoint and var are values
*varPoint = var;

Try the following example in the editor below.

You are given a function solve which has the following parameters:

  • int *A: an integer
  • int *B: an integer

The function is declared with a void return type, so there is no value to return.
Modify the values in memory so that A contains their sum and B contains their absolute difference.

Start solving Pointers on Interview Code Editor
Hints
  • Complete Solution

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College you graduated from
College/University Name
Job Title
Job Title
Engineering Leadership
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer
Security Leadership
Database Administrator
Data Analyst
Data Engineer
Non Coder
Other
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test