Listen to Onge teacher Mooc notes (5)--pointers and arrays

Source: Internet
Author: User

What is received in the function if we pass an array to the parameter through the parameters of the function? We know that if a normal variable is passed, then the parameter receives a value, and if a pointer variable is passed, the value is also received by the parameter, except that the value is the address. So what is an array? Pass the array as a value to a function, and in the parameters of the function there is an array variable to receive the array and see what the array variable is receiving. Take Minmax as an example to test the following:

Can I count the number of groups A in the Minmax function? In Minmax and main add the printf function, from the results can be seen, in main a size is 68, and in the Minmax function is the size of the array a 8,8 is what, under 64-bit machine, 8 is exactly the same size as a pointer, and the size of the address is the same.

1#include <stdio.h>2 voidMinmax (intA[],intLenint*max,int*min);3 4 intMainvoid) {5     inta[]={1,2,3,4,5,6,7,8,9, A, -, -, -, -, +, at, -};6     intMin,max;7printf"main sizeofa[]=%lu\n",sizeof(a));//sizeof A in main8Minmax (A,sizeof(a)/sizeof(a[0]),&min,&max);9printf"min=%d,max=%d\n", Min,max);Ten     return 0; One } A  - voidMinmax (intA[],intLenint*min,int*max) { -     inti; theprintf"Minmax sizeofa[]=%lu\n",sizeof(a));//sizeof A in Minmax -*min =*max=a[0]; -      for(i=1; i<len;i++){ -         if(a[i]<*min) { +*min=A[i]; -         } +         if(a[i]>*max) { A*max=A[i]; at         } -     } -}
View Code

And then look at the address of a in main and Minmax respectively, the result address is the same, this shows what, the description in the Minmax of this array is main in this array, they are the same. Again, for example, in Minmax to modify the value of a[0], and then in main after calling the Minmax function output A[0] value, found that the actual result has indeed changed.

Minmax parameter int a[] is the pointer, then why this parameter must leave an empty square bracket, why the square brackets to write the number is useless, why in the Minmax sizeof no way to get the number of this array element, because it is actually a pointer, It just looks like an array, so since it's actually a pointer, we write it like a pointer line, a[] written *a, and found no change in editing and running results. So we can say that there is a connection between the array and the pointer: the array in the function parameter is actually a pointer, that is, sizeof (a) = Siziof (int*), but for such a pointer you can use the square brackets [] operator of the array to perform the operation. So the prototypes of the following four functions are equivalent, not the equivalent of their type, but rather that they are equivalent as they appear in the function parameter table, and they are equivalent as function prototypes.

1 int sum (int *a,int  n); 2 int sum (int *,int); 3 int sum (int a[],int  n); 4 int sum (int [],int);
View Code

In fact, the array variable is a special pointer, because the array variable itself is the expression of the address, in the Fetch address & when the experiment, directly take the name of the array to get the address of the array, the array name itself is the address, so the int a[10];int *p=a, do not need to use the address character & , but each element of the array is expressed as a single variable, such as A[0],a[1] is a single variable, so the address of a single element is required to use the & symbol, a==&a[0] This thing has been seen in the address.

[] This operator can do the array, or the pointer do, p[0] equivalent to *P, do experimental testing, in the Minmax code of the main function to add the following code, the output results show *p=p[0].

1     int *p =&min; 2     printf ("*p=%d\n", *p); 3     printf ("p[0]=%d\n", p[0]);
View Code

P[0] What do you mean, I think P refers to the place is the number of words, p[0] is the first element of the array p. So actually we do not have an array, there is a variable called min, Min is stored in 2,p point min, then *p is the value of the variable referred to, p[0] is the point where P refers to an array, now it is not an array is just a variable, you can think of it as an array, as an int MIN[1] See, as min[1], what is its valid array? Min[0]. Of course, for the normal variable can not be written, but for the pointer variable can be written, that is, where it refers to the first element is taken out as a p[0].

Similarly, the * operator we know is to do with the pointer, is to take out the point where the pointer refers to the value, or the array can be done. If there is an array can also say *a=25, we test this thing, the result is *a for 1120, visible *a readable can be written, in short, can be used as pointers.

The array variable is a const pointer, as mentioned in the previous array, if the definition of two arrays, int a[],int b[], so that b=a is not possible, between the array can not be assigned value. So when the transfer if int *q=a, this is possible, one can not be what is the difference that? In fact, we say that int b[] can be regarded as int * Const B,CONST meaning B is a constant, can not be changed, it is this array can not be another array, so the array is a constant pointer, can not be assigned value, can not represent something else.

Listen to Onge teacher Mooc notes (5)--pointers and arrays

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.