Passing an array to a function

Source: Internet
Author: User
Pass An array of address forms

The array name corresponds to a constant pointer to the first address of the array. for one-dimensional arrays

When you pass an array of address methods, if there are numbers in the array square brackets in the function declaration, if a positive number is omitted, if negative, then the general function declaration and definition are as follows:

void Modifyarray (int b[], int size);
void Modifyarray (int b[], int size) {
    int i = 0;
    for (; i < size; i++) {
        b[i] = 0;
    }
}

Use as follows:

int array[6];
Modifyarray (array, 6);

The factor group represents the address of the first element of the array, so using an array masterpiece as an argument allows you to pass the starting address of the array to the tuned function. So when the called function modifies an array element in the function body, the array element that is stored in the original storage unit is actually modified. It can also be declared as follows:

void Modifyarray (int *b, int size);
void Modifyarray (int *b, int size) {
    int i = 0;
    for (; i < size; i++) {
        b[i] = 0;
    }
}

You can use the Const type qualifier to prevent the modified function from modifying an array element when it encounters a function that prohibits modifying the value of an array element as follows:

void PrintArray (const int b[], int size);
void PrintArray (const int b[], int size) {
    int i = 0;
    for (; i < size; i++)
        printf ("%d\n", B[i]);
}
Multidimensional Arrays

For a two-dimensional array, the test is as follows:

#include <stdio.h> void func1 (int arr[2][3]);
void Func2 (int arr[1][3]);
void func3 (int arr[][3]);  void Func4 (int arr[2][2]);   note:expected ' Int (*) [2] ' but argument is of type ' int (*) [3] ' void Func5 (int arr[2][]);    Error:array has incomplete element type void Func6 (int arr[][]);
    Error:array has incomplete element type void func1 (int arr[2][3]) {int J, K;
    printf ("function1\n");
        for (j = 0; J < 2; J + +) {for (k = 0; k < 3; k++) {printf ("%d", arr[j][k]);
    } printf ("\ n");
    }} void Func2 (int arr[1][3]) {int J, K;
    printf ("function2\n");
        for (j = 0; J < 2; J + +) {for (k = 0; k < 3; k++) {printf ("%d", arr[j][k]);
    } printf ("\ n");
    }} void Func3 (int arr[][3]) {int J, K;
    printf ("function3\n");
        for (j = 0; J < 2; J + +) {for (k = 0; k < 3; k++) {printf ("%d", arr[j][k]);
    } printf ("\ n"); }} void FUNC4 (int arr[2][2]) {int J, K;
    printf ("function4\n");
        for (j = 0; J < 2; J + +) {for (k = 0; k < 3; k++) {printf ("%d", arr[j][k]);
    } printf ("\ n");
    }} void Func5 (int arr[2][]) {//error:array has incomplete element type int J, K;
    printf ("function5\n");
        for (j = 0; J < 2; J + +) {for (k = 0; k < 3; k++) {printf ("%d", arr[j][k]);
    } printf ("\ n");
    }} void Func6 (int arr[][]) {//error:array has incomplete element type int J, K;
    printf ("function6\n");
        for (j = 0; J < 2; J + +) {for (k = 0; k < 3; k++) {printf ("%d", arr[j][k]);
    } printf ("\ n");
    }} int main (int argc, char const *argv[]) {int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
    Func1 (arr);
    Func2 (arr);
    FUNC3 (arr);      Func4 (arr);      warning:passing argument 1 of ' func4 ' from incompatible pointer type FUNC5 (ARR); Error Func6 (arr);
Error return 0; }

where Func5 and Func6 cannot be compiled, the hint Error:array has incomplete element type.
Func4 only warning, the hint that the incoming data should be int (*) [2] instead of INT (*) [3]. The output is:

1 2 3
3 4 5

The func1,func2,func3 can be output normally.
Test again as follows:

#include <stdio.h> void Func7 (int (*arr) [3]);  void Func8 (int (*arr) [2]);

note:expected ' Int (*) [2] ' but argument is of type ' int (*) [3] ' void func9 (int (*arr) []);
    void Func7 (int (*arr) [3]) {int J, K;
    printf ("function7\n");
        for (j = 0; J < 2; J + +) {for (k = 0; k < 3; k++) {printf ("%d", arr[j][k]);
    } printf ("\ n");
    }} void Func8 (int (*arr) [2]) {int J, K;
    printf ("function8\n");
        for (j = 0; J < 2; J + +) {for (k = 0; k < 3; k++) {printf ("%d", arr[j][k]);
    } printf ("\ n");
    }} void Func9 (int (*arr) []) {int J, K;
    printf ("function9\n");   for (j = 0; J < 2; J + +) {for (k = 0; k < 3; k++) {printf ("%d", arr[j][k]);
    Error:invalid use of the array with unspecified bounds} printf ("\ n");
    }} int main (int argc, char const *argv[]) {int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
    Func7 (arr);   Func8 (arr); //warning:passing argument 1 of ' func8 ' from incompatible pointer type FUNC9 (ARR);
return 0; }

