15
Name: Imamul Kadir ID: 162-15-7775 Course Code: CSE122 Course Teacher: Moushumi Zaman Bonny Daffodil International University Department of CSE

Pointer in c

Embed Size (px)

Citation preview

Page 1: Pointer in c

Name: Imamul KadirID: 162-15-7775

Course Code: CSE122Course Teacher: Moushumi Zaman Bonny

Daffodil International UniversityDepartment of CSE

Page 2: Pointer in c

POINTER

Page 3: Pointer in c

What is a pointer?

Pointer is a kind of variable that can hold the address of a variable. The sign ‘&’ used to refer somethings address and ‘*’ used to refer the value of a pointer variable. ‘%p’, ‘%x’, ‘%X’, ‘%u’ are the format specifier of pointer.

Page 4: Pointer in c

address

content

address

&*pointer

Page 5: Pointer in c

Declaring a pointer variableGeneral syntax of pointer declaration is,data-type *pointer_name;

Data type of pointer must be same as the variable, which the pointer is pointing. void type pointer works with all data types, but isn't used often.

Example DescriptionInt* p p is a pointer to an integer.

Int** p p is a pointer to a pointer to an integer.

Int*[] p p is a single-dimensional array of pointers to integers.

char* p p is a pointer to a char.

void* p p is a pointer to an unknown type.

Page 6: Pointer in c

Initialization of Pointer variable

Pointer Initialization is the process of assigning address of a variable to pointer variable. Pointer variable contains address of variable of same data type. In C language address operator ‘&’ is used to determine the address of a variable. The ‘&’ returns the address of the variable associated with it.

Page 7: Pointer in c

Pointer variable always points to same type of data.

float a; int *ptr; ptr = &a; //ERROR, type mismatch

int a = 10 ; int *ptr ; //pointer declaration ptr = &a ; //pointer initialization or, int *ptr = &a ; //initialization and declaration together

Page 8: Pointer in c

Dereferencing of Pointer

Once a pointer has been assigned the address of a variable. To access the value of variable, pointer is dereferenced, using the indirection operator ‘*’.

int a,*p; a = 10; p = &a; printf("%d",*p); //this will print the value of a. printf("%d",*&a); //this will also print the value of a. printf("%u",&a); //this will print the address of a. printf("%u",p); //this will also print the address of a. printf("%u",&p); //this will also print the address of p.

Page 9: Pointer in c

An Example of pointer

Page 10: Pointer in c

Pointer of pointers

A pointer to a pointer is a chain of pointers. Normally, a pointer contains the address of a variable. A variable that is a pointer to a pointer must be declared as such. This is done by placing an additional asterisk in front of its name. For example, an ‘int’ type pointer to pointer - **p1, ***p2, ****p3 etc. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, which points to the location that contains the actual value as shown below.

Page 11: Pointer in c

Pointer Pointer Variable

Address Address Value

When we define a pointer to a pointer, the first pointer contains the address of the second pointer, which points to the location that contains the actual value as shown below.

Page 12: Pointer in c

An example of pointer of pointers

Page 13: Pointer in c

Given Task

Page 14: Pointer in c
Page 15: Pointer in c

THANK YOU!