0% found this document useful (0 votes)
74 views20 pages

Pointer in C++

Pointer variables store memory addresses rather than values directly. Pointers allow accessing the value of the variable being pointed to indirectly. Pointers must be declared and initialized before use. The & operator returns the address of a variable. The * operator dereferences a pointer to access the value stored at the pointer's address. Pointer arithmetic and logical operators allow manipulating and comparing pointers.

Uploaded by

Champ Mian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views20 pages

Pointer in C++

Pointer variables store memory addresses rather than values directly. Pointers allow accessing the value of the variable being pointed to indirectly. Pointers must be declared and initialized before use. The & operator returns the address of a variable. The * operator dereferences a pointer to access the value stored at the pointer's address. Pointer arithmetic and logical operators allow manipulating and comparing pointers.

Uploaded by

Champ Mian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

Computer Programming

Lecture No. 3
C/C++ Pointers
Pointer Variable Declarations and Initialization

• Why we need Pointers:


– Accessing array elements
– Passing arrays and strings as arguments
– Creating data structures such as linked lists
– Dynamic memory allocation, which can grow at runtime using.
Pointer Variable Declarations and Initialization

The main difference between normal variables and pointers is that,


variables contain a value,
value while pointers contain a memory address.
address
•Memory address:
– We know each memory address contain some value.
– Thus if pointers contain memory addresses, we can get its
value (indirectly).

C
… 7 3 4 …
172 173 174 175 176 177 178 179 180 181
C is a variable Memory
P
P is a Pointer ? … 174 3 4 …
832 833 834 835 836 837 838 839 840 841
Pointers

• A pointer variable must be declared before it can be used.

• Examples of pointer declarations:


int *a;
float *b;
char *c;
• The asterisk,
asterisk when used as above in the declaration,
tells the compiler that the variable is to be a pointer,
and the type of data (the value that we get indirectly)
that the pointer points to.
Referencing
• The unary operator & gives the address of a variable

• The statement
P = &C
& ;

• assigns the address of C to the variable P, and now P


points to C
Referencing

int C;
int *P; /* Declare P as a pointer to int */
C = 7;
P = &C;

C
… 7 3 4 …
172 173 174 175 176 177 178 179 180 181

P
… 174 3 4 …
832 833 834 835 836 837 838 839 840 841
Dereferencing

• The unary operator * is the dereferencing


operator

• Applied on pointers
• Access the value of object the pointer points to
• The statement
*P = 5;
puts in C (the variable pointed by P) the value 5
Dereferencing

cout << *P; /* Prints out ‘7’ */


*P = 177;
P = 177; /* This is unadvisable!! */

C
… 7 3
177 4 …
172 173 174 175 176 177 178 179 180 181

P
… 174 3
177 4 …
832 833 834 835 836 837 838 839 840 841
Null Pointer
• A pointer initialized to a o or Null points to
nothing is called Null pointer
• Zero of Null is the only value which can be
initialized to pointer without casting.
• e.g.
• int *p=0;
or
• int *p;
p=0;
Bits and Bytes

• Memory is divided into bytes, each of which are further


divided into bits
– Each bit can have a value of 0 or 1
– A byte is eight bits
• Can hold any value from 0 to 255
– Memory can be thought of as a sequence of bytes, numbered
starting at 0

0 1 2 3 4 5 6
Storing Variables

• Each variables is stored in some sequence of bytes


– Number of bytes depends on what?
– Number of bytes depends on the data type
– Can two variables share a byte?
– Two variables will never share a byte – a variable uses
all of a byte, or none of it
• Example:
– An int is usually stored as a sequence of four bytes
Using the Address

• This holds true for every language – each variable has to


be stored somewhere
– In C/C++, you can get and use the address
• For any variable x, &x returns the address of x
Pointers & Allocation (1/2)

• After declaring a pointer:


int *ptr1;
ptr1 doesn’t actually point to anything yet. So its
address is NULL.

. We can either:
– make it point to something that already exists,
• ptr1 = &C ptr = NULL

– or
– allocate room in memory for something new that it
will point to… (dynamic memory will discuss later)

Memory
Pointers & Allocation (2/2)

• Pointing to something that already exists:


int *ptr, var1, var2;
var1 = 5;
ptr = &var1;
var2 = *ptr;
• var1 and var2 have room implicitly allocated for
them.

ptr  
? var1 ?
5 var2 ?
5
More C/C++ Pointer Dangers
• Declaring a pointer just allocates space to hold the pointer – it
does not allocate something to be pointed to! Thus the address is
NULL.

• What does the following code do?


• We can’t store anything in the pointer (ptr) unless ptr contains
some address.

void f()
{
int *ptr;
*ptr = 5;
}
Arithmetic on Pointers
• A pointer may be incremented or decremented.

• This means only address in the pointer is incremented or


decremented.

• An integer (can be constant) may be added to or subtracted from


a pointer.

• Pointer variables may be subtracted from one another.

• Pointer variables can be used in comparisons, but usually only in


a comparison to pointer variables or NULL.
Arithmetic on Pointers

• When an integer (n) is added to or subtracted from a


pointer (ptr)
– The new pointer value (ptr) is changed by the ptr address
plus (+) n multiple (*) the (bytes of ptr data type).
– ptr + n * (bytes of ptr data type)
• Example
– int *ptr;
– ptr = 1000;
– ptr = ptr + 2;
– // ptr address is now changed to 1000 + 2*4 (because
integer consumes 4 bytes) New address is now 1008
Arithmetic on Pointers
• Example (2)
– long *ptr;
Address data
– ptr = 1000;
1000
– ptr++; 1000 + 1*4 = 1004
1002 num1
1004
• Example (3)
1006
– float *ptr;
1008
– ptr = 1000;
– ptr+=3; 1000 + 3*4 = 1012

• Example (4)
Memory width is 2 bytes
– int *ptr;
– int num1 = 0;
– ptr = &num1;
– ptr++; 1002 + 4 = 1006
Arithmetic on Pointers
• Example (5) Address data
void main (void) 1000
{
1002 num1 = 93
int *pointer1, *pointer2;
int num1 = 93; 1004
pointer1 = &num1; //address of num1 1006 pointer1 = 1002
pointer2 = pointer1; // pointer1
address is assign to pointer2 1008 pointer2 = 1002

}
Logical operators on Pointers
• We can apply logical operators (<, >, <=, >=, ==, != ) on
pointers.
– But remember pointers can be compared to pointers or
– NULL

• Example (6)
– int *pointer1, *pointer2; // both pointer2 contains NULL
addresses
– int num1 = 93;
– If ( pointer1 == NULL ) // pointer compared to NULL
– pointer1 = &num1;
– pointer2 = &num1;
– If ( pointer1 == pointer2 ) // pointer compared to pointer
• printf(“Both pointers are equal”);

You might also like