The technical deduction of function parameter degradation as pointer in C language array

Source: Internet
Author: User

//the technical deduction of the function parameter degradation of the array as a pointer#include <stdio.h>#include<stdlib.h>#include<string.h>//a technique deduction of function parameter degradation as pointer in one-dimensional arrayvoidPRINTFA (Char* strarr[3]);//in computers, arrays are stored linearly, and two-dimensional array elements are arranged in a//For example: 1,2,3,4,5,6,7,8,9 like this set of data we can think of as a one-dimensional array int a[9]={1,2,3,4,5,6,7,8,9};//It can also be considered a two-dimensional array int b[3][3]={1,2,3,4,5,6,7,8,9};//so the computer doesn't know what the step size of the array name is, how many bytes a+1 move or how many bytes the b+1 moves.//This requires the programmer to tell the computer what the step size of the array name is.//for the subject void PRINTFA (char * strarr[3]); Array to do function parameters The array is a one-dimensional array array element type is char *//then the size of the array strarr should be sizeof (char *) or 4 bytes//so we just have to tell the computer that you jump 4 bytes and it's OK.//so technical deduction for PRINTFBvoidPRINTFB (Char*strarr[]);//because the computer doesn't care how many of your elements are 3 or 30 and the computer doesn't matter what the programmer needs to be concerned about (this is the array cross-border problem)//function Parameter char * strarr[] also tell the computer I am a one-dimensional array array element is a char * type//so the size of the array strarr is either sizeof (char *) or 4 bytes//then we continue to deduce that since the computer only needs to determine the step size of each move of the array is 4 bytes .//then void printfc (char * * strarr); it is also possible to write Strarr is a pointer to a variable in the Strarr of a type that is char *//The step is only related to the value of the pointer, so the Strarr step is sizeof (char *) or 4 bytes//so the C language developer has done the optimization printfc (and I have no problem designing C language is so optimized)voidPRINTFC (Char**Strarr);//char * strarr[3] has 2 benefits for parameter degradation to char * * Strarr)//Benefit 1: Copying a one-dimensional array char * strarr[3] than copying a pointer char * * Strarr will consume more memory space//char * strarr[3] requires sizeof (char *) * 3 = 12 bytes of memory space;//while char * * * * * * Strarr requires sizeof (char * *) = 4 bytes of memory space;//savings in memory and resource consumption when creating arrays//Benefit 2: Reduced useless parsing, 3 useless for char * strarr[3] elements//This is a one-dimensional array this information is useless, because when traversing the array from the beginning of the first address to traverse, just give the computer a first address on the line//the computer traverses backwards from the first address without knowing what he is, just know how much each step is .//the technical deduction of the function parameter degradation as a pointer in a two-dimensional arrayvoidPRINTFD (intarr[3][4]);//for a two-dimensional array, the computer also needs to know the array name Arr's step is the number of bytes to be moved every time the computer is traversed.//so first we should determine the array name what type of arr is//The array name is a pointer to the first element of the array (my own inference) then the two-dimensional array can be imagined as a one-dimensional array, except that each element of this one-dimensional array is special, or a one-dimensional array//so according to the inferred array name, the type of arr is a one-dimensional array of pointers to a one-dimensional array that defines typedef int MYARR[4];//The definition of a one-dimensional array pointer is such a typedef int (* Pmyarr) [4];//a one-dimensional array pointer defines a simplified version of the INT (* Pmyarr) that is so defined [4];//so the type of the array name arr is int (* Pmyarr) [4]; However, the step size of the array name arr needs to be determined based on the value of the array name arr.//The value of the array name arr is the first address of a one-dimensional array type that is typedef int MYARR[4], and the size of typedef int MYARR[4] is sizeof (int) *4=20 bytes//the step size for the array name arr is 20 bytes//The deduction of a one-dimensional array indicates that an array element is not important to a computer two-bit array elements can be thought of as one-dimensional arrays//Push performance void printfd (int arr[][4]);voidPRINTFE (intarr[][4]);//and because the computer only needs to know the first address and the stride size, can use INT (*p) [4] instead of int arr[][4]voidPRINTFF (int(*arr) [4]);//combined with the above analysis, we deduced that the function parameter of the array is degenerate into a pointer, and the type of the pointer is the type of the array name voidMain () {Char* strarr[3] = {"123","456","789"}; intarr[3][4] = {0 }; System ("Pause");}

The technical deduction of function parameter degradation as pointer in C language array

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.