C language pointer variable as function parameter detailed _c language

Source: Internet
Author: User
Tags array length arrays

In C, the parameters of a function can be either integers, decimals, characters, and other specific data, or pointers to them. Using a pointer variable as a function parameter can pass the address outside of the function to the inside of the function so that the data outside the function can be manipulated within the function, and the data will not be destroyed as the function ends.

arrays, strings, dynamically allocated memory are all collections of data, and there is no way to pass through a single parameter to the inside of a function, but to pass their pointers and influence the data collection within the function through pointers.

Sometimes, for integers, decimals, characters and other basic types of data operations must also rely on pointers, a typical example is the exchange of two variable values.

Some beginners may use the following method to exchange the values of two variables:

#include <stdio.h>
void swap (int a, int b) {
  int temp;//temporary variable
  temp = A;
  A = b;
  b = temp;
}
int main () {
  int a = $, b =;
  Swap (A, b);
  printf ("A =%d, B =%d\n", A, b);
  return 0;
}

Run Result:

A = $, b = 99

As can be seen from the result, the value of a and B has not changed, and the exchange failed. This is because the A, B, and the main () inside of the Swap () function B is different variables, occupy different memory, they have no other relationship except the name, swap () exchange is its internal A, B value, does not affect it outside (main () internal) A, B value.

Using pointer variables as parameters makes it easy to solve the above problem:

#include <stdio.h>
void swap (int *p1, int *p2) {
  int temp;//temporary variable
  temp = *P1;
  *P1 = *P2;
  *P2 = temp;
}
int main () {
  int a = $, b =;
  Swap (&a, &b);
  printf ("A =%d, B =%d\n", A, b);
  return 0;
}

Run Result:

A =, B = 66

When the swap () function is invoked, the address of the variable A and B is assigned to P1, p2, so that *p1, *P2 represents the variable A, b itself, and the value of the Exchange *P1, *P2 is the value of a and B. When the function ends, the P1 and P2 are destroyed, but its effect on external A and B is "persisted" and does not "revert to the original" as the function ends.

Note that temporary variable temp is particularly important because the execution *P1 = *P2, the value of a after the statement is overwritten by the value of B, and cannot be found without first saving A's value.

Using Arrays as Function parameters

An array is a collection of data that cannot be passed through a parameter to a function within a single pass, and you must pass an array pointer if you want to manipulate the array within the function. The following example defines a function, Max (), to find the element with the largest value in the array:

#include <stdio.h>
int max (int *intarr, int len) {
  int i, maxValue = intarr[0];//Suppose the No. 0 element is the maximum for
  (I=1 i <len; i++) {
    if (MaxValue < intarr[i]) {
      maxValue = Intarr[i];
    }
  }
  
  return maxValue;
}
int main () {
  int nums[6], I, maxValue;
  int len = sizeof (nums)/sizeof (int);
  Reads the data entered by the user and assigns the value to the array element for
  (i=0; i<len; i++) {
    scanf ("%d", nums+i);
  printf ("Max value is%d!\n", Max (Nums, Len));
  return 0;
}

Run Result:

12 55 30 8 93 27↙
Max value is 93!

Parameter intarr is just an array pointer, the array length cannot be obtained by this pointer within the function, and the array length must be passed inside the function as a function parameter. Each element of an array nums is an integer, and scanf () asks for the address of the memory in which it was stored when reading the integer entered by the user, Nums+i is the address of the group I element.

When using an array as a function parameter, the parameters can also be given in the form of a "true" array. For example, for the max () function above, its arguments can be written in the following form:

int max (int intarr[6], int len) {
  int i, maxValue = intarr[0];//Suppose the No. 0 element is the maximum for
  (i=1; i<len; i++) {
    if (maxva Lue < Intarr[i]) {
      maxValue = Intarr[i];
    }
  return maxValue;
}

int intarr[6] seems to define an array of 6 elements, and when you call Max (), you can pass all the elements of the array "a brain".

The reader can also omit the length of the array and abbreviate the formal parameter to the following form:

int max (int intarr[], int len) {
  int i, maxValue = intarr[0];//Suppose the No. 0 element is the maximum for
  (i=1; i<len; i++) {
    if (max Value < Intarr[i]) {
      maxValue = Intarr[i];
    }
  }
  return maxValue;
}

int intarr[] Although an array is defined, no array length is specified, as if an array of any length can be accepted.

In fact, both types of array definitions are false, whether int intarr[6] or int intarr[] will not create an array, the compiler will not allocate memory for them, the actual array does not exist, they will eventually be converted to a pointer such as int *intarr. This means that neither form can pass all the elements of an array "a brain", and you have to behave in a way that uses array pointers.

int intarr[6] This form can only explain that the function expects the user to pass an array with 6 elements, and does not mean that there can be only 6 elements in the array, and that the actual passed arrays may have fewer or more than 6 elements.

It should be emphasized that no matter which way the array is passed, the length of the array cannot be evaluated within the function, because Intarr is just a pointer, not a real array, so an extra argument must be added to pass the array length.

Why is the C language not allowed to pass all elements of an array directly, but must pass an array pointer?

The passing of a parameter is essentially a process of assigning a value, which is a copy of the memory. A memory copy is a piece of data that is copied onto another piece of memory.

For basic types of data like int, float, and char, they tend to consume only a few bytes of memory, which are very fast to copy memory. An array is a collection of data, and there is no limit to the number of data. There may be few, or thousands, of memory copies of them may be a long process, seriously slowing the efficiency of the program, in order to prevent poorly skilled programmers write inefficient code, C language does not support the syntax of the data collection directly assigned.

In addition to the C language, C + +, Java, Python and other languages also prohibit the copying of large chunks of memory, at the bottom of the use of similar pointers to implement.

Above on the C language pointer variable as a function of the data collation, follow-up continue to collate relevant knowledge, thank you for your support of this site!

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.