1

I have an array declared in my main() function, and I need to access it in a function that is being called from main(). How do I make the array available to the function? And what is the best way to do this? I tried to use the static keyword, but that did not change anything. Should I declare the array outside of main() as well as the function being called?

1
  • 11
    pass it as argument. Commented Aug 3, 2018 at 22:03

2 Answers 2

3

How do I make the array available to the function?

In C, you can pass variables like arrays to functions by putting the name of the variable in the parentheses of the function you're calling.

And what is the best way to do this?

Let's say you had the following code:

void INeedNums(int nums) {
    // Do something with them nums.
}
int main() {
    int aBunchofNums[] = {1,2,3,4};
    INeedNums(aBunchofNums);
}

This code would send the entire array of {1,2,3,4} to the function for it to work with.

Should I declare the array outside of main() as well as the function being called?

You can declare it outside of the main() function, making it an external variable. External variables can be accessed by any function.

The code above sends the array to a function, where the function can use it. Notice how that function establishes a new variable called int nums in order to handle it, that means it's not using the same exact variable, which means that when you edit the array within that second function, it does not change the array that resides in main() that you sent to it. There are several methods to changing this, namely using pointers, like in P__J__'s answer.

Sign up to request clarification or add additional context in comments.

Comments

1

You can pass it as the argument to the other function, or may declare it global.

Examples - and exercise to understand the scopes of the variables:

int array[100]

int add(size_t first, size_t second, size_t result )
{
    array[result] = array[first] + array[second];
    return array[result];
}

int add1(int *array, size_t first, size_t second, size_t result )
{
    array[result] = array[first] + array[second];
    return array[result];
}


void foo1(void)
{
    int array[100];

    add1(array, 5, 7, 9);
    add(5, 7, 9);
}

4 Comments

You may explain what variable shadowing is if this isn't meant as an exercise for the reader :) This might be nitpicking but size_t is not defined. Include for example stddef.h
@Inrin size_t is a standard type and the only correct for the table indexes. It is logical that you have to include the appropriate header files.
@P__J__ - the need to include appropriate header files may be logical to you. It is rarely logical to beginners to C, particularly those who ask basic questions like this. And, size_t is not the only correct type to use for table indices.
I agree with the others that you should include appropriate header files explicitly in an answer pitched at learners; also, maybe you shouldn't suggest using global variables to beginners, or at least mention that it is bad practice to use globals when better solutions exist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.