Pointer in c
-
Upload
imamul-kadir -
Category
Engineering
-
view
87 -
download
1
Transcript of Pointer in c
Name: Imamul KadirID: 162-15-7775
Course Code: CSE122Course Teacher: Moushumi Zaman Bonny
Daffodil International UniversityDepartment of CSE
POINTER
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.
address
content
address
&*pointer
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.
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.
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
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.
An Example of pointer
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.
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.
An example of pointer of pointers
Given Task
THANK YOU!