Pointer exercises in C Language

Source: Internet
Author: User
/*************************************** ******************************** // 01. C * // * compare the size with the pointer pointing to the function *//************************* **************************************** * ******/# include <stdio. h> int min (int A, int B) {If (A> B) return B; elsereturn A;} int main () {int (* p) (INT, INT); // pointer to the function variable P, the parameter must specify the type, otherwise the error P = min; // P points to the min function int A, B, RE; printf ("enter two integers: \ n"); scanf ("% d", & A, & B); Re = (* p) (A, B ); printf ("min = % d \ n", RE); return 0 ;} /*************************************** * ******************************** // 02. C * // * use the function that returns the pointer to find the maximum value *//************************* **************************************** * ******/# include <stdio. h> # define arraysize 10int * findmax (int * P, int N) {int I, * max; max = P; // Max points to the first address of the input array & A [0] for (I = 0; I <n; I ++) if (* (p + I)> * max) max = P + I; return Max;} int main () {int A [arraysize]; int * Re; int I; printf (" Enter % d integers \ n ", arraysize); for (I = 0; I <arraysize; I ++) scanf (" % d ", & A [I]); re = findmax (A, arraysize); printf ("maxnum = % d \ n", * Re); Return 0 ;} /*************************************** ******************************** // 03. C * // * use pointers to store array element values in reverse order *//************************ **************************************** * *******/# include <stdio. h> # define arraysize 10 void invert (int * a, int N) {int * First, * Last, * mid; int temp; First = A; // The first address of the array mid = a + (n-1)/2; // point to the intermediate element last = a + n-1; // The end address of the array (; first <= mid; first ++, last --) {temp = * First; * First = * last; * Last = temp;} int main () {int A [arraysize]; int I; printf ("Enter % d integer \ n", arraysize); for (I = 0; I <arraysize; I ++) {scanf ("% d", & A [I]) ;}invert (A, arraysize); for (I = 0; I <arraysize; I ++) printf ("% d", a [I]); Return 0 ;} /*************************************** *********************************//* 04. C * // * use a pointer to implement string matching *//*************************** **************************************** * *****/# include <stdio. h> # include <string. h> // stra is the string to be matched, and strb is the string to be matched. // find straint match (char * strb, char * stra) in strb) {int I, j, startpos = 0; // int Lena = strlen (stra)-1; int lenb = strlen (strb)-1; int endmatch = Lena; // compare whether the last character of stra is the same as the character in strb. If the character is the same, compare whether stra and strb match for (j = 0; endmatch <= lenb; endmatch + +, Startpos ++) {If (strb [endmatch] = stra [Lena]) for (j = 0, I = startpos; j <Lena & strb [I] = stra [J];) {I ++; j ++;} If (j = Lena) // matched {return (startpos + 1) ;}} if (endmatch> lenb) {printf ("not matchable \ n"); Return-1;} return 0 ;} int main () {char a [] = "abaabcac"; char B [] = "acabaabcaabaabcac"; int P = match (B, A); If (P! =-1) {printf ("matchable \ n"); printf ("the start position is % d \ n", P);} return 0 ;} /*************************************** * ******************************** // * 05. C * // * use a pointer to find the maximum element, and output the subscript of the array where the maximum element is located *//***************************** **************************************** * **/# include <stdio. h> # define arraysize 10int * findmax (int * a, int N, Int & script) {int * max; max = A; int I; for (I = 0; I <n; I ++) {If (* (a + I)> * max) {max = (a + I); script = I ;}} return Max ;} int main () {int * max; int A [arraysize]; int I, SCR; printf ("Enter % d integers:", arraysize); for (I = 0; I <arraysize; I ++) {scanf ("% d", & A [I]);} max = findmax (A, arraysize, Scr ); printf ("Max num is: A [% d] = % d \ n", Scr, * max); return 0 ;} /*************************************** * ******************************** // 06. C * // * Find the same element in two arrays *//************************** **************************************** * *****/# include <stdio. h> # define arraysizea 5 # define arraysizeb 4 // sort the array void sort (int * a, int N) {int I, j; int temp; for (I = 0; I <n-1; I ++) for (j = I + 1; j <n; j ++) {If (* (a + I)> * (a + J) {temp = * (a + I); * (a + I) = * (a + J); * (a + J) = temp ;}}// find the same element int * Find (int * a, int * B, int an, int bn) {int * PTA, * PTB; PTA = A; PTB = B; while (PTA <(a + an) & PTB <(B + bn) {If (* PTA <* PTB) PTA ++; else if (* PTA> * PTB) PTB ++; else // equal return PTA;} return 0 ;}int main () {int I; int * Re; int A [arraysizea], B [arraysizeb]; printf ("Enter % d integers in array A:", arraysizea); for (I = 0; I <arraysizea; I ++) scanf ("% d", & A [I]); printf ("Enter % d integers in array B:", arraysizeb ); for (I = 0; I <arraysizeb; I ++) scanf ("% d", & B [I]); sort (A, arraysizea); sort (B, arraysizeb); Re = find (a, B, arraysizea, arraysizeb); printf ("same num is % d \ n", * Re); Return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.