Assignment on pointer with solution

Questions:
  1. Assume the definitions and initializations:
    char c = 'T', d = 'S';
    char *p1 = &c;
    char *p2 = &d;
    char *p3;
    				
    p3 = &d;
    printf("*p3 = %s\n", *p3);   // (1)
    
    p3 = p1;
    printf("*p3 = %s , p3 = %u\n", *p3, p3);
    
    *p1 = *p2;
    printf("*p1 = %s , p1 = %u\n", *p1,p1);

    Ans.

    *p3 = S *p3 = T, p3 = 6940 *p1 = S, p1 = 9772
  2. Consider the following statements:
    int *p;
    int i;
    int k;
    i = 42;
    k = i;
    p = &i;
    				
    A. k = 75; 
    B. *k = 75; 
    C. p = 75; 
    D. *p = 75; 
    E. Two or more of the answers will change i to 75.

    Ans.

    D. *p = 75
  3. Explain the error.
    char c = 'A';
    double *p = &c;
    

    Ans.

    warning: initialization from incompatible pointer type
  4. Give the value of the left-hand side variable in each assignment statement. Assume the lines are executed sequentially. Assume the address of the blocks array is 4434.
    int main()
    {
       char blocks[3] = {'A','B','C'};
       char *ptr = &blocks[0]; // *ptr is initialized with 4434
       char temp;
    
       temp = blocks[0]; // temp = 'A'
       temp = *(blocks + 2); // temp = 'C'
       temp = *(ptr + 1); // temp = 'B'
       temp = *ptr; // temp = 'A'
    
       ptr = blocks + 1; // ptr = 4435
       temp = *ptr; // temp = 'B'
       temp = *(ptr + 1); // temp = 'C'
    
       ptr = blocks; // ptr = 4434
       temp = *++ptr; // temp = 'B'
       temp = ++*ptr; // temp = 'C'
       temp = *ptr++; // temp = 'C'
       temp = *ptr; // temp = 'C'
    
       return 0;
    }
    			
  5. For the following functions, use the pointer notation ONLY. Do NOT use the array index [] notation.
  6. Write a piece of code which prints the characters in a cstring in a reverse order.
    char s[10] = "abcde";
    char* cptr;
    
    // WRITE YOUR CODE HERE
    

    Ans.

    #include<stdio.h> #include<string.h> int main() { char s[10] = "abcde"; char* cptr; int i,l; l=strlen(s); cptr=s; cptr=cptr+l; for (i=l; i >= 0 ;i--) { printf("%c", *cptr); cptr--; } return 0; }
  7. Write a function countEven(int*, int) which receives an integer array and its size, and returns the total number of even numbers in the array.
    	

    Ans.

    #include<stdio.h> #include<stdlib.h> int counteven(int *a,int size) { int i; int n=0; for(i=0;i<size;i++) { if(a[i]%2==0) { n++; } } return n; } int main() { int *a,i=0,n,e; printf("Enter length of array :"); scanf("%d",&n); a=(int *)malloc(n * sizeof(int)); for(i=0;i<n;i++) { printf("Enter a[%d]:",i+1); scanf("%d",(a+i)); } for(i=0;i<n;i++) { printf("a[%d]=%d\n",i+1,*(a+i)); } e=counteven(a,n); printf("\nTotal even no. in given array are %d\n",e); return 0; }
  8. Write a function that returns a pointer to the maximum value of an array of double's. If the array is empty, return NULL.
    double* maximum(double* a, int size);

    Ans.

    #include <stdio.h> double * maximum(double *a, int size) { double *tmp; int i; for(i=0;i<size;i++) { printf("\na[%d]=%0.2f\n",i+1,*(a+i)); } for(i=0;i<size;i++) { if(*(a+i) > *tmp) { tmp=(a+i); } } return tmp; } void main() { double array[4] = {90.00,20.00,80.00,10.00}; double *s; s=maximum(array,4); printf("\nThe largest number is : %0.2f \n",*s); }
  9. Write a function myStrLen(char*) which returns the length of the parameter cstring.  Write the function without using the C function strlen.

    Ans.

    #include<stdio.h> int mystrlen(char *s) { int i=0; while(*(s+i) != '\0') { i++; } return i; } int main() { char *s="This is it"; int l; printf("%s",s); l=mystrlen(s); printf("The length of string is %d \n",l); return 0; }
  10. Write a function contains(char*, char) which returns true if the 1st parameter cstring contains the 2nd parameter char, or false otherwise.

    Ans.

    #include<stdio.h> #include<string.h> #include<stdlib.h> int check(char *s,char j); void main() { char *s,j; int i,l=0; const MAX_LINE_SIZE = 256; char line[ MAX_LINE_SIZE ]; printf("Enter String :"); if (fgets( line, MAX_LINE_SIZE, stdin ) != NULL) { s = malloc( strlen( line ) ); strcpy( s, line ); } printf("Enter Character :"); scanf("%c",&j); l=check(s,j); if(l==1) printf("\ntrue"); else printf("\nfalse"); } int check (char *s,char j) { int l=strlen(s)-1; int i; printf("%d",l); for(i=0;i<l;i++) { if(*(s+i) == j) { return 1; } } }
  11. Write a function revString(char*) which reverses the parameter cstring.  The function returns nothing. Complete the following code.
    int main()
    {
      char s[10] = "abcde";
      revString(s);  // call the function
      return 0;
    }
    
    void revtString(char* ptr)
    {
      // WRITE YOUR CODE HERE
    }
    

    Ans.

    #include<stdio.h> #include<string.h> void revtString(char* ptr); int main() { char s[10] = "abcde"; printf("This is before reversing %s\n ",s); revtString(s); // call the function return 0; } void revtString(char* ptr) { int i,l; l=strlen(ptr); printf("This is after reversing "); ptr +=l; for(i=0;i<=l;i++) { printf("%c",*ptr); ptr--; } }
  12. swap_nums seems to work, but not swap_pointers. Fix it.
    #include <stdio.h> 
    void swap_nums(int *x, int *y) 
    { 
    	int tmp; 
    	tmp = *x; 
    	*x = *y; 
    	*y = tmp; 
    } 
    void swap_pointers(char *x, char *y) 
    { 
    	char *tmp; 
    	tmp = x; 
    	x = y; 
    	y = tmp; 
    } 
    int main() 
    { 
    	int a,b; 
    	char *s1,*s2; 
    	a = 3; b=4; 
    	swap_nums(&a,&b); 
    	printf("a is %d\n", a); 
    	printf("b is %d\n", b); 
    	s1 = "I should print second"; 
    	s2 = "I should print first"; 
    	swap_pointers(s1,s2); 
    	printf("s1 is %s\n", s1); 
    	printf("s2 is %s\n", s2); 
    	return 0; 
    }
    

    Ans.

    #include <stdio.h> void swap_nums(int *x, int *y) { int tmp; tmp = *x; *x = *y; *y = tmp; } void swap_pointers( char **x, char **y) { char *tmp; tmp = (*x); (*x)=(*y); (*y)=tmp; } int main() { int a,b; a = 3; b=4; char *s1,*s2; swap_nums(&a,&b); printf("a is %d\n", a); printf("b is %d\n", b); s1 = "I should print second"; s2 = "I should print first"; swap_pointers(&s1,&s2); printf("s1 is %s\n", s1); printf("s2 is %s\n", s2); return 0; }
  13. Write a program to perform Matrix multiplication and Addtion using DMA( Dynamic memory allocation)

    Ans.

    #include<stdio.h> #include<stdlib.h> void main() { int **a,**b,**m,**s; int i,j,k,r,c,r1,c1; printf("Enter size of row of matrix A :"); scanf("%d",&r); printf("Enter size of column of matrix A :"); scanf("%d",&c); printf("Enter size of row of matrix B : "); scanf("%d",&r1); printf("Enter size of column of matrix B :"); scanf("%d",&c1); a=(int **)malloc(r * sizeof(int)); b=(int **)malloc(r1 * sizeof(int)); for(i=0;i<r;i++) { a[i]=(int *)malloc(c * sizeof(int)); } for(i=0;i<r1;i++) { b[i]=(int *)malloc(c1 * sizeof(int)); } printf("\n\nEnter A:\n\n"); for(i=0;i<r;i++) { for(j=0;j<c;j++) { printf("Enter a[%d][%d]:",i,j); scanf("%d",*(a+i)+j); } } printf("\n\nEnter B:\n\n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { printf("Enter b[%d][%d]:",i,j); scanf("%d",*(b+i)+j); } } printf("\nMatrix A\n"); for(i=0;i<r;i++) { for(j=0;j<c;j++) { printf("%d ",*(*(a+i)+j)); } printf("\n"); } printf("\n Matrix B\n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { printf("%d ",*(*(b+i)+j)); } printf("\n"); } if(r==r1 && c==c1) { s=(int **)malloc(r * sizeof(int)); for(i=0;i<r;i++) { s[i]=(int *)malloc(c * sizeof(int)); } printf("\n\nSum of matrix.\n"); for(i=0;i<r;i++) { for(j=0;j<c;j++) { *(*(s+i)+j)=0; *(*(s+i)+j)=*(*(a+i)+j) + *(*(b+i)+j); } } for(i=0;i<r;i++) { for(j=0;j<c;j++) { printf("%d\t",*(*(s+i)+j)); } printf("\n"); } free(s); } else { printf("\n\nSum of matrix is not possible.\n"); } if(c==r1) { m=(int **)malloc(r * sizeof(int)); for(i=0;i<r;i++) { m[i]=(int *)malloc(c * sizeof(int)); } printf("\n\nMultiplication of matrix.\n"); for(i=0;i<r;i++) { for(j=0;j<c;j++) { *(*(m+i)+j)=0; for(k=0;k<c;k++) { *(*(m+i)+j)=*(*(m+i)+j)+*(*(a+i)+k) * *(*(b+k)+j); } } } for(i=0;i<r;i++) { for(j=0;j<c;j++) { printf("%d\t",*(*(m+i)+j)); } printf("\n"); } free(m); } else { printf("\n\nMultiplication of matrix is not possible.\n"); } free(a); free(b); }
  14. Write a program to create array of pointer to hold 10 strings and sort it in ascending order.

    Ans.

    #include<string.h> #include<stdlib.h> void main() { char *s[10],*tmp; int i,j; for(i=0;i<10;i++) { s[i]=(char *)malloc(10 * sizeof(char)); } for(i=0;i<10;i++) { printf("Enter String [%d] :",i+1); scanf("%s",s[i]); } for(i=0;i<10-1;i++) { for(j=i+1;j<10;j++) { if(strcmp(s[i],s[j])>0) { tmp=s[i]; s[i]=s[j]; s[j]=tmp; } } } printf("\nRe-ordered Strings\n"); for(i=0;i<10;i++) { printf("\n%s",s[i]); } }
  15. Write a program to input one string and a character , count how many times the character is occurred in the given string.

    Ans.

    #include<stdio.h> #include<stdlib.h> #include<string.h> void main() { char *s; char j; int i,n=0; const MAX_LINE_SIZE = 256; char line[ MAX_LINE_SIZE ]; printf("Enter String :"); if (fgets( line, MAX_LINE_SIZE, stdin ) != NULL) { s = malloc( strlen( line ) ); strcpy( s, line ); } printf("Enter the character you want to count :"); scanf("%c",&j); for(i=0;i<strlen(s);i++) { if(*(s+i) == j) n++; } printf("%c occurs %d times\n",j,n); }