In the C language, pointers are very powerful tools. The flexible use of pointers can make program writing more efficient. Pointers can either pass values or deliver addresses. It is the flexibility of the pointer that makes it easy for a novice like a blogger to make mistakes and cause the program to crash.
Therefore, it is important to understand the use of pointers and the way in which he is called in the function, which is the goal that bloggers are working on. According to the study in recent days, bloggers are here to share a few small examples of the use of pointers, we learn together.
1. Type conversion (ITOA)
#include <stdio.h> #include <string.h> #include <assert.h>void swap (char *a , char *b) { int tmp = *a; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//uses pointers to implement the exchange of two arguments, defining a temporary variable to hold the value of the parameter *a = *b; //*a and *b point to the address of the argument *b = tmp;} Void reverse (char *pstart, char *pend) { //The order is reversed when the number is in the array, this assert (pstart != null & & pend != null); The //function flips the reverse character while (pstart < pend) { //swap (Pstart, pend);p start++;p end--;}} Char *my_itoa (int n, char *s) { char *pret = s; //conversion function assert (s != NULL); ///Assertion: A debug mode if (S != null) {if (n < 0) { //the processing of negative numbers in *s = '-'; n = n * ( -1); s++;} while (n ) {*s = n % 10 + ' 0 '; //remove each digit of the number n /= 10; //die in addition to s++;} *s = '; if (*pret == '-') //Negative reverse (pret+1, Pret+strlen (pret)-1); ELSE&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp; // Positive reverse (Pret,pret + strlen (pret )-1); Return pret;} Return null;} Int main () {int num = 0;char arr[20];p rintf ("Please enter number: \ n"); scanf_s ("%d", &num); Char *pret = my_itoa (num,arr );p rintf ("%s\n", pret); return 0;}
2. Type conversion (atoi)
#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h> #include <string.h>int my_atoi ( Char a[]) {char *p = a;int flag = 1;int ret = 0;while ( Isspace (*p)) { //isspace function can determine the null character in a string p++;} if (*p == '-') //determines if it is a negative flag = -1;if (*p == ' + ' | | *p == '-') { //skips sign bit p++;} while (*p != ') {ret = ret * 10 + *p - ' 0 '; //minus character 0 (ASCII value 48) to get true numeric p++;} return flag * ret ; //implementation of negative output}int main () { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;INT&NBSP;ARR[10];p rintf ("Please enter number: \ n"); gets (arr); int ret = my_atoi (arr);p rintf ("%d\n", ret); return 0;}
3. Find the number of characters in a string
#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h> #include <ctype.h>int main () { char a[20]; int i = 0; int j = 0; int k = 0; char *p; //can also initialize a pointer when it is defined char *p =a; printf ("Please enter string: \ n"); gets (a); p = a; //pointer p points to the first address of array a while (*p != ') {//string does not end if ((*p >= 48) && (*p <= 57)) { //detection of logarithmic characters i++;} else if (isspace (*p )) {//detection of NULL characters j++; }else //detection of other characters k++;p ++; The //pointer points to the next character}printf ("The number appears%d times, the white space character appears%d times, the other characters appear%d times \ n", i, j, k); return 0;}
I hope we can give suggestions and make progress together!
This article is from the "Molova" blog, make sure to keep this source http://molova.blog.51cto.com/10594266/1686254
C Language: The use of pointers