The Difference between Array Name and Pointer in C Language and a Simple Bubble Sort

Reviewing the old ancestor C, I wrote a simple bubble sort to arrange the elements in the integer array from small to large, but there was a small problem. After consulting the data, we found that the array name is quite interesting.

cat

Problem recurrence

Usually, the array name is used as a special pointer, so it was first written like this.

 int * maoPao(int *a); int main() { int a[5] = {21,3,12,9,5}; int *p = maoPao(a); printf("%d %d %d %d %d",p[0],p[1],p[2],p[3],p[4]); } int * maoPao(int *p) { int len = sizeof(p)/sizeof(p[0]); Printf ("Number of array elements% d  r  n", len); //Bubble sort for(int i = 0; i < len - 1; i++) { int flag = 1; for(int j = 0 ; j < len - 1 -i;j++) { if(p[j] > p[j+1]) { int t = p[j]; p[j] = p[j+1]; p[j+1] = t; flag = 0; } } if(flag) { break; } } //Void can also be used, because the operations are all pointer related, and the value of array a has changed. return p; }

Operation results:

 Number of array elements 2 3 21 12 9 5

That's not right. There are five array elements. How can they become two? The result of bubbling is also incorrect.

Then try to output sizeof (p) and sizeof (p [0]) successively. The running result is 8 4

How can an array become a common pointer variable?

solve the problem

After investigation, it is said that when an array name is used as a function parameter, it loses its meaning in the function body and is only a pointer. At the same time, it also loses its constant property, which can be self increasing, self decreasing, etc., and can be modified.

Three conclusions about the nature of array names:
1. The connotation of array name is that it is a data structure with only entities, and this data structure is an array.
2. The extension of array name is that it can be converted into a pointer to the entity it refers to, and is a pointer constant.
3. The pointer to the array is another variable type, pointer variable, which only means the storage address of the array.

Therefore, the method of getting the number of array elements by sizeof division is normal in the main function, but if the formal parameter is passed to other function bodies, the formal parameter here is just a common pointer variable, and the result is no longer correct.

Therefore, the array length is passed to the processing function together with the array as a parameter.

The processing function is modified as:

 int * maoPao(int *p,int arrLen) { int len = arrLen; Printf ("Number of array elements% d  r  n", len); //Bubble sort for(int i = 0; i < len - 1; i++) { int flag = 1; for(int j = 0 ; j < len - 1 -i;j++) { if(p[j] > p[j+1]) { int t = p[j]; p[j] = p[j+1]; p[j+1] = t; flag = 0; } } if(flag) { break; } } //Void can also be used, because the operations are all pointer related, and the value of array a has changed. return p; }

Modify the function call and declaration.
Operation results:

 Number of array elements 5 3 5 9 12 21

I am used to using Java C # and so on, but I almost forget about the ancestor of C.

References

  1. [Website] Blog Park The Difference between Array Name and Pointer in C Language
Zimiao haunting blog (azimiao. com) All rights reserved. Please note the link when reprinting: https://www.azimiao.com/2219.html
Welcome to the Zimiao haunting blog exchange group: three hundred and thirteen million seven hundred and thirty-two thousand

Comment

*

*

Comment area

  1. C # does not include some contents of the c language, will it be used in C #?

    • hare 07-20 17:11 reply

      Machine code generates assembly, assembly generates C, and C generates everything.

  2. c0smxsec 08-02 21:31 reply

    Follow Big Brother C

  3. Delight 08-08 13:22 reply

    Only c++'s passing meow refueling

  4. Time House 08-26 08:46 reply

    233333, your name is very 6, maoPao, a bit anti X-man X-type (face covered)