Apply for Zend Framework Certification Training

c



< Pointers in c 10 Advanced Coding Problems On String Pointers in C >



20 Advanced Pointer Questions with Solutions

1. Program to swap two numbers using pointers
#include <stdio.h>
void swap(int *a, int *b){
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}
int main(){
    int x = 10, y = 20;
    swap(&x,&y);
    printf("x=%d y=%d",x,y);
}
2 Print array values
#include<stdio.h>
int main(){
    int arr[]={23,56,23,78,34};
    int *ptr = arr;
    for(int i=0;i<5;i++){
        printf("%d ",*(ptr+i));
    }
}

3. Program to find sum of array elements using pointers
#include <stdio.h>
int main(){
    int arr[5]={1,2,3,4,5};
    int *p = arr;
    int sum = 0;
    for(int i=0;i<5;i++)
        sum += *(p+i);
    printf("Sum = %d",sum);
}

4. Program to find smallest element using pointer
#include <stdio.h>
int main(){
    int arr[5]={8,4,6,1,9};
    int *p=arr;
    int min=*p;
    for(int i=1;i<5;i++) {
        if(*(p+i)<min)
            min=*(p+i);
    }
    printf("Min=%d",min);
}

5. Program to find Greatest element using pointer
#include <stdio.h>
int main(){
    int arr[5]={8,4,6,1,9};
    int *p=arr;
    int min=*p;
    for(int i=1;i<5;i++) {
        if(*(p+i)<min)
            min=*(p+i);
    }
    printf("Min=%d",min);
}


6. Program to reverse an array using pointers
#include <stdio.h>
int main(){
    int arr[5]={1,2,3,4,5};
    int *start = arr;
    int *end = arr+4;
    int temp;
    while(start < end)    {
        temp = *start;
        *start = *end;
        *end = temp;
        start++;
        end--;
    }
    for(int i=0;i<5;i++)
        printf("%d ",arr[i]);
}
7. Bubble Sort using Pointers

#include <stdio.h>
int main(){
    int arr[]={22,5412,78,23,90};
    int n=5,i,j,*p=arr,temp;
    for(i=0;i<n;i++){
        printf("%d ",*(arr+i));
    }
    for(i=0;i<n-1;i++){
        for(j=0;j<n-i-1;j++){
            if( *(p+j)>*(p+j+1)){
                temp = *(p+j);
                *(p+j)= *(p+j+1);
                *(p+j+1)= temp;
            }
        }
    }
    printf("\n");
    for(i=0;i<n;i++){
        printf("%d ",*(arr+i));
    }
}


(B)Pointer Question on String
8. Program to print string using pointer

#include <stdio.h>
int main(){
    char str[]="Hello";
    char *p=str;
    while(*p!='\0'){
        printf("%c",*p);
        p++;
    }
}

9. Program to count length of string using pointer
#include <stdio.h>
int main(){
    char str[]="Programming";
    char *p=str;
    int len=0;
    while(*p!='\0'){
        len++;
        p++;
    }
    printf("Length=%d",len);
}

10. Program to copy one string to another using pointers
#include <stdio.h>
int main(){
    char str1[]="Pointer";
    char str2[20];
    char *p=str1;
    char *q=str2;
    while(*p!='\0'){
        *q=*p;
        p++;
        q++;
    }
    *q='\0';
    printf("%s",str2);
}
11 find the length of array using pointer without using any predefined function
12 count no of charecter in a string in c

#include <stdio.h>
int main() {
    char str[] = "welcome to be owner";
    char *ptr = str;
    int l = 0;
    // Find length using pointer
    while (*ptr != '\0') {
        l++;
        ptr++;
    }
    printf("Length = %d\n", l);
    for (int i = 0; i < l; i++) {
        // Check if already counted
        int visited = 0;
        for (int k = 0; k < i; k++){
            if (str[i] == str[k]){
                visited = 1;
                break;
            }
        }
        if (visited)
            continue;
        int count = 0;
        for (int j = 0; j < l; j++) {
            if (str[i] == str[j]) {
                count++;
            }
        }
        printf("%c = %d\n", str[i], count);
    }
    return 0;
}
12 count no of character in a string in Java

