Loading presentation...

Present Remotely

Send the link below via email or IM


Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.


Pointers in C and C++ Programming

No description

Sandy Keaton

on 12 March 2013

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Pointers in C and C++ Programming

A Map to Locations and Values http://alumni.cs.ucr.edu/~pdiloren/C++_Pointers/neighbor.htm
Forouzan Savitch, Chapter 2.1; Deitel & Deitel, Chapter 1.16
pw1.netcom.com/~tjensen/ptr/pointers.htm Works Cited As you can see it takes a lot of ‘clues’ in your own memory cells to understand pointers.
We are at the end of this presentation so I hope the ‘pointer concept’ is in your grasp.
There are a lot to remember but the 2 main “points” are to always think of the ‘address of’ when you see ‘&’ and * means ‘value pointed by’. X Marks the Spot Arrays are considered to also be a pointer but some programmers disagree.
When you declare an array the memory locations are permanent unlike a pointer where you move them into other memory cells.
A pointer is a variable and an array is a constant (constant meaning that it never changes) Arrays Remember in the beginning when we said that pointers point to the next consecutive memory cell and that integers hold 2 cells and long integers hold 4 and so on…

Lets say we have ‘int *ptr1;’ and we will give it an address of 10, so if I wrote ‘++ptr1’ the computer/compiler knows that it is an integer pointer and that holds 2 memory cells so the next cell would be 12, it would add 2 cells and if it were a long integer it would add 4 cells and so on…. This is called “pointer arithmetic”. More About Pointers int Jack = 1, Mary = 2; //2 variables, both the size of int
int *ptr1, *ptr2; //we have 2 pointers ptr1 and ptr2 both the size of int
ptr1 = &Jack //pointer 1(ptr1) is pointing to address of Jack
ptr2 = &Mary //pointer 2(ptr2) is pointing to address of Mary
*ptr1 = 10; //changed the value to 100
ptr1 = ptr2;
ptr1 = 20;
printf(”Jack = %d\n", Jack); //this will print out 10
printf(”Mary = %d\n", Mary); //this will print out 20 Pointer Example The reference operator that is used to find the value stored in the house you are pointing at is the asterisk (*), and when you see this preceding a variable it means “value pointed by”.

If Jack wanted to know the value stored in the house Mary was pointing at you would do this:
Int Jack = *Mary; Reference Operator Looking at this a little more closely, lets give Sam an address of 12345 and a value stored inside of his house of 100.

Int Sam = 100; //1rst line
Mary = Sam; //2nd line
Jack = //3rd line - 12345(address)

1rst line tells us that Sam has 100 stored in his house
2nd line tells us that ‘Mary = Sam’ so she will also store 100 in her house
3rd line puts Sam’s address in Jacks house Using the Pointer If we want to find where your variable lives we would use that ‘&’ sign.

This sign precedes the name of your variable.
Its definition is “address of” and in the beginning when you see the ampersand sign(&) its just easier to just say to yourself “address of”.

Lets say in our map to the treasure (or what's stored inside our variable) there are 3 houses we will include. The first is Jacks, second is Mary’s and the third is Sam. Where Your Variable Lives Storing a value in your variable is easy once you decide what you want to store.

Lets say for example you want to store a number in your one of the houses along the way on your hunt. You would just put ‘int Sam = 100;’

You have now stored the number 100 in the size of an integer and your variable name is ‘Sam’.

You should also remember that you can change this value along the way. Storing Values in Variables Variables are stored in consecutive memory cells inside of a computers memory that are a byte long and each with a single address.

Depending on your variable will depend how many bytes your computer will set aside in a memory cell or a block of memory cells.

For an integer it will hold 2 bytes for a long integer it will hold 4 bytes, and for a character it holds 1 byte. Variables cont. Variables in computer programming are usually given names that describe them.
Integer which is shortened to ‘int’ which describes a whole number such as 12345
Character which is shortened to ‘char’ which describes one character like the letter ‘a’.
Float or Double which describes a decimal number like 123.456 and 1234.45678987 respectively. Variables Lets say that we are going on a treasure hunt and
your friend told you that your other friend knows
where the treasure is, and this is how we will find it in
C and C++ programming with pointers.

But before we begin we need to know more about
variables. You should also know that the
computer or compiler that runs your program will
assign the address of your variable at runtime. Def. of Pointers cont. A pointer references a location in memory, and
obtaining the value stored at that location is
known as dereferencing the pointer.

As an example:
A clue in a treasure map could be considered a
pointer to the corresponding clue;
dereferencing such a pointer would be
done by going to that spot with the clue you
just read. Def. of Pointers cont. int array[5] = {10,21,32}; //set of numbers
int *ptr1; //initializing my pointer
ptr1 = array[1]// gets address of the 1 place holder which is the number 21
printf("*ptr1 = %d\n", *ptr1); //prints a 21
p1++; // point to the next element
printf("*ptr1 = %d\n", *ptr1); //prints a 32 Example of an Array An Array is a set of numbers or characters (cannot be both) that has different values.
Example : int a[3] = {1,2,3};(a set of 3 numbers that in memory start with 0,1,2)

The number 1 has a place holder of 0, the number 2 has a place holder of 1, and the number 3 has a placeholder of 2.
So if I referenced ‘int a[1]; what is associated with that place holder is actually the number of 2. Arrays cont. To declare a pointer all you have to do is use the asterisk(*), int *Mary;.

If you want to declare 2 pointers of the same type you can do this - int *Mary, *Jack;

int *Mary, Jack; - only declares one pointer

If Mary is a ‘float’ then only a float pointer can point to it, same goes for ‘char’, ‘double’ and ‘int’ etc…..

Remember to get the value stored in an address when you are using pointers, you have to use the asterisk(*)! How to Declare a Pointer Variables cannot start with a digit
‘int 5buster;’ is illegal
Variables should not start with an underscore_
‘int _buster;’ is illegal
Variables should consist of 31 or fewer characters to ensure portability
Keywords such as ‘int’ and ‘float’ cannot be used as variable names
Variable names should be descriptive of their use
Variable names are case sensitive
‘int Buster;’ is not the same as ‘int buster;’ Rules for Variables Definitions of Pointers

In the C and C++ Programming world a
pointer is a special kind of variable that
holds the address of another pointer.

Simply put, a pointer points to an
address and gives you a value. The
value can either be an address or what is
kept in the address. Int Mary = 100;
Int *Jack;
Int Jack = Initializing a pointer just means to give it a value.

Initializing a variable also means to give it a value.

int Mary = 100; //initializing a variable

Then we will make a pointer:
Int *Jack; //this is our pointer

Now we will point it:
int Jack = &Mary //puts Mary’s address in Jacks house

int Jack = 100; // now Jack and Mary both have a value of 100 Initializing the Pointer In the beginning we said that a pointer points to a value and that there are two values.

The first value is the address and that is considered the lvalue because it is on the left side of the operator (ex. =, <, >, !=, <=, >=).

The second value is what is stored inside the address and that is called the rvalue, it is stored on the right side of the operand.
It is illegal to reverse the values, you cannot put for instance:
‘5 = int Jack;’ it has to be ‘int Jack = 5;’ Values *Mary = 50 50 50 100 12345 100 12345 100 Sam’s house Mary’s house Jack’s house If Jack is pointing to Mary and Mary is pointing to Sam if you change Mary’s value Sam’s value would also change but Jacks would not. Make Sure You Know Who is Pointing to Who Jack = &Sam Mary = Sam 12345 100 12345 100 Jack’s house Mary’s house Sam’s house Using the Pointer cont. Int Sam = 100;
Int Mary = Sam; (100)
Int Jack = (12345) How to Use Pointers in C and C++ Programming int **Jack; //double pointer(2 asterisks)
int *Sam; //another pointer
int Mary = 7; //a regular integer named Mary, and initialized to 7

Sam = &Mary; //puts Mary’s address in Sam’s house
Jack = &Sam; //puts Sam’s address in Jack’s house

printf(”Jack = %d\n", Jack); //Jack is storing Sam’s address, will print out Sams address
printf("&Sam = %d\n", &Sam); // address of Sam(which is the same as above)
printf("*Jack = %d\n", *Jack); //Mary’s address which is stored in Jack’s house
printf("&Mary = %d\n", &Mary); //Mary’s address which is the same as the line above
printf("**Jack = %d\n", **Jack); //Jack is pointing to Sams house, Sam is pointing to Mary’s house so what is the value stored in Mary's house, it is the number 7 (with ** you jump twice instead of once) Pointing a Pointer to a Pointer
Full transcript