Pointer in c
-
Upload
imamul-kadir -
Category
Engineering
-
view
83 -
download
1
Transcript of Pointer in c
![Page 1: Pointer in c](https://reader034.fdocument.pub/reader034/viewer/2022042520/58edc4461a28ab8f1c8b46e7/html5/thumbnails/1.jpg)
Name: Imamul KadirID: 162-15-7775
Course Code: CSE122Course Teacher: Moushumi Zaman Bonny
Daffodil International UniversityDepartment of CSE
![Page 2: Pointer in c](https://reader034.fdocument.pub/reader034/viewer/2022042520/58edc4461a28ab8f1c8b46e7/html5/thumbnails/2.jpg)
POINTER
![Page 3: Pointer in c](https://reader034.fdocument.pub/reader034/viewer/2022042520/58edc4461a28ab8f1c8b46e7/html5/thumbnails/3.jpg)
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](https://reader034.fdocument.pub/reader034/viewer/2022042520/58edc4461a28ab8f1c8b46e7/html5/thumbnails/4.jpg)
address
content
address
&*pointer
![Page 5: Pointer in c](https://reader034.fdocument.pub/reader034/viewer/2022042520/58edc4461a28ab8f1c8b46e7/html5/thumbnails/5.jpg)
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](https://reader034.fdocument.pub/reader034/viewer/2022042520/58edc4461a28ab8f1c8b46e7/html5/thumbnails/6.jpg)
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](https://reader034.fdocument.pub/reader034/viewer/2022042520/58edc4461a28ab8f1c8b46e7/html5/thumbnails/7.jpg)
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](https://reader034.fdocument.pub/reader034/viewer/2022042520/58edc4461a28ab8f1c8b46e7/html5/thumbnails/8.jpg)
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](https://reader034.fdocument.pub/reader034/viewer/2022042520/58edc4461a28ab8f1c8b46e7/html5/thumbnails/9.jpg)
An Example of pointer
![Page 10: Pointer in c](https://reader034.fdocument.pub/reader034/viewer/2022042520/58edc4461a28ab8f1c8b46e7/html5/thumbnails/10.jpg)
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](https://reader034.fdocument.pub/reader034/viewer/2022042520/58edc4461a28ab8f1c8b46e7/html5/thumbnails/11.jpg)
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](https://reader034.fdocument.pub/reader034/viewer/2022042520/58edc4461a28ab8f1c8b46e7/html5/thumbnails/12.jpg)
An example of pointer of pointers
![Page 13: Pointer in c](https://reader034.fdocument.pub/reader034/viewer/2022042520/58edc4461a28ab8f1c8b46e7/html5/thumbnails/13.jpg)
Given Task
![Page 14: Pointer in c](https://reader034.fdocument.pub/reader034/viewer/2022042520/58edc4461a28ab8f1c8b46e7/html5/thumbnails/14.jpg)
![Page 15: Pointer in c](https://reader034.fdocument.pub/reader034/viewer/2022042520/58edc4461a28ab8f1c8b46e7/html5/thumbnails/15.jpg)
THANK YOU!