Pointers in c - Mohammad Salman

MohammadSalman129 1,033 views 21 slides Jun 22, 2020
Slide 1
Slide 1 of 21
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21

About This Presentation

This Presentation gives you all knowledge about #CPointers, #PointersInC.
The given slides are as follows:
1. Introduction of Pointers.
2. Systems to create a pointer in C.
3. Valid Pointer Examples in C.
4. Graphical representation of address assigning in c using pointer.
5. 1st Sample program of p...


Slide Content

Pointers In C? POINTER  is a variable that stores address of another variable. A Pointer in C is used to allocate memory dynamically i.e. at run time. The pointer variable might be belonging to any of the data type such as int, float, char, double, short etc. The purpose of pointer is to save memory space and achieve faster execution time.

Pointer Syntax data_type *var_name; Example : int *p;  char *p; Where, * is used to denote that “p” is pointer variable and not a normal variable. KEY POINTS TO REMEMBER ABOUT POINTERS IN C: 1. Normal variable stores the value whereas pointer variable stores the address of the variable. 2. The content of the C pointer always be a whole number i.e. address. 3. Always C pointer is initialized to null, i.e. int *p = null. 4. The value of null pointer is 0.

Let's see some valid pointer declarations int *x; /* pointer to an integer */ int *xyz, dafar; /* xyz is a pointer to type integer and dafar is an integer variable */ double *ptr2; /* pointer to a double */ float *ptr3; /* pointer to a float */ char *ch1 ; /* pointer to a character */ float *ptr, nalayak; /*ptr is a pointer to type float and nalayak is an ordinary float variable */

Assigning addresses to Pointers Let's take an example. Int *pc, c; c = 5; pc = &c; Here, 5 is assigned to the c variable. And, the address of c is assigned to the pc pointer. 5 Variable c Value Address 2008 2008 Pointer Variable pc Value Address 5060 NOTE: By the way, * is called the dereference operator (when working with pointers). It operates on a pointer and gives the value stored in that pointer.

A Simple program for pointer: #include <stdio.h> void main() { int a=10; // variable declaration int *p; // pointer variable declaration p = &a; // store address of variable ‘a’ in pointer ‘p’ printf(“Address stored in a variable p is: %x\n”, p); // accessing the address printf(“Value stored in a variable p is : %d\n”, *p); // accessing the value getch(); } Output : Address stored in a variable p is: 60ff08 Value stored in a variable p is: 10

Types of a pointer Null pointer void pointer wild pointer

Null pointer We can create a null pointer by assigning null value during the pointer declaration. This method is useful when you do not have any address assigned to the pointer. A null pointer always contains value 0. #include <stdio.h> void main() { int *p = NULL; //null pointer printf(“The value inside variable p is:%x”,p); } Output: The value inside variable p is : 0

void pointer In C programming, a void pointer is also called as a generic pointer. It does not have any standard data type. A void pointer is created by using the keyword void. It can be used to store an address of any variable . #include <stdio.h> void main() { void *p = NULL; //void pointer printf("The size of pointer is:%d",sizeof(p)); getch(); } Output: The size of pointer is : 4 or 8 (bytes) ( depending on system you have)

Wild pointer A pointer is said to be a wild pointer if it is not being initialized to anything. These types of pointers are not efficient because they may point to some unknown memory location which may cause problems in our program and it may lead to crashing of the program. One should always be careful while working with wild pointers. #include <stdio.h> void main() { int *p; //wild pointer printf("\n%d",*p); getch(); } Output: Output would be unexpected…………….

Let's take an example. int *pc, c; c = 5; pc = &c; c = 1; printf("%d", c); // Output: 1 printf("%d", *pc); // Output: 1 Changing Value Pointed by Pointers Let’s take an example. int *pc, c; c = 5; pc = &c; *pc = 1; printf("%d", *pc); // Output: 1 printf("%d", c); // Output: 1

#include <stdio.h> int main() { int* pc, c; c = 22; printf("Address of c: %p\n", &c); printf("Value of c: %d\n\n", c); // 22 pc = &c; printf("Address of pointer pc: %p\n", pc); printf("Content of pointer pc: %d\n\n", *pc); // 22 c = 11; printf("Address of pointer pc: %p\n", pc); printf("Content of pointer pc: %d\n\n", *pc); // 11 *pc = 2; printf("Address of c: %p\n", &c); printf("Value of c: %d\n\n", c); // 2 getch(); } Output Address of c: 2686784 Value of c: 22 Address of pointer pc: 2686784 Content of pointer pc: 22 Address of pointer pc: 2686784 Content of pointer pc: 11 Address of c: 2686784 Value of c: 2

