简体   繁体   中英

How to pass the address of an array to a function?

I just wanted to ask on how to pass the address of an array to a function.

void me( int *a ) {
    printf("%d\n", *a); // that gives me 11
}

int main () {
    int x[3]  = { 11, 12, 13 };

    me(&x[0]);

return 0;
}

But what if I want to pass the whole array not by repeating the me(&x[//the position I want to pass]);

Is it possible?

All other answers are good, only a thing to add, form my point of you.

You shoul add a parameter to your function to be able to use it with different arrays with different lengths, so pass the length of array to me function

#include <stdio.h>

void me( int *a, size_t length ) {
    size_t i;
    for (i=0; i<length; i++)
       printf("Element %zu is %d\n", i, a[i]);
}

int main () {

    int x[3]  = { 11, 12, 13 };

    me(x, sizeof(x)/sizeof(x[0]));

    return 0;
}

As you can see I used sizeof which returns the bytes length of the array. So dividing it with the size in bytes of a single element of the array. The result is the number of elements of the array.

But you are passing the "whole array" to the function. By passing a pointer to the first element you are in fact passing a pointer to the rest of the elements too.

To print out the second element of the array, modify the function such as this:

void me( int *a ) {
    printf("%d\n", a[1]); // prints the second element in a
}

This works because for any array or pointer a and index i the expression a[i] is the same as *(a + i) .

Also note that simply passing x is the same as passing &x[0] , as arrays naturally decays to a pointer to its first element. So the call could just be

me(x);  // Same as me(&x[0])

Address of the array is same as the address of the first element. To print the rest of the elements just increment the pointer like below.

#include <stdio.h>

void me( int *a ) {
    printf("%d\n", *a); // that gives me 11
    printf("%d\n", *(a+1)); // will give you 12
    printf("%d\n", *(a+2)); // will give you 13
}

int main () {

    int x[3]  = { 11, 12, 13 };

    me(&x[0]);

return 0;
}

when you pass array name to a function , It will automatically pass the address of the first element of the array.so we can loop the address and access other elements also.You can try this.

#include<stdio.h>

void me( int *a) {
    int i;
    for(i=0;i<3;i++){
        printf("%d\n",*(a+i));
    }
}

int main () {

    int x[3]  = { 11, 12, 13 };

    me(x);

return 0;
}

output

11
12
13

Yes, You can pass entire array to function. So,

 me(x); //Pass only name of array

Array hold the starting address of elements means x[0] address.

#include<stdio.h>

void me( int *a) {
    int i=0;
    while (i<3)
    {
        printf("%d\n",*(a+i));
        i++;
    }
}

int main () {

    int x[3]  = { 11, 12, 13 };

    me(x);

return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM