0% found this document useful (0 votes)
24 views

C 8

This document discusses pointers in C/C++. It explains what pointers are, how to declare and assign values to pointers, pointer arithmetic, pointers and arrays, void pointers, NULL pointers, pointers as function parameters and return values, and more pointer concepts in detail with examples.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

C 8

This document discusses pointers in C/C++. It explains what pointers are, how to declare and assign values to pointers, pointer arithmetic, pointers and arrays, void pointers, NULL pointers, pointers as function parameters and return values, and more pointer concepts in detail with examples.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

C

Pointer
 Pointer is a variable whose value is the address of another variable
 int a=123;
1002 1003 Address
0 0 0 0 0 0 0 0 0 1 1 1 1 0 1 1 Content
a Name given to address
Figure 1: Storing a value using a variable in any memory address

28FF08

28FF08 123

p a
Pointer
 Pointer is a variable whose value is the address of another variable
 &a means---address of the variable a
 Address operator: &
 Address operator operates on variable, array element
 &a (int a;)
 &a[5] (int a[10];)
 &a (int a[10];)
 &123
 &(a+b)
Pointer Declaration
 data_type *pointer_name;
 data_type* pointer_name;
 char *a; //pointer to a character
 int *a; // pointer to an integer [a will point to an integer type variable’s memory address]
 float *a; // pointer to a float
 double *a; // pointer to a double
 int * a;

 Difference between
int a;
int *a; //Assume it is a→
Assigning Value to Pointer
 It is necessary to explicitly mention whom a pointier variable is pointing
 pointer_name=&variable_name;
 p=&a;

28FF08

28FF08 123

p a
Assigning Value to Pointer
 Whose address is pointed by a pointer should be declared before the pointer
Assigning Value to Pointer
 Data types should not conflict
Using the Data of the Pointed Variable

28FF04 28FF00

28FF04 11 33

p a b

28FF04 28FF00

28FF04 11 11

p a b

28FF04 28FF00

28FF04 22 11

p a b

 De-referencing a pointer
Pointer Arithmetic

x=(p1)*(p2);
Pointer and Arrays
 Name of an array is the address of its first element (base address)
a[0] a[1] a[2] a[3] a[4]
11 22 33 44 55
28FEE4 28FEE8 28FEEC 28FEF0 28FEF4

p=&a; p
28FEE4

a[0] a[1] a[2] a[3] a[4]


11 22 33 44 55
28FEE4 28FEE8 28FEEC 28FEF0 28FEF4

p
28FEE8

a[0] a[1] a[2] a[3] a[4]


11 22 33 44 55
28FEE4 28FEE8 28FEEC 28FEF0 28FEF4

p
28FEF4
Pointer and Arrays
 a[i]; //a[2],a[3]
 *(a+i); //*(a+2),*(a+3)

 Why use pointers while using arrays?


Pointer and Arrays
a[0] a[1] a[2]
11 22 33
28FEE8 28FEEC 28FEF0

p
28FEE8

a[0] a[1] a[2] a[0] a[1] a[2] a[0] a[1] a[2] a[0] a[1] a[2]
11 22 33 11 23 33 11 23 33 11 23 34
28FEE8 28FEEC 28FEF0 28FEE8 28FEEC 28FEF0 28FEE8 28FEEC 28FEF0 28FEE8 28FEEC 28FEF0

p p p p
28FEEC 28FEEC 28FEF0 28FEF0
Pointer and Arrays

 Decrement operator could be used similarly


Pointer and 2D Arrays
Array of Pointers
a[0] a[1] a[2] a[3] a[4] p[0] p[1] p[2] p[3] p[4]
 int a[5];
 int *p[5]; 11 22 33 44 55 → → → → →

p[0] p[1] p[2] p[3] p[4]


 int *p[5],a; 31F0AB

 p[4]=&y; → → → → 31F0AB y
Pointer Casting
 (data_type*) pointer_name;
void Pointers
 No associated data type
 Can hold address of any type
 void *pointer_name;

 Casting is required for using a void pointer as the operand of any operator
 Generic pointer
NULL Pointers
 A pointer is usually assigned the address of another variable
 Besides, it can be assigned a NULL value
 A pointer who is assigned a NULL value is known as a NULL pointer
 data_type *pointer_name=NULL;
 int *p=NULL;
 char *p=NULL;
const Pointers
 const data_type *pointer_name=value;
 data_type* const pointer_name=value;
Pointer of Pointer

28FF04 28FF08

28FF04 28FF08 123

p2 p1 a
Pointers and Functions
 Pointer as the parameter of a function
Pointers and Functions
 Pointer as the parameter of a function
Pointers and Functions
 Pointer as the parameter of a function
Pointers and Functions
 Pointer as the return value of a function
 data_type *function_name(parameter_list/argument_list)
{
:
return address;
}
 char *func(){}
 int *func(){}
 float *func(){}
 double *func(){}
Pointers and Functions
 Function pointer

You might also like