element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Code Exchange
  • Technologies
  • More
Code Exchange
Blog C++ Tutorial - Pointers
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Code Exchange to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: oneleggedredcow
  • Date Created: 13 Mar 2013 1:25 AM Date Created
  • Views 1079 views
  • Likes 0 likes
  • Comments 0 comments
  • code_exchange
  • Code Exchange
  • programming
  • raspberry_pi
  • code
  • rpi
  • c++
  • learnc++
Related
Recommended

C++ Tutorial - Pointers

oneleggedredcow
oneleggedredcow
13 Mar 2013

Introduction

In the previous tutorial, we discussed classes.  Classes are a great way to group related variables and methods together.  Also, they make for a logical division of code into multiple files, so that you don’t have thousands of lines of code in one file. At the end of the tutorial, I alluded to a better way to pass a variable into a method, modify it and return the modified value.  This is going to lead us to the topic of pointers, which is a very powerful tool and an essential one if you want to master C++.

Code

Pointers are basically an address in memory.  (They are called pointers because they point to where your data is located in memory.) So, how do we get the address of a variable?  Here’s how:

 

int num = 5;

cout << &num << endl;


We declare a variable and then we use the & symbol before the variable to get the address of the variable.  This works with any data type, even if the variable is a struct or a class.

 

So, a pointer is a way to store that address:

 

int* ptr = &num;

cout << ptr << endl;


So now the ptr variable stores the location of the num variable.  The value printed to the screen from both code snippets is the same. When declaring a pointer, you need to tell the compiler what data type it is pointing to.  In this case, we are pointing to an integer.  The star between the int and the variable name says that you want a pointer to an integer (instead of storing the integer itself).  One interesting thing to note is that since the compiler ignores whitespace, the following are exactly the same:

 

int *ptr1 = &num;

int* ptr2 = &num;

int*ptr3 = &num;


Now, if you want to get the value that the pointer is pointing to, you do this:

 

int val = *ptr;

cout << val << endl;


The asterisk before the pointer variable tells the computer that you want the value that the pointer is pointing to.  Therefore, the & and the * operators are inverses of each other.

 

Now, let’s take a step back and diagram what is happening in memory:

 

Variable Name

Address

Value

<other_stuff>

<other_stuff>

<other_stuff>

ptr

001BFA7C

001BFA88

<other_stuff>

<other_stuff>

<other_stuff>

num

001BFA88

5

<other_stuff>

<other_stuff>

<other_stuff>

 

So, the ptr variable stores the location of the num variable as its value, and since the ptr variable is something that the computer has to store, that means that it has a location in memory as well.  So, wait does this mean that you can do something like this?

 

int** ptr2 = &ptr;


You sure can!  This means that ptr2 is a pointer, which has a value in memory is the address of a pointer.  So, in our example above the value of ptr2 would be 001BFA7C. Furthermore, you can get back the original value of num by doing something like this:

 

cout << **ptr2 << endl;


This is some extremely powerful stuff and it can get pretty confusing trying to keep what points to what straight.

 

One use for pointers is to pass a value to a function so that it can be modified.  Take this code snippet for example:

 

void fun(int num)

{

num = 7;

}


 

int main()

{

int num = 5;

fun(num);

 

cout << num << endl;

 

return 0;

}


What value do you think is printed out?  The answer is 5.  The reason is that when you call a function, by default it copies the values that you pass in and the function works on the copies.  So, in this case, the copy’s value is changed to 7.  Well, this doesn’t change the value of num in main, so it remains 5 the whole time.  One way to demonstrate this is to print out the location of the variables in memory, using the & operator:

 

void fun(int num)

{

num = 7;

cout << &num << endl;

}


 

int main()

{

int num = 5;

cout << &num << endl;

fun(num);

 

cout << num << endl;

 

return 0;

}


If you run this, then you will clearly see that the num in fun and the num in main point to two different locations in memory.  So, how can we modify this so that they point to the same location in memory?  How about something like this:

 

void fun(int* ptr)

{

*ptr = 7;

}


 

int main()

{

int num = 5;

int* ptr = &num;

fun(ptr);

 

cout << num << endl;

 

return 0;

}


Now we pass in a pointer, which points to the variable num.  The pointer is copied (much like the int was copied), but this really doesn’t matter to us.  We don’t care about the location of the pointer in memory, all we care about is what it points to (which is num).  So, we can use the * operator to get at the value of num and change it.  Here’s what the computer’s memory looks like:

 

Variable Name

Address

Value

<other_stuff>

<other_stuff>

<other_stuff>

num

0024F894

5

<other_stuff>

<other_stuff>

<other_stuff>

ptr (in main)

0024F974

0024F894

<other_stuff>

<other_stuff>

<other_stuff>

ptr (in fun)

0024F980

0024F894

<other_stuff>

<other_stuff>

<other_stuff>

 

At first glance, the code above might seem a bit weird because we are using the * operator on the left hand side of the equals sign.  This is a nice feature that lets us set the value the pointer is pointing to.

 

Since this is a fairy common operation, there is a short hand version of it that looks something like this:

 

void fun(int& num)

{

num = 7;

}


 

int main()

{

int num = 5;

fun(num);

 

cout << num << endl;

 

return 0;

}


This tells the compiler that you do not want it to copy the value that you are passing in, that you want to manipulate the value directly.  If you print out the address of num within fun and main like we did before, you will see that they are the same location in memory.

 

That’s the trick that we could have used in the previous tutorial so that we didn’t have to pass the Fruit class back as the return parameter.

Summary

In this tutorial, we started to learn about pointers and how they store the location of another variable in memory.

 

In the next tutorial, we will build upon this knowledge by dynamically allocating memory for storing arrays.  If you think back to the tutorial on arrays, we knew the size of the array when we were writing the program.  This is usually not the case, so in the next tutorial, we will talk about how to make an array of an arbitrary size.

 

If you have any questions or comments about what was covered here, post them to the comments.  I watch them closely and will respond and try to help you out.

Tutorial Index

01 - Hello Raspberry Pi

02 - Reading User Input

03 - If Statement

04 - For Loops

05 - While Loops

06 - Functions

07 - Data Types

08 - Arrays

09 - Structures

10 - Classes

11 - Pointers

12 - Dynamic Arrays

13 - Linked List

14 - Linked List Operations

15 - STL List/Vector

16 - Templates

17 - Inheritance

18 - File I/O

19 - Strings

20 - Constants

21 - Hash Tables

  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube