Example programs C,P2

Q1 Program to read and print an array using pointers in C.

int main() {
    int arr[10];
    int N,i;
    int *ptr=arr;
    printf("Enter size of an array:");
    scanf("%d",&N);
    printf("Enter elements in array:\n");
    for(i=0;i<N;i++){
        scanf("%d",(ptr+i));
    }
    printf("Array elements:");
    for(i=0;i<N;i++){
        printf("%d",*(ptr+i));
    }
    return 0;
}

Q2 Program to swap 2 numbers using pointers in C.

#include<stdio.h>
int
main(){
int a,b,*p1=&a,*p2=&b;
scanf("%d%d",&a,&b);
printf("\nBefore swap:swap:*p1=%d *p2=%d",*p1,*p2);
*p1=*p1+*p2;
*p2=*p1-*p2;
*p1=*p1-*p2;
printf("\nAfter swap:swap:*p1=%d *p2=%d",*p1,*p2);
return 0;
}

Thanks for reading this blog. Hope this blog helped you and dont forget to follow.