Ask Your Question
4

How can an array be passed as an argument to a function in C Programming?

asked 2023-07-10 13:09:01 +0000

devzero gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-07-10 13:27:02 +0000

djk gravatar image

In C Programming, an array can be passed as an argument to a function by specifying the array name without square brackets, along with its size (if known), in the function parameter list. The following is an example of this:

void myFunction(int arr[], int size) {
  // function code
}

In this example, the myFunction function takes an array of integers arr and its size size as arguments. Within the function, the elements of the array can be accessed using array indexing (arr[0], arr[1], etc.).

When calling the function, the array name is also passed without square brackets. For example:

int main() {
  int myArray[5] = {1, 2, 3, 4, 5};

  myFunction(myArray, 5);

  return 0;
}

In this example, the myArray array is passed to myFunction, along with its size (5).

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-10 13:09:01 +0000

Seen: 12 times

Last updated: Jul 10 '23