C Programming Exercises, Practice, Solution : Pointer

1.在C中编写一个程序以显示指针的基本声明。
期待输出:

z sotres the address of m  = 0x7ffe97a39854
*z stores the value of m = 10
&m is the address of m = 0x7ffe97a39854
&n stores the address of n = 0x7ffe97a39858
&o stores the address of o = 0x7ffe97a3985c
&z stores the address of z = 0x7ffe97a39860

解决:


1. #include <stdio.h>  
2. void main(void)
3. {
4. int m=10,n,o;
5. int *z=&m ;
6.
7. "\n\n Pointer : Show the basic declaration of pointer :\n");
8. "-------------------------------------------------------\n");
9. " Here is m=10, n and o are two integer variable and *z is an integer");
10. "\n\n z stores the address of m = %p\n", z); // z is a pointer so %p would print the address
11. "\n *z stores the value of m = %i\n", *z);
12. "\n &m is the address of m = %p\n", &m); // &m gives the address of the integer variable m
13. // so %p is the specifier for that address
14. "\n &n stores the address of n = %p\n", &n);
15. "\n &o stores the address of o = %p\n", &o);
16. "\n &z stores the address of z = %p\n\n", &z); // &z gives the address, where the pointer z is
17. // stored -> still an address -> %p is the right
18. // specifier
19. }


2.在C中编写一个程序,演示如何处理程序中的指针。

期待输出:


Address of m : 0x7ffcc3ad291c
Value of m : 29

Now ab is assigned with the address of m.
Address of pointer ab : 0x7ffcc3ad291c
Content of pointer ab : 29

The value of m assigned to 34 now.
Address of pointer ab : 0x7ffcc3ad291c
Content of pointer ab : 34

The pointer variable ab is assigned with the value 7 now.
Address of m : 0x7ffcc3ad291c
Value of m : 7


C Code:




1. #include <stdio.h>  
2. int main()
3. {
4. int* ab;
5. int m;
6. m=29;
7. "\n\n Pointer : How to handle the pointers in the program :\n");
8. "------------------------------------------------------------\n");
9. " Here in the declaration ab = int pointer, int m= 29\n\n");
10.
11. " Address of m : %p\n",&m);
12. " Value of m : %d\n\n",m);
13. ab=&m;
14. " Now ab is assigned with the address of m.\n");
15. " Address of pointer ab : %p\n",ab);
16. " Content of pointer ab : %d\n\n",*ab);
17. m=34;
18. " The value of m assigned to 34 now.\n");
19. " Address of pointer ab : %p\n",ab);
20. " Content of pointer ab : %d\n\n",*ab);
21. *ab=7;
22. " The pointer variable ab is assigned the value 7 now.\n");
23. " Address of m : %p\n",&m);//as ab contain the address of m
24. //so *ab changed the value of m and now m become 7
25. " Value of m : %d\n\n",m);
26. return 0;
27. }



3.在C中编写程序以演示使用&(地址of)和*(地址处的值)运算符。


期待输出:


Using & operator : ----------------------- address of m = 0x7ffea3610bb8 address of fx = 0x7ffea3610bbc address of cht = 0x7ffea3610bb7 Using &

and * operator :                                                                                     
-----------------------------
value at address of m = 300
value at address of fx = 300.600006
value at address of cht = z

Using only pointer variable :
----------------------------------
address of m = 0x7ffea3610bb8
address of fx = 0x7ffea3610bbc
address of cht = 0x7ffea3610bb7

Using only pointer operator :
----------------------------------
value at address of m = 300
value at address of fx= 300.600006
value at address of cht= z

C Code:



1. #include <stdio.h>  
2. void main()
3. {
4. int m=300;
5. float fx = 300.60;
6. char cht = 'z';
7.
8. "\n\n Pointer : Demonstrate the use of & and * operator :\n");
9. "--------------------------------------------------------\n");
10.
11.
12. int *pt1;
13. float *pt2;
14. char *pt3;
15. pt1= &m;
16. pt2=&fx;
17. pt3=&cht;
18. " m = %d\n",m);
19. " fx = %f\n",fx);
20. " cht = %c\n",cht);
21. "\n Using & operator :\n");
22. "-----------------------\n");
23. " address of m = %p\n",&m);
24. " address of fx = %p\n",&fx);
25. " address of cht = %p\n",&cht);
26. "\n Using & and * operator :\n");
27. "-----------------------------\n");
28. " value at address of m = %d\n",*(&m));
29. " value at address of fx = %f\n",*(&fx));
30. " value at address of cht = %c\n",*(&cht));
31. "\n Using only pointer variable :\n");
32. "----------------------------------\n");
33. " address of m = %p\n",pt1);
34. " address of fx = %p\n",pt2);
35. " address of cht = %p\n",pt3);
36. "\n Using only pointer operator :\n");
37. "----------------------------------\n");
38. " value at address of m = %d\n",*pt1);
39. " value at address of fx= %f\n",*pt2);
40. " value at address of cht= %c\n\n",*pt3);
41. }

4.在C中编写一个程序,使用指针添加两个数字。

测试数据:
输入第一个数字:5
输入第二个数字:6
期待输出:


The sum of the entered numbers is : 11


C Code:

1. #include <stdio.h>  
2. int main()
3. {
4. int fno, sno, *ptr, *qtr, sum;
5.
6. "\n\n Pointer : Add two numbers :\n");
7. "--------------------------------\n");
8.
9. " Input the first number : ");
10. "%d", &fno);
11. " Input the second number : ");
12. "%d", &sno);
13.
14. ptr = &fno;
15. qtr = &sno;
16.
17. sum = *ptr + *qtr;
18.
19. " The sum of the entered numbers is : %d\n\n",sum);
20.
21. return 0;
22. }

5.在C中编写程序,使用调用引用添加数字。

测试数据:
输入第一个数字:5
输入第二个数字:6
期待输出:


The sum of 5 and 6 is 11


C Code:

1. #include <stdio.h>  
2. long addTwoNumbers(long *, long *);
3.
4. int main()
5. {
6. long fno, sno, *ptr, *qtr, sum;
7.
8. "\n\n Pointer : Add two numbers using call by reference:\n");
9. "-------------------------------------------------------\n");
10.
11. " Input the first number : ");
12. "%ld", &fno);
13. " Input the second number : ");
14. "%ld", &sno);
15. sum = addTwoNumbers(&fno, &sno);
16. " The sum of %ld and %ld is %ld\n\n", fno, sno, sum);
17. return 0;
18. }
19. long addTwoNumbers(long *n1, long *n2)
20. {
21. long sum;
22. sum = *n1 + *n2;
23. return sum;
24. }


6.在C中编写一个程序,使用指针找到两个数字之间的最大数目。 转到编辑器

测试数据:
输入第一个数字:5
输入第二个数字:6
期待输出:


6 is the maximum number.


C Code:


#include <stdio.h>
#include <stdlib.h>
void main()
{
int fno,sno,*ptr1=&fno,*ptr2=&sno;

printf("\n\n Pointer : Find the maximum number between two numbers :\n");
printf("------------------------------------------------------------\n");

printf(" Input the first number : ");
scanf("%d", ptr1);
printf(" Input the second number : ");
scanf("%d", ptr2);


if(*ptr1>*ptr2)
{
printf("\n\n %d is the maximum number.\n\n",*ptr1);
}
else
{
printf("\n\n %d is the maximum number.\n\n",*ptr2);
}

}


7.在C中编写一个程序,将n个元素存储在数组中,并使用指针打印元素。 
测试数据:
输入要存储在数组中的元素数量:5
输入5数组中的元素数量:
元素 - 0:5
元素-1:7
元素-2:2
元素-3:9
元素-4:8
期待输出:


The elements you entered are : element - 0 : 5 element - 1 : 7 element - 2 : 2 element - 3 : 9 element - 4 : 8


C Code:



    1. #include <stdio.h>  
    2. int main()
    3. {
    4. int arr1[25], i,n;
    5. "\n\n Pointer : Store and retrieve elements from an array :\n");
    6. "------------------------------------------------------------\n");
    7. " Input the number of elements to store in the array :");
    8. "%d",&n);
    9.
    10. " Input %d number of elements in the array :\n",n);
    11. for(i=0;i<n;i++)
    12. {
    13. " element - %d : ",i);
    14. "%d",arr1+i);
    15. }
    16. " The elements you entered are : \n");
    17. for(i=0;i<n;i++)
    18. {
    19. " element - %d : %d \n",i,*(arr1+i));
    20. }
    21. return 0;
    22. }


    8.在C中编写一个程序,使用指针打印给定字符串的所有排列。
    期待输出:


    The permutations of the string are : abcd abdc acbd acdb adcb adbc bacd badc bcad bcda bdca bdac cbad cbda cabd cadb cdab cdba db ca dbac dcba dcab dacb dabc


    C Code:


    #include <stdio.h>
    #include <string.h>

    void changePosition(char *ch1, char *ch2)
    {
    char tmp;
    tmp = *ch1;
    *ch1 = *ch2;
    *ch2 = tmp;
    }
    void charPermu(char *cht, int stno, int endno)
    {
    int i;
    if (stno == endno)
    printf("%s ", cht);
    else
    {
    for (i = stno; i <= endno; i++)
    {
    changePosition((cht+stno), (cht+i));
    charPermu(cht, stno+1, endno);
    changePosition((cht+stno), (cht+i));
    }
    }
    }

    int main()
    {
    char str[] = "abcd";
    printf("\n\n Pointer : Generate permutations of a given string :\n");
    printf("--------------------------------------------------------\n");
    int n = strlen(str);
    printf(" The permutations of the string are : \n");
    charPermu(str, 0, n-1);
    printf("\n\n");
    return 0;
    }


    9.在C中编写一个程序,使用动态内存分配找到最大的元素。
    测试数据:
    Input total number of elements(1 to 100): 5 

    Number 1: 5 
    Number 2: 7 
    Number 3: 2 
    Number 4: 9 
    Number 5: 8
    期待输出:


    The Largest element is : 9.00


    C Code:



    1. #include <stdio.h>  
    2. #include <stdlib.h>
    3. int main()
    4. {
    5. int i,n;
    6. float *element;
    7. "\n\n Pointer : Find the largest element using Dynamic Memory Allocation :\n");
    8. "-------------------------------------------------------------------------\n");
    9. " Input total number of elements(1 to 100): ");
    10. "%d",&n);
    11. float*)calloc(n,sizeof(float)); // Memory is allocated for 'n' elements
    12. if(element==NULL)
    13. {
    14. " No memory is allocated.");
    15. exit(0);
    16. }
    17. "\n");
    18. for(i=0;i<n;++i)
    19. {
    20. " Number %d: ",i+1);
    21. "%f",element+i);
    22. }
    23. for(i=1;i<n;++i)
    24. {
    25. if(*element<*(element+i))
    26. *element=*(element+i);
    27. }
    28. " The Largest element is : %.2f \n\n",*element);
    29. return 0;
    30. }



    10.在C中编写一个程序,使用指针计算字符串的长度。
    测试数据:
    输入字符串:w3resource
    期待输出:


    The length of the given string w3resource is : 10


    C Code:


    #include <stdio.h>
    int calculateLength(char*);

    void main()
    {
    char str1[25];
    int l;
    printf("\n\n Pointer : Calculate the length of the string :\n");
    printf("---------------------------------------------------\n");

    printf(" Input a string : ");
    fgets(str1, sizeof str1, stdin);

    l = calculateLength(str1);
    printf(" The length of the given string %s is : %d ", str1, l-1);
    printf("\n\n");

    }

    int calculateLength(char* ch) // ch = base address of array str1 ( &str1[0] )
    {
    int ctr = 0;
    while (*ch != '\0')
    {
    ctr++;
    ch++;
    }
    return ctr;
    }


    11. Write a program in C to swap elements using call by reference. ​​Go to the editor​​​ Test Data : 
    Input the value of 1st element : 5 
    Input the value of 2nd element : 6 
    Input the value of 3rd element : 7 
    期待输出:


    The value before swapping are : element 1 = 5 element 2 = 6 element 3 = 7 The value after swapping are : element 1 = 7 element 2 = 5 element 3 = 6


    C Code:



    1. #include <stdio.h>  
    2. void swapNumbers(int *x,int *y,int *z);
    3. int main()
    4. {
    5. int e1,e2,e3;
    6. "\n\n Pointer : Swap elements using call by reference :\n");
    7. "------------------------------------------------------\n");
    8. " Input the value of 1st element : ");
    9. "%d",&e1);
    10. " Input the value of 2nd element : ");
    11. "%d",&e2);
    12. " Input the value of 3rd element : ");
    13. "%d",&e3);
    14.
    15.
    16. "\n The value before swapping are :\n");
    17. " element 1 = %d\n element 2 = %d\n element 3 = %d\n",e1,e2,e3);
    18. swapNumbers(&e1,&e2,&e3);
    19. "\n The value after swapping are :\n");
    20. " element 1 = %d\n element 2 = %d\n element 3 = %d\n\n",e1,e2,e3);
    21. return 0;
    22. }
    23. void swapNumbers(int *x,int *y,int *z)
    24. {
    25. int tmp;
    26. tmp=*y;
    27. *y=*x;
    28. *x=*z;
    29. *z=tmp;
    30. }


    12.在C中编写一个程序,使用指针查找给定数字的阶乘。 
    测试数据:
    输入数字:5
    期待输出:


    The Factorial of 5 is : 120


    C Code:




    1. #include <stdio.h>  
    2. void findFact(int,int*);
    3. int main()
    4. {
    5. int fact;
    6. int num1;
    7. "\n\n Pointer : Find the factorial of a given number :\n");
    8. "------------------------------------------------------\n");
    9. " Input a number : ");
    10. "%d",&num1);
    11.
    12. findFact(num1,&fact);
    13. " The Factorial of %d is : %d \n\n",num1,fact);
    14. return 0;
    15. }
    16.
    17. void findFact(int n,int *f)
    18. {
    19. int i;
    20.
    21. *f =1;
    22. for(i=1;i<=n;i++)
    23. *f=*f*i;
    24. }



    13.在C中编写一个程序,使用指针来计算字符串中元音和辅音的数量。 
    测试数据:
    输入字符串:string
    期待输出:


    Number of vowels : 1 Number of constant : 5


    C Code:


    1. #include <stdio.h>  
    2. int main()
    3. {
    4. char str1[50];
    5. char *pt;
    6. int ctrV,ctrC;
    7. "\n\n Pointer : Count the number of vowels and consonants :\n");
    8. "----------------------------------------------------------\n");
    9. " Input a string: ");
    10. sizeof str1, stdin);
    11.
    12. //assign address of str1 to pt
    13. pt=str1;
    14.
    15. ctrV=ctrC=0;
    16. while(*pt!='\0')
    17. {
    18. if(*pt=='A' ||*pt=='E' ||*pt=='I' ||*pt=='O' ||*pt=='U' ||*pt=='a' ||*pt=='e' ||*pt=='i' ||*pt=='o' ||*pt=='u')
    19. ctrV++;
    20. else
    21. ctrC++;
    22. //pointer is increasing for searching the next character
    23. }
    24.
    25. " Number of vowels : %d\n Number of constant : %d\n",ctrV,ctrC);
    26. return 0;
    27. }


    14.在C中编写一个程序,使用指针对数组进行排序。
    测试数据:
    testdata 
    期待输出:


    Test Data : 


    Input the number of elements to store in the array : 5 


    Input 5 number of elements in the array : 


    element - 1 : 25 


    element - 2 : 45 


    element - 3 : 89 


    element - 4 : 15 


    element - 5 : 82 


    期待输出:


    The elements in the array after sorting : element - 1 : 15 element - 2 : 25 element - 3 : 45 element - 4 : 82 element - 5 : 89


    C Code:




      1. #include <stdio.h>  
      2. void main()
      3. {
      4. int *a,i,j,tmp,n;
      5. "\n\n Pointer : Sort an array using pointer :\n");
      6. "--------------------------------------------\n");
      7.
      8. " Input the number of elements to store in the array : ");
      9. "%d",&n);
      10.
      11. " Input %d number of elements in the array : \n",n);
      12. for(i=0;i<n;i++)
      13. {
      14. " element - %d : ",i+1);
      15. "%d",a+i);
      16. }
      17. for(i=0;i<n;i++)
      18. {
      19. for(j=i+1;j<n;j++)
      20. {
      21. if( *(a+i) > *(a+j))
      22. {
      23. tmp = *(a+i);
      24. *(a+i) = *(a+j);
      25. *(a+j) = tmp;
      26. }
      27. }
      28. }
      29. "\n The elements in the array after sorting : \n");
      30. for(i=0;i<n;i++)
      31. {
      32. " element - %d : %d \n",i+1,*(a+i));
      33. }
      34. printf("\n");
      35. }

      15.在C中编写一个程序,显示一个函数返回指针。
      测试数据:
      Input the first number : 5 
      Input the second number : 6 
      期待输出:


      The number 6 is larger.


      C Code:




      1. #include <stdio.h>  
      2. int* findLarger(int*, int*);
      3. void main()
      4. {
      5. int numa=0;
      6. int numb=0;
      7. int *result;
      8. "\n\n Pointer : Show a function returning pointer :\n");
      9. "--------------------------------------------------\n");
      10. " Input the first number : ");
      11. "%d", &numa);
      12. " Input the second number : ");
      13. "%d", &numb);
      14.
      15. result=findLarger(&numa, &numb);
      16. " The number %d is larger. \n\n",*result);
      17. }
      18.
      19. int* findLarger(int *n1, int *n2)
      20. {
      21. if(*n1 > *n2)
      22. return n1;
      23. else
      24. return n2;
      25. }



      16.在C中编写一个程序,使用指针计算数组中所有元素的总和。
      测试数据:
      Input the number of elements to store in the array (max 10) : 5 
      Input 5 number of elements in the array : 
      element - 1 : 2 
      element - 2 : 3 
      element - 3 : 4 
      element - 4 : 5 
      element - 5 : 6
      期待输出:


      The sum of array is : 20


      C Code:



      1. #include <stdio.h>  
      2. void main()
      3. {
      4. int arr1[10];
      5. int i,n, sum = 0;
      6. int *pt;
      7.
      8. "\n\n Pointer : Sum of all elements in an array :\n");
      9. "------------------------------------------------\n");
      10.
      11. " Input the number of elements to store in the array (max 10) : ");
      12. "%d",&n);
      13.
      14. " Input %d number of elements in the array : \n",n);
      15. for(i=0;i<n;i++)
      16. {
      17. " element - %d : ",i+1);
      18. "%d",&arr1[i]);
      19. }
      20.
      21. // pt store the base address of array arr1
      22.
      23. for (i = 0; i < n; i++) {
      24. sum = sum + *pt;
      25. pt++;
      26. }
      27.
      28. " The sum of array is : %d\n\n", sum);
      29. }


      17.在C中编写一个程序,以相反的顺序打印数组的元素。
      测试数据:
      Input the number of elements to store in the array (max 15) : 5 
      Input 5 number of elements in the array : 
      element - 1 : 2 
      element - 2 : 3 
      element - 3 : 4 
      element - 4 : 5 
      element - 5 : 6 
      期待输出:


      The elements of array in reverse order are : element - 5 : 6 element - 4 : 5 element - 3 : 4 element - 2 : 3 element - 1 : 2


      C Code:


      1. #include <stdio.h>  
      2. void main()
      3. {
      4. int n, i, arr1[15];
      5. int *pt;
      6. "\n\n Pointer : Print the elements of an array in reverse order :\n");
      7. "----------------------------------------------------------------\n");
      8.
      9. " Input the number of elements to store in the array (max 15) : ");
      10. "%d",&n);
      11. // pt stores the address of base array arr1
      12. " Input %d number of elements in the array : \n",n);
      13. for(i=0;i<n;i++)
      14. {
      15. " element - %d : ",i+1);
      16. "%d",pt);//accept the address of the value
      17. pt++;
      18. }
      19.
      20. pt = &arr1[n - 1];
      21.
      22. "\n The elements of array in reverse order are :");
      23.
      24. for (i = n; i > 0; i--)
      25. {
      26. "\n element - %d : %d ", i, *pt);
      27. pt--;
      28. }
      29. printf("\n\n");
      30. }

      18.在C中编写一个程序以显示指针对结构的使用。
      期待输出:


      John Alter from Court Street


      C Code:



      19.在C中编写一个程序以显示指向union的指针。
      期待输出:

      1. #include <stdio.h>  
      2. struct EmpAddress
      3. {
      4. char *ename;
      5. char stname[20];
      6. int pincode;
      7. }
      8. employee={"John Alter","Court Street \n",654134},*pt=&employee;
      9.
      10. int main()
      11. {
      12. "\n\n Pointer : Show the usage of pointer to structure :\n");
      13. "--------------------------------------------------------\n");
      14. " %s from %s \n\n",pt->ename,(*pt).stname);
      15. return 0;
      16. }


      Jhon Mc Jhon Mc


      C Code:



      1. #include <stdio.h>  
      2. union empAdd
      3. {
      4. char *ename;
      5. char stname[20];
      6. int pincode;
      7. };
      8.
      9. int main()
      10. {
      11. "\n\n Pointer : Show a pointer to union :\n");
      12. "----------------------------------------\n");
      13. union empAdd employee,*pt;
      14. "Jhon Mc\0Donald";//assign the string up to null character i.e. '\0'
      15.
      16. pt=&employee;
      17.
      18. " %s %s\n\n",pt->ename,(*pt).ename);
      19.
      20. return 0;
      21. }


      20.在C中编写一个程序,以显示一个指向数组的指针,内容是指向结构的指针。
      期待输出:


      Exmployee Name : Alex Employee ID : 1002


      C Code:



      1. #include <stdio.h>  
      2. struct employee
      3. {
      4. char *empname;
      5. int empid;
      6. };
      7.
      8. int main()
      9. {
      10. "\n\n Pointer : Show a pointer to an array which contents are pointer to structure :\n");
      11. "-----------------------------------------------------------------------------------\n");
      12.
      13. static struct employee emp1={"Jhon",1001},emp2={"Alex",1002},emp3={"Taylor",1003};
      14. struct employee(*arr[])={&emp1,&emp2,&emp3};
      15. struct employee(*(*pt)[3])=&arr;
      16.
      17. " Exmployee Name : %s \n",(**(*pt+1)).empname);
      18. "---------------- Explanation --------------------\n");
      19. "(**(*pt+1)).empname\n");
      20. "= (**(*&arr+1)).empname as pt=&arr\n");
      21. "= (**(arr+1)).empname from rule *&pt = pt\n");
      22. "= (*arr[1]).empname from rule *(pt+i) = pt[i]\n");
      23. "= (*&emp2).empname as arr[1] = &emp2\n");
      24. "= e2.empname = Alex from rule *&pt = pt\n\n");
      25. " Employee ID : %d\n",(*(*pt+1))->empid);
      26. "---------------- Explanation --------------------\n");
      27. "(*(*pt+1))-> empid\n");
      28. "= (**(*pt+1)).empid from rule -> = (*).\n");
      29. "= emp2.empid = 1002\n");
      30. "\n\n");
      31. return 0;
      32. }


      21.在C中编写一个程序,使用指针打印所有的字母表。
      期待输出:


      The Alphabets are : A B C D E F G H I J K L M N O P Q R S T U V W X Y Z


      C Code:




      1. #include <stdio.h>  
      2.
      3. int main()
      4. {
      5. char alph[27];
      6. int x;
      7. char *ptr;
      8. "\n\n Pointer : Print all the alphabets:\n");
      9. "----------------------------------------\n");
      10. ptr = alph;
      11.
      12. for(x=0;x<26;x++)
      13. {
      14. 'A';
      15. ptr++;
      16. }
      17. ptr = alph;
      18.
      19. printf(" The Alphabets are : \n");
      20. for(x=0;x<26;x++)
      21. {
      22. " %c ", *ptr);
      23. ptr++;
      24. }
      25. "\n\n");
      26. return(0);
      27. }



      22.在C中编写一个程序,使用指针反向打印一个字符串。
      测试数据:
      Input a string : w3resource 
      期待输出:


      Reverse of the string is : ecruoser3w



      C Code:




      1. #include <stdio.h>  
      2. int main()
      3. {
      4. char str1[50];
      5. char revstr[50];
      6. char *stptr = str1;
      7. char *rvptr = revstr;
      8. int i=-1;
      9. "\n\n Pointer : Print a string in reverse order :\n");
      10. "------------------------------------------------\n");
      11. " Input a string : ");
      12. "%s",str1);
      13. while(*stptr)
      14. {
      15. stptr++;
      16. i++;
      17. }
      18. while(i>=0)
      19. {
      20. stptr--;
      21. *rvptr = *stptr;
      22. rvptr++;
      23. --i;
      24. }
      25. '\0';
      26. " Reverse of the string is : %s\n\n",revstr);
      27. return 0;
      28. }