A pointer that points to array in C.
Question
Declare an array variable with 8 elements. { 23 45 12 56 72 18 34 45}
Have a pointer that points to array.
Print the address and value of pointer
Try to increment pointer by 5.
Print the address and value of pointer
Try to decrement pointer by 2
Print the address and value of pointer
Try to increment pointer by 7.
Print the address and value of pointer
Try to decrement pointer by 8
Print the address and value of pointer
Now assign another pointer2 and assign the first pointer to this new pointer
Try to increment pointer2 by 4.
Subtract the two pointers and Print the value of the element pointed by pointer2 – pointer1
Print all even number indexed array elements using all the 4 logics below
Array subscripting (standard array indexing)
-
- Pointer offset notation on array name.
- And Pointer offset notation with another pointer.
- Pointer subscript notation on another pointer.
Summary
Here in this C program, we write an array variable with 8 elements. Which is going to Have a pointer that points to array. And we have also given the specific number by which we are going to increment the pointer.
Example:
Here Pointer is used. Pointer is something that is used to point the address in the program of any value. And also pointer holds a variable address. So pointer points to the address that hold the variable with the same data type.
let’s take the example of the pointer.
int a = 11;
int *p = &a;
Now here a is the variable and p is the pointer. And pointer is pointing to the address of the variable a.
Also, we can take the pointer to the next array or location. And this is done by incrementing the pointer. So we just have to put (P+i) instead of only writing p. Where p is the pointer and i is the value by which you want to add the location.
Code
#include<stdio.h> #include<conio.h> void main() { //array declaration int arr[8]={23,45,12,56,72,18,34,45}; int *p=arr; //pointer that points array int *p2,i; //pointer 2 //print address and value of pointer printf("\n\nAddress pf pointer = %d",p); printf("\nValue of pointer = %d",*p); p=p+5; //increment pointer by 5 //print address and value of pointer printf("\n\nAddress pf pointer = %d",p); printf("\nValue of pointer = %d",*p); p=p-2; //decrement pointer by 2; //print address and value of pointer printf("\n\nAddress pf pointer = %d",p); printf("\nValue of pointer = %d",*p); p=p+7; //increment pointer by 7 //print address and value of pointer printf("\n\nAddress pf pointer = %d",p); printf("\nValue of pointer = %d",*p); p=p-8; //decrement pointer by 8; //print address and value of pointer printf("\n\nAddress pf pointer = %d",p); printf("\nValue of pointer = %d",*p); p2=p; //assigning value to pointer2 p2=p2+4; //increment pointer2 by 4 p=arr; printf("\n\nValue after subtraction is : %d",p2-p); //value after subtraction of pointers //printing even elements printf("\nElements at even indexing using array subscripting : "); //array subscripting for(i=0;i<8;i++) if(i%2==0) printf("%d,",arr[i]); printf("\nElements at even indexing using pointer offset notation: "); //pointer offset for(i=0;i<8;i++) if(i%2==0) printf("%d,",*(p+i)); p2=p; printf("\nElements at even indexing using pointer offset notation with another pointer: "); //pointer offset for(i=0;i<8;i++) if(i%2==0) printf("%d,",*(p2+i)); printf("\nElements at even indexing using pointer offset notation on another pointer: "); //pointer offset for(i=0;i<8;i++) if(i%2==0) printf("%d,",*(p2+i)); }
Output
Also read, For these situations, just design the h files you need to implement the classes you’d need.