Difference between %u, %x, %X and %p %u prints the value as an unsigned decimal %x prints the value as a unsigned hex value - using lower case a, b, c, e, d, f %X prints the value as unsigned hex value using upper case A, B, C, D, E, F. %p is the proper format string for pointer printing.

Advantages of Pointers Pointers are useful for accessing memory locations. Pointers provide an efficient way for accessing the elements of an array structure. Pointers are used for dynamic memory allocation as well as deallocation. Pointers are used to form complex data structures such as linked list, graph, tree, etc.

Disadvantages of Pointers Pointers are a little complex to understand. If an incorrect value is provided to a pointer, it may cause memory corruption. Pointers are also responsible for memory leakage. Programmers find it very difficult to work with the pointers; therefore it is programmer's responsibility to manipulate a pointer carefully.

Pointers with Array we access the array elements using its index, but this method can be eliminated by using pointers. Pointers make it easy to access each array element. #include <stdio.h> void main() { int a[5]={1,2,3,4,5}; //array initialization int *p; //pointer declaration /*the ptr points to the first element of the array*/ p=a; /*We can also type simply ptr==&a[0] */ printf("Printing the array elements using pointer\n"); for(int i=0;i<5;i++) //loop for traversing array elements { printf("\n%x",*p); //printing array elements p++; //incrementing to the next element, you can also write p=p+1 } getch(); } The Output is: 1 2 3 4 5

Let’s take another example of “ Pointers with Array ”. #include <stdio.h> void main() { int x[5] = {1, 2, 3, 4, 5}; int* ptr; // ptr is assigned the address of the third element ptr = &x[2]; printf("*ptr = %d \n", *ptr); // 3 printf("*(ptr+1) = %d \n", *(ptr+1)); // 4 printf("*(ptr-1) = %d", *(ptr-1)); // 2 getch(); } When you run the program, the output will be: *ptr = 3 *(ptr+1) = 4 *(ptr-1) = 2

Relationship Between Arrays and Pointers #include < stdio.h > int main() { int x[4]; int i ; for( i = 0; i < 4; ++ i ) { printf ("&x[%d] = %p\n", i , &x[ i ]); } printf ("Address of array x: %p", x); getch (); } output: &x[0] = 1450734448 &x[1] = 1450734452 &x[2] = 1450734456 &x[3] = 1450734460 Address of array x: 1450734448

From the above example, it is clear that &x[0] is equivalent to x. And, x[0] is equivalent to *x. Similarly, &x[1] is equivalent to x+1 and x[1] is equivalent to *(x+1). &x[2] is equivalent to x+2 and x[2] is equivalent to *(x+2). ... Basically, &x[ i ] is equivalent to x+i and x[ i ] is equivalent to *( x+i ).

Pointer and Strings #include <stdio.h> #include <string.h> void main() { char str[]="Hello Dafars!"; char *p; int i; p=str; printf("First character is:%c\n",*p); p =p+1; printf("Next character is:%c\n",*p); printf("Printing all the characters in a string\n"); p=str; //reset the pointer for(i=0; i< strlen(str); i++) { printf("%c\n",*p); p++; } getch(); }

Functions with Array Parameters In C, we cannot pass an array by value to a function. Whereas, an array name is a pointer (address), so we just pass an array name to a function which means to pass a pointer to the array. int add_array (int *p, int size); int main() { int arr[5] = {100, 220, 37, 16, 98}; printf(“Sum is %d\n", add_array(arr, 5)); getch(); } int add_array (int *p, int size) { int total = 0; int k; for (k = 0; k < size; k++) { total += p[k]; /* it is equivalent to total +=*p ;p++; */ } return (total); } Output: Sum is 471

Passing Pointers to Functions #include < stdio.h > void addOne (int * ptr ); void main() { int* p, i = 10;d p = & i ; addOne (p); printf ("%d", *p); // 11 getch (); } void addOne (int * ptr ) { (* ptr )++; // adding 1 to * ptr }