public class Main {
    public static void main(String[] args) {
        String str = "hello";
        int l = 0;
        // Find length (similar to pointer logic)
        while (l < str.length()) {
            l++;
        }
        System.out.println("Length = " + l);
        for (int i = 0; i < l; i++) {
            // Check if already counted
            boolean visited = false;
            for (int k = 0; k < i; k++) {
                if (str.charAt(i) == str.charAt(k)) {
                    visited = true;
                    break;
                }
            }
            if (visited)
                continue;
            int count = 0;
            for (int j = 0; j < l; j++) {
                if (str.charAt(i) == str.charAt(j)) {
                    count++;
                }
            }
            System.out.println(str.charAt(i) + " = " + count);
        }
    }
}

13. Program to find largest element using pointers
#include <stdio.h>
int main(){
    int arr[5]={5,8,2,9,3};
    int *p=arr;
    int max=*p;
    for(int i=1;i<5;i++){
        if(*(p+i)>max)
            max=*(p+i);
    }
    printf("Max=%d",max);
}
14. Program to print array elements using pointer increment
#include <stdio.h>
int main(){
    int arr[]={10,20,30,40};
    int *p=arr;
    for(int i=0;i<4;i++){
        printf("%d ",*p);
        p++;
    }
}
12. Program demonstrating pointer to pointer
#include <stdio.h>
int main(){
    int a=10;
    int *p=&a;
    int **q=&p;
    printf("%d",**q);
}
13. Program using pointer to function
#include <stdio.h>
int add(int a,int b){
    return a+b;
}
int main(){
    int (*p)(int,int);
    p=add;
    printf("Sum=%d",p(4,5));
}

14. Program to add two numbers using pointers
#include <stdio.h>
int main(){
    int a=5,b=6;
    int *p=&a;
    int *q=&b;
    printf("Sum=%d",*p+*q);
}


15. Program to print address of array elements
#include <stdio.h>
int main(){
    int arr[3]={10,20,30};
    for(int i=0;i<3;i++)
        printf("%p\n",&arr[i]);
}
16. Program demonstrating pointer subtraction
#include <stdio.h>
int main(){
    int arr[]={1,2,3,4,5};
    int *p=&arr[4];
    int *q=&arr[1];
    printf("%ld",p-q);
}
17. Program to print elements using pointer indexing
#include <stdio.h>
int main(){
    int arr[]={11,22,33};
    int *p=arr;
    for(int i=0;i<3;i++)
        printf("%d ",p[i]);
}

18. Program to demonstrate pointer and array relation
#include <stdio.h>
int main(){
    int arr[]={1,2,3};
    printf("%d\n",*(arr+1));
}
Output
2
19. Program using pointer to structure
#include <stdio.h>
struct student{
    int id;
};
int main(){
    struct student s={101};
    struct student *p=&s;
    printf("%d",p->id);
}
20. Program to modify array elements using pointer
#include <stdio.h>
int main(){
    int arr[]={1,2,3};
    int *p=arr;
    for(int i=0;i<3;i++)
        *(p+i)=*(p+i)*2;
    for(int i=0;i<3;i++)
        printf("%d ",arr[i]);
}
21. Program demonstrating pointer comparison
#include <stdio.h>
int main(){
    int arr[5];
    int *p=&arr[0];
    int *q=&arr[4];
    if(p<q)
        printf("p is before q");
}
22. Program to access 2D array using pointer
#include <stdio.h>
int main(){
    int arr[2][2]={{1,2},{3,4}};
    int *p=&arr[0][0];
    for(int i=0;i<4;i++)
        printf("%d ",*(p+i));
}

< Pointers in c 10 Advanced Coding Problems On String Pointers in C >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top