Func7 normal output, FUNC8 output is as follows:

1 2 3
3 4 5

FUNC9 cannot be compiled through.
Test again as follows:

#include <stdio.h> void func10 (int **arr);
void Func11 (int *arr[3]);
void func12 (int *arr[2]);

void func13 (int *arr[]);
    void func10 (int **arr) {int J, K;
    printf ("function10\n");
        for (j = 0; J < 2; J + +) {for (k = 0; k < 3; k++) {printf ("%d", arr[j][k]);
    } printf ("\ n");
    }} void Func11 (int *arr[3]) {int J, K;
    printf ("function11\n");
        for (j = 0; J < 2; J + +) {for (k = 0; k < 3; k++) {printf ("%d", arr[j][k]);
    } printf ("\ n");
    }} void Func12 (int *arr[2]) {int J, K;
    printf ("function12\n");
        for (j = 0; J < 2; J + +) {for (k = 0; k < 3; k++) {printf ("%d", arr[j][k]);
    } printf ("\ n");
    }} void Func13 (int *arr[]) {int J, K;
    printf ("function13\n");
        for (j = 0; J < 2; J + +) {for (k = 0; k < 3; k++) {printf ("%d", arr[j][k]);
    } printf ("\ n"); }} int Main(int argc, char const *argv[])
    {int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
    FUNC10 (arr);
    FUNC11 (arr);
    FUNC12 (arr);
    FUNC13 (arr);
return 0; }

FUNC10,FUNC11,FUNC12,FUNC13 compile-time warning input data types are not the same, run the program will not work properly.

The explanations are as follows:
For a two-dimensional array, arr, which has 2 elements, each element is an array of 3 int, so the type of arr can be seen as pointing to a pointer to the beginning of an array of 3 int elements. So when passing a parameter, the formal parameter should be declared as a pointer to an array that has 3 int elements. This is the following:

void Func7 (int (*arr) [3]);

Equivalent to FUNC1,FUNC2,FUNC3. FUNC1,FUNC2,FUNC3 the number of its front brackets is ignored. Such as:

void func1 (int arr[2][3]);
void Func2 (int arr[1][3]);
void func3 (int arr[][3]);

The parentheses outside the *arr in Func7 cannot be ignored because the priority [] is greater than the priority, and if FUNC11,FUNC12,FUNC13 is represented as an array of pointers of type int *, which is int * *, the same as the FUNC10 parameter type. As follows:

void func10 (int **arr);
void Func11 (int *arr[3]);
void func12 (int *arr[2]);
void func13 (int *arr[]);

For Func5,func6 that cannot be compiled, the compiler prompts for an incomplete element type. Such as:

void Func5 (int arr[2][]);
void Func6 (int arr[][]);

For FUNC9, it does not indicate the bounds of the array being pointed to:

void func9 (int (*arr) []);

But pass the correct first address, if want to use normally, need to func9 its address to int (*) [3] type of pointer (FUNC10 etc. can use as follows), as follows:

void func9 (int (*arr) []) {
    int (*a) [3] = arr;
    Int J, K;
    printf ("function9\n");
    for (j = 0; J < 2; J + +) {
        for (k = 0; k < 3; k++) {
            printf ("%d", a[j][k]);
        }
        printf ("\ n");}
}

Func4,func8 are pointers to int (*) [2], which assigns a pointer to the INT (*) [3] type. As follows:

void Func4 (int arr[2][2]);
void Func8 (int (*arr) [2]);

The compiler first ignores the number in the first [] after arr in the FUNC4 parameter list, and considers arr to be a pointer to an array of elements containing two int types.
For an n-row m-column array of two-dimensional arrays with a single element size of B bytes, the address of its element array[j][k] is calculated as an array + ((M * j) + k) * B, so the number of columns in a two-dimensional array is indispensable when the two-dimensional array is passed.
So for Func4 and Func8, arr is considered a pointer to an array of two elements, so arr[0][2] The calculated element should be 3,arr[1][0] the same calculated element is also 3, the result of the operation is as follows:

1 2 3
3 4 5

Arrays of higher dimensions can be used to determine how to pass in a function based on their address calculation. Passing an array in the form of a value

When structs are passed to a function, they are passed by value, and an array can be passed as a value by using the struct. It is necessary to create a struct that is a member of the array, and then pass the struct as a value, so that the array is passed in the way of passing values. Cases:

#include <stdio.h>

#define SIZE 5

typedef struct
{
    int data[size];
    int size;
} Array;

void Modifyarray (Array);

int main (int argc, char const *argv[])
{
    int i;
    Array array;
    array.size = size;
    for (i = 0; i < SIZE; i++) {
        array.data[i] = i;
    }

    Modifyarray (array);

    for (i = 0; i < SIZE; i++) {
        printf ("%d\n", Array.data[i]);
    }
    return 0;
}

void Modifyarray (Array a) {
    int i;
    for (i = 0; i < a.size; i++) {
        a.data[i] = 1;
    }
}

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.