1. Array as function parameter
In C, an array is a parameter of a function that is degraded to a pointer. when an array is passed as a parameter to a function, it is passed a pointer instead of an array , passing the address of the first element of the array. Here we will explain in the order of shaping variables.
void Sortarray (int a[], int num), and void Sortarray (int a[100], int num) can all be represented by void Sortarray (int *a, int num). In general, if the function parameter is an array, there can be two parameters, one is the name of the arrays, and the other is the array length. For sorting, it is generally necessary to know the address of the first element of the given array, the starting address of the array to be sorted in memory, and the number of data to be sorted.
Here we use a simple bubble sort, the module code is as follows:
/*2016-7-12 Jason Gel */void sortarray (int *a, int num) {int I, J, temp;for (i =0; I <num;i++) //outer: Each time you select an element to be sorted, depending on Times Backward {for (j=i+1; j<num; j + +) //inner: The outer selected element is compared with all subsequent elements to find the smallest element {if (A[i]>a[j]) //Exchange class Code {TEMP = a[i];a[i]= a[j];a[j]=temp;}}}}
2. Parameter Passing
If a is an array, it is clear that we all know how to pass the parameters, the code is as follows:
int num =0; In the 32 machine, tell the C compiler to allocate 4 bytes of memory int a [] = {1,3,5,12,6,7,54,32}; Tells the C compiler to allocate 32 bytes of memory // num = sizeof (a)/sizeof (int); num = sizeof (a)/sizeof (a[0]); printf ("Before sorting:");p Rintarray (a,num); Sortarray (A,num); printf ("After sorting:");p Rintarray (a,num);}
If a is the address of a single shaping variable, can it be similar to using num = sizeof (a)/sizeof (A[0]) to calculate the value of num? such as the following code:
int test =5;int *p = &test;int num1 = 0;num1 = sizeof (P)/sizeof (p[0]); Sortarray (P,NUM1); printf ("After sorting:");p Rintarray (P,NUM1);
is that OK? A lot of people are confused, p[0] is not the first element of the index group value, here p is not an array ah, p[0] all seems not to exist, how can this be used? We must remember that in C language you can use pointers to represent each element of an array, essentially two different symbolic representations of the same object, such as the definition: A[n] equivalent to * (A+n), which is addressed to memory A, then moving n cells, and then removing the array. P[0] is equivalent to * (p+0), and the value is of type int.
Num1=sizeof (P)/sizeof (p[0]) is equivalent to Num1=sizeof (p)/sizeof (int);
This statement does not have any errors on the syntax, but the 32 machine and the 64-bit machine running results are different, the 32 machine has normal results, 64-bit machine error results, the reason is as shown in the end of this article.
3. Declaring array parameterspremise: The actual parameter is an array name. C has the same interpretation for int a [] and int * A,
which is a pointer to int . since the prototype allows omitting names, the following 4 prototypes are equivalent.
/** function prototype declaration 4 equivalent form int sum (int *a, int n) int sum (int *, int) int sum (int a[], int n) int sum (int [], int)
When defining a function, the name is not omitted, and the following two forms are equivalent.
/**
4. The difference between a and &a
int num =0; In the 32 machine, tell the C compiler to allocate 4 bytes of memory int a [] = {1,3,5,12,6,7,54,32}; Tells the C compiler to allocate 32 bytes of memory printf ("a:%d, a+1:%d,&a:%d,&a+1:%d\n", a,a+1,&a,&a+1);//a+1 and &a+1 results are different Although the output results above, A and &a are the same. However, the data type represented by a and &a is not the same as/* Important///A represents the address of the first element of the data (first element), and coincides with the entire array address, but it cannot represent the entire array, only the address of the starting entity//&a represents the address of the entire array (Special note) Its addition 1 is the total number of bytes in the whole array of 1
Outputresults: a:1638176,
a+1:1638180, &a:1638176,
&a+1:1638208
5. The number of bytes that the pointer occupiesThe number of bytes that the pointer occupies is related to the operating system and compiler.
/**2016-7-12 Jason Gel **/#include <stdio.h> #include <stdlib.h> #include <string.h>//void printArray (int * A, int num) and void PrintArray (int a[], int num) are equivalent//function definitions when the name is not omitted. Function prototypes allow the ellipsis of names. /** function prototype declaration 4 equivalent form int sum (int *a, int n) int sum (int *, int) int sum (int a[], int n) int sum (int [], int)//may be slightly Unfamiliar type */void PrintArray (int * A, int num) {int i; for (i = 0; i< num; i++) {printf ("%3d", A[i]);} printf ("\ n");} Here is the bubble sort (bubble sort) void Sortarray (int *a, int num) {int I, J, temp;for (i =0; I <num;i++)//outer: Each time a single element is selected that needs to be sorted (j=i+1; j<num; j + +)//inner layer: The outer selected element is compared with all subsequent elements to find the smallest element {if (A[i]>a[j])//Exchange class Code {TEMP = A[i];a[i]=a[j];a [J]=temp;}}} int main () {int num = 0; In the 32 machine, tell the C compiler to allocate 4 bytes of memory int a [] = {1,3,5,12,6,7,54,32}; Tells the C compiler to allocate 32 bytes of memory int Test =5;int *p = &test;int num1 = 0;num1 = sizeof (P)/sizeof (p[0]);p rintf ("num1:%d, sizeof (p):%d , sizeof (P[0]):%d \ n ", num1,sizeof (p), sizeof (P[0])); Sortarray (P,NUM1); printf ("After a single element is sorted:");p Rintarray (P,NUM1);p rintf ("a:%d, a+1:%d,&a:%d,&a+1:%d\n", a,a+1,&a,&a+1); printf ("sizeof (NUM1):%d\n", sizeof (NUM1)); printf ("sizeof (a):%d\n\n", sizeof (a)); printf ("sizeof (int):%d sizeof (double):%d sizeof (char):%d \ n", sizeof (int), sizeof (double), sizeof (char));p rintf (" sizeof (int *):%d sizeof (double*):%d sizeof (char*):%d \ n ", sizeof (int *), sizeof (double*), sizeof (char*));//a+1 and &a+ 1 results are different//although the output results above, A and &a. However, the data type represented by a and &a is not the same as/* important/The address of the first element of the data represented by the//a (first element), coincident with the entire array address, but it cannot represent the entire array, only the address of the initiating entity//&a represents the address of the entire array (especially special attention) Its plus 1 is the total number of bytes in the whole array of 1//num =sizeof (a);//This is the number of bytes for the entire array, 32 bytes of printf ("argument A's data type is the entire array, the bytes are:%d \ n", sizeof (a));//num = sizeof (a)/sizeof (int); num = sizeof (a)/sizeof (a[0]); Note that the canonical printf ("before sorting:");p Rintarray (a,num); Sortarray (A,num); printf ("After sorting:");p Rintarray (a,num); return 0;}
VC 32-bit compiler runs:
The 64-bit compiler runs:
Core:
It can be seen that in a 64-bit machine, the pointer of int* is 8 bytes, int* in 32 bits is 4 bytes, because:
sizeof (p)/sizeof (p[0])//equivalent to sizeof (int *)/sizeof (int)
So the 64-bit machine should have turned out to be 1 now 2, causing the program to go wrong.
From the output of the program we can see that: the data type of argument A is the entire array, the byte is the total. Although argument a represents an array name, its actual data type is not an int *, but rather the total number of bytes represented by the entire array . Do not confuse this with the previous article A and &a to indicate the address .
C language is worth in-depth knowledge point----array to do function parameters, array name A and &a, array name a "data type"