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
解决:
- #include <stdio.h>
- void main(void)
- {
- int m=10,n,o;
- int *z=&m ;
- printf("\n\n Pointer : Show the basic declaration of pointer :\n");
- printf("-------------------------------------------------------\n");
- printf(" Here is m=10, n and o are two integer variable and *z is an integer");
- printf("\n\n z stores the address of m = %p\n", z); // z is a pointer so %p would print the address
- printf("\n *z stores the value of m = %i\n", *z);
- printf("\n &m is the address of m = %p\n", &m); // &m gives the address of the integer variable m
- // so %p is the specifier for that address
- printf("\n &n stores the address of n = %p\n", &n);
- printf("\n &o stores the address of o = %p\n", &o);
- printf("\n &z stores the address of z = %p\n\n", &z); // &z gives the address, where the pointer z is
- // stored -> still an address -> %p is the right
- // specifier
- }
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:
- #include <stdio.h>
- int main()
- {
- int* ab;
- int m;
- m=29;
- printf("\n\n Pointer : How to handle the pointers in the program :\n");
- printf("------------------------------------------------------------\n");
- printf(" Here in the declaration ab = int pointer, int m= 29\n\n");
- printf(" Address of m : %p\n",&m);
- printf(" Value of m : %d\n\n",m);
- ab=&m;
- printf(" Now ab is assigned with the address of m.\n");
- printf(" Address of pointer ab : %p\n",ab);
- printf(" Content of pointer ab : %d\n\n",*ab);
- m=34;
- printf(" The value of m assigned to 34 now.\n");
- printf(" Address of pointer ab : %p\n",ab);
- printf(" Content of pointer ab : %d\n\n",*ab);
- *ab=7;
- printf(" The pointer variable ab is assigned the value 7 now.\n");
- printf(" Address of m : %p\n",&m);//as ab contain the address of m
- //so *ab changed the value of m and now m become 7
- printf(" Value of m : %d\n\n",m);
- return 0;
- }
期待输出:
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:
- #include <stdio.h>
- void main()
- {
- int m=300;
- float fx = 300.60;
- char cht = 'z';
- printf("\n\n Pointer : Demonstrate the use of & and * operator :\n");
- printf("--------------------------------------------------------\n");
- int *pt1;
- float *pt2;
- char *pt3;
- pt1= &m;
- pt2=&fx;
- pt3=&cht;
- printf ( " m = %d\n",m);
- printf ( " fx = %f\n",fx);
- printf ( " cht = %c\n",cht);
- printf("\n Using & operator :\n");
- printf("-----------------------\n");
- printf ( " address of m = %p\n",&m);
- printf ( " address of fx = %p\n",&fx);
- printf ( " address of cht = %p\n",&cht);
- printf("\n Using & and * operator :\n");
- printf("-----------------------------\n");
- printf ( " value at address of m = %d\n",*(&m));
- printf ( " value at address of fx = %f\n",*(&fx));
- printf ( " value at address of cht = %c\n",*(&cht));
- printf("\n Using only pointer variable :\n");
- printf("----------------------------------\n");
- printf ( " address of m = %p\n",pt1);
- printf ( " address of fx = %p\n",pt2);
- printf ( " address of cht = %p\n",pt3);
- printf("\n Using only pointer operator :\n");
- printf("----------------------------------\n");
- printf ( " value at address of m = %d\n",*pt1);
- printf ( " value at address of fx= %f\n",*pt2);
- printf ( " value at address of cht= %c\n\n",*pt3);
- }
4.在C中编写一个程序,使用指针添加两个数字。
测试数据:
输入第一个数字:5
输入第二个数字:6
期待输出:
The sum of the entered numbers is : 11
C Code:
- #include <stdio.h>
- int main()
- {
- int fno, sno, *ptr, *qtr, sum;
- printf("\n\n Pointer : Add two numbers :\n");
- printf("--------------------------------\n");
- printf(" Input the first number : ");
- scanf("%d", &fno);
- printf(" Input the second number : ");
- scanf("%d", &sno);
- ptr = &fno;
- qtr = &sno;
- sum = *ptr + *qtr;
- printf(" The sum of the entered numbers is : %d\n\n",sum);
- return 0;
- }
5.在C中编写程序,使用调用引用添加数字。
测试数据:
输入第一个数字:5
输入第二个数字:6
期待输出:
The sum of 5 and 6 is 11
C Code:
- #include <stdio.h>
- long addTwoNumbers(long *, long *);
- int main()
- {
- long fno, sno, *ptr, *qtr, sum;
- printf("\n\n Pointer : Add two numbers using call by reference:\n");
- printf("-------------------------------------------------------\n");
- printf(" Input the first number : ");
- scanf("%ld", &fno);
- printf(" Input the second number : ");
- scanf("%ld", &sno);
- sum = addTwoNumbers(&fno, &sno);
- printf(" The sum of %ld and %ld is %ld\n\n", fno, sno, sum);
- return 0;
- }
- long addTwoNumbers(long *n1, long *n2)
- {
- long sum;
- sum = *n1 + *n2;
- return sum;
- }
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:
- #include <stdio.h>
- int main()
- {
- int arr1[25], i,n;
- printf("\n\n Pointer : Store and retrieve elements from an array :\n");
- printf("------------------------------------------------------------\n");
- printf(" Input the number of elements to store in the array :");
- scanf("%d",&n);
- printf(" Input %d number of elements in the array :\n",n);
- for(i=0;i<n;i++)
- {
- printf(" element - %d : ",i);
- scanf("%d",arr1+i);
- }
- printf(" The elements you entered are : \n");
- for(i=0;i<n;i++)
- {
- printf(" element - %d : %d \n",i,*(arr1+i));
- }
- return 0;
- }
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:
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- int i,n;
- float *element;
- printf("\n\n Pointer : Find the largest element using Dynamic Memory Allocation :\n");
- printf("-------------------------------------------------------------------------\n");
- printf(" Input total number of elements(1 to 100): ");
- scanf("%d",&n);
- element=(float*)calloc(n,sizeof(float)); // Memory is allocated for 'n' elements
- if(element==NULL)
- {
- printf(" No memory is allocated.");
- exit(0);
- }
- printf("\n");
- for(i=0;i<n;++i)
- {
- printf(" Number %d: ",i+1);
- scanf("%f",element+i);
- }
- for(i=1;i<n;++i)
- {
- if(*element<*(element+i))
- *element=*(element+i);
- }
- printf(" The Largest element is : %.2f \n\n",*element);
- return 0;
- }
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:
- #include <stdio.h>
- void swapNumbers(int *x,int *y,int *z);
- int main()
- {
- int e1,e2,e3;
- printf("\n\n Pointer : Swap elements using call by reference :\n");
- printf("------------------------------------------------------\n");
- printf(" Input the value of 1st element : ");
- scanf("%d",&e1);
- printf(" Input the value of 2nd element : ");
- scanf("%d",&e2);
- printf(" Input the value of 3rd element : ");
- scanf("%d",&e3);
- printf("\n The value before swapping are :\n");
- printf(" element 1 = %d\n element 2 = %d\n element 3 = %d\n",e1,e2,e3);
- swapNumbers(&e1,&e2,&e3);
- printf("\n The value after swapping are :\n");
- printf(" element 1 = %d\n element 2 = %d\n element 3 = %d\n\n",e1,e2,e3);
- return 0;
- }
- void swapNumbers(int *x,int *y,int *z)
- {
- int tmp;
- tmp=*y;
- *y=*x;
- *x=*z;
- *z=tmp;
- }
12.在C中编写一个程序,使用指针查找给定数字的阶乘。
测试数据:
输入数字:5
期待输出:
The Factorial of 5 is : 120
C Code:
- #include <stdio.h>
- void findFact(int,int*);
- int main()
- {
- int fact;
- int num1;
- printf("\n\n Pointer : Find the factorial of a given number :\n");
- printf("------------------------------------------------------\n");
- printf(" Input a number : ");
- scanf("%d",&num1);
- findFact(num1,&fact);
- printf(" The Factorial of %d is : %d \n\n",num1,fact);
- return 0;
- }
- void findFact(int n,int *f)
- {
- int i;
- *f =1;
- for(i=1;i<=n;i++)
- *f=*f*i;
- }
13.在C中编写一个程序,使用指针来计算字符串中元音和辅音的数量。
测试数据:
输入字符串:string
期待输出:
Number of vowels : 1 Number of constant : 5
C Code:
- #include <stdio.h>
- int main()
- {
- char str1[50];
- char *pt;
- int ctrV,ctrC;
- printf("\n\n Pointer : Count the number of vowels and consonants :\n");
- printf("----------------------------------------------------------\n");
- printf(" Input a string: ");
- fgets(str1, sizeof str1, stdin);
- //assign address of str1 to pt
- pt=str1;
- ctrV=ctrC=0;
- while(*pt!='\0')
- {
- if(*pt=='A' ||*pt=='E' ||*pt=='I' ||*pt=='O' ||*pt=='U' ||*pt=='a' ||*pt=='e' ||*pt=='i' ||*pt=='o' ||*pt=='u')
- ctrV++;
- else
- ctrC++;
- pt++; //pointer is increasing for searching the next character
- }
- printf(" Number of vowels : %d\n Number of constant : %d\n",ctrV,ctrC);
- return 0;
- }
14.在C中编写一个程序,使用指针对数组进行排序。
测试数据:
testdata
期待输出:
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:
- #include <stdio.h>
- void main()
- {
- int *a,i,j,tmp,n;
- printf("\n\n Pointer : Sort an array using pointer :\n");
- printf("--------------------------------------------\n");
- printf(" Input the number of elements to store in the array : ");
- scanf("%d",&n);
- printf(" Input %d number of elements in the array : \n",n);
- for(i=0;i<n;i++)
- {
- printf(" element - %d : ",i+1);
- scanf("%d",a+i);
- }
- for(i=0;i<n;i++)
- {
- for(j=i+1;j<n;j++)
- {
- if( *(a+i) > *(a+j))
- {
- tmp = *(a+i);
- *(a+i) = *(a+j);
- *(a+j) = tmp;
- }
- }
- }
- printf("\n The elements in the array after sorting : \n");
- for(i=0;i<n;i++)
- {
- printf(" element - %d : %d \n",i+1,*(a+i));
- }
- printf("\n");
- }
15.在C中编写一个程序,显示一个函数返回指针。
测试数据:
Input the first number : 5
Input the second number : 6
期待输出:
The number 6 is larger.
C Code:
- #include <stdio.h>
- int* findLarger(int*, int*);
- void main()
- {
- int numa=0;
- int numb=0;
- int *result;
- printf("\n\n Pointer : Show a function returning pointer :\n");
- printf("--------------------------------------------------\n");
- printf(" Input the first number : ");
- scanf("%d", &numa);
- printf(" Input the second number : ");
- scanf("%d", &numb);
- result=findLarger(&numa, &numb);
- printf(" The number %d is larger. \n\n",*result);
- }
- int* findLarger(int *n1, int *n2)
- {
- if(*n1 > *n2)
- return n1;
- else
- return n2;
- }
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:
- #include <stdio.h>
- void main()
- {
- int arr1[10];
- int i,n, sum = 0;
- int *pt;
- printf("\n\n Pointer : Sum of all elements in an array :\n");
- printf("------------------------------------------------\n");
- printf(" Input the number of elements to store in the array (max 10) : ");
- scanf("%d",&n);
- printf(" Input %d number of elements in the array : \n",n);
- for(i=0;i<n;i++)
- {
- printf(" element - %d : ",i+1);
- scanf("%d",&arr1[i]);
- }
- pt = arr1; // pt store the base address of array arr1
- for (i = 0; i < n; i++) {
- sum = sum + *pt;
- pt++;
- }
- printf(" The sum of array is : %d\n\n", sum);
- }
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:
- #include <stdio.h>
- void main()
- {
- int n, i, arr1[15];
- int *pt;
- printf("\n\n Pointer : Print the elements of an array in reverse order :\n");
- printf("----------------------------------------------------------------\n");
- printf(" Input the number of elements to store in the array (max 15) : ");
- scanf("%d",&n);
- pt = &arr1[0]; // pt stores the address of base array arr1
- printf(" Input %d number of elements in the array : \n",n);
- for(i=0;i<n;i++)
- {
- printf(" element - %d : ",i+1);
- scanf("%d",pt);//accept the address of the value
- pt++;
- }
- pt = &arr1[n - 1];
- printf("\n The elements of array in reverse order are :");
- for (i = n; i > 0; i--)
- {
- printf("\n element - %d : %d ", i, *pt);
- pt--;
- }
- printf("\n\n");
- }
18.在C中编写一个程序以显示指针对结构的使用。
期待输出:
John Alter from Court Street
C Code:
- #include <stdio.h>
- struct EmpAddress
- {
- char *ename;
- char stname[20];
- int pincode;
- }
- employee={"John Alter","Court Street \n",654134},*pt=&employee;
- int main()
- {
- printf("\n\n Pointer : Show the usage of pointer to structure :\n");
- printf("--------------------------------------------------------\n");
- printf(" %s from %s \n\n",pt->ename,(*pt).stname);
- return 0;
- }
19.在C中编写一个程序以显示指向union的指针。
期待输出:
Jhon Mc Jhon Mc
C Code:
- #include <stdio.h>
- union empAdd
- {
- char *ename;
- char stname[20];
- int pincode;
- };
- int main()
- {
- printf("\n\n Pointer : Show a pointer to union :\n");
- printf("----------------------------------------\n");
- union empAdd employee,*pt;
- employee.ename="Jhon Mc\0Donald";//assign the string up to null character i.e. '\0'
- pt=&employee;
- printf(" %s %s\n\n",pt->ename,(*pt).ename);
- return 0;
- }
20.在C中编写一个程序,以显示一个指向数组的指针,内容是指向结构的指针。
期待输出:
Exmployee Name : Alex Employee ID : 1002
C Code:
- #include <stdio.h>
- struct employee
- {
- char *empname;
- int empid;
- };
- int main()
- {
- printf("\n\n Pointer : Show a pointer to an array which contents are pointer to structure :\n");
- printf("-----------------------------------------------------------------------------------\n");
- static struct employee emp1={"Jhon",1001},emp2={"Alex",1002},emp3={"Taylor",1003};
- struct employee(*arr[])={&emp1,&emp2,&emp3};
- struct employee(*(*pt)[3])=&arr;
- printf(" Exmployee Name : %s \n",(**(*pt+1)).empname);
- printf("---------------- Explanation --------------------\n");
- printf("(**(*pt+1)).empname\n");
- printf("= (**(*&arr+1)).empname as pt=&arr\n");
- printf("= (**(arr+1)).empname from rule *&pt = pt\n");
- printf("= (*arr[1]).empname from rule *(pt+i) = pt[i]\n");
- printf("= (*&emp2).empname as arr[1] = &emp2\n");
- printf("= e2.empname = Alex from rule *&pt = pt\n\n");
- printf(" Employee ID : %d\n",(*(*pt+1))->empid);
- printf("---------------- Explanation --------------------\n");
- printf("(*(*pt+1))-> empid\n");
- printf("= (**(*pt+1)).empid from rule -> = (*).\n");
- printf("= emp2.empid = 1002\n");
- printf("\n\n");
- return 0;
- }
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:
- #include <stdio.h>
- int main()
- {
- char alph[27];
- int x;
- char *ptr;
- printf("\n\n Pointer : Print all the alphabets:\n");
- printf("----------------------------------------\n");
- ptr = alph;
- for(x=0;x<26;x++)
- {
- *ptr=x+'A';
- ptr++;
- }
- ptr = alph;
- printf(" The Alphabets are : \n");
- for(x=0;x<26;x++)
- {
- printf(" %c ", *ptr);
- ptr++;
- }
- printf("\n\n");
- return(0);
- }
22.在C中编写一个程序,使用指针反向打印一个字符串。
测试数据:
Input a string : w3resource
期待输出:
Reverse of the string is : ecruoser3w
C Code:
- #include <stdio.h>
- int main()
- {
- char str1[50];
- char revstr[50];
- char *stptr = str1;
- char *rvptr = revstr;
- int i=-1;
- printf("\n\n Pointer : Print a string in reverse order :\n");
- printf("------------------------------------------------\n");
- printf(" Input a string : ");
- scanf("%s",str1);
- while(*stptr)
- {
- stptr++;
- i++;
- }
- while(i>=0)
- {
- stptr--;
- *rvptr = *stptr;
- rvptr++;
- --i;
- }
- *rvptr='\0';
- printf(" Reverse of the string is : %s\n\n",revstr);
- return 0;
- }