answersLogoWhite

0


Best Answer

The declaration of an array of 3 int pointers will be:

int *p[3];

The declaration of a function that receive an array of int pointers will be:

int my_func(int **p);

or

int my_func(int *p[]);

and you can pass it to other function simply by passing p.

User Avatar

Wiki User

βˆ™ 13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

βˆ™ 14y ago

Arrays are passed as pointers, eg:

void function (int *ip);

int main (void)

{

int n[32];

int *ip = &n[0];

/* these three do the same thing: */

function (&n[0]);

function (n);

function (ip);

}

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 13y ago

You don't pass the entire array. You pass a single value, the address of one of its elements, usually the first element.

int a[] = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10};

This creates an int array of length 10, with initial values as stated. The first element is a[0], having value of 1, and the last element is a[9], having value of 10.

somefunction (int * b);

somefunction(a);

This declares a function that receives an address to an int array. The call passes the address of a[0] to the function. You could just as easily pass some other element, such as a[3], by saying somefunction (&a[3]); or somefunction (a+3); Of course, in this case, you would not want to access b[9], because that would violate the addressability limit of the pointer, but you could certainly access b[3], which would be the same as a[6].

Note, in this example, that I mix array syntax with pointer syntax. In fact, the two are the same, and you can easily inter-mix them. You could just as easily say...

somefunction (int b[]);

somefunction(a);

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 15y ago

eg:

void foo (char par [])

{

...

}

void bar (void)

{

char var [33];

foo (var); /* or: foo (&var[0]); */

}

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 12y ago

type *A[] AS AN ARGUMENT

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you pass an array to a function in C programming?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How is an array name interpretedwhen it is passed to a function?

An array is still an array, regardless of how you pass it into functions. It is still an array-type variable at the beginning of a function. However, the function itself may manipulate the array by "imploding" it into a string with a delimiter, using array values with only specific keys, and such.


How do you write a C Program to fill up an Integer Array?

Reference:cprogramming-bd.com/c_page1.aspx# array programming


What is return type of string in c?

In C programming, a string doesn't have a specific return type as it's essentially an array of characters. So, if a function is returning a string, it should be declared to return a pointer to a char (char*), since a string in C is represented as an array of characters terminated by a null character ('\0').


Why should a function that accepts an array as an argument and processes that array also accept an argument specifying the array?

Basically in c++ passing an array as an argument only provides a pointer to the first value and that function won't know how many values it has.If you read beyond the size you will just get garbage from memory.


How do you pass an array as a parameter?

AnswerUnfortunately your question is to broad. All progamming languages vary in the way things are done. I will just give a general way of doing it.You have to pass the multidimensional array into the function by including it with calling the function. On the receiving end you have to declare another multidimensional array so the information can be passed into it. Depending on the language, you may not be passing in a multidimensional array, instead that array may be stored in an object which you can pass instead.Hope this helps some.-Ashat-in C, when passing two dimensional arrays the compiler needs to know the width so it can calculate memory offsets.passing a 2d array of width 4:voidFunc(type array[][4]);

Related questions

Can you pass addresses to a function in C plus plus?

If the identifier you want to pass is an ordinary identifier, pass it as the address of... function(&identifier); If the identifier you want to pass is an array identifier, pass its name... function(arrayname);


How is an array name interpretedwhen it is passed to a function?

An array is still an array, regardless of how you pass it into functions. It is still an array-type variable at the beginning of a function. However, the function itself may manipulate the array by "imploding" it into a string with a delimiter, using array values with only specific keys, and such.


How are entire arrays passed from one method to another?

In some programming languages, like C, you can pass the new method (or function) an address pointer to the first element in the array. As long as you don't leave the scope of the method the array was created in, the array will remain valid. In other languages that don't support memory addresses, like FORTRAN, it must be done by making the array global.


How do you write a C Program to fill up an Integer Array?

Reference:cprogramming-bd.com/c_page1.aspx# array programming


What is return type of string in c?

In C programming, a string doesn't have a specific return type as it's essentially an array of characters. So, if a function is returning a string, it should be declared to return a pointer to a char (char*), since a string in C is represented as an array of characters terminated by a null character ('\0').


Why should a function that accepts an array as an argument and processes that array also accept an argument specifying the array?

Basically in c++ passing an array as an argument only provides a pointer to the first value and that function won't know how many values it has.If you read beyond the size you will just get garbage from memory.


How do you pass an array as a parameter?

AnswerUnfortunately your question is to broad. All progamming languages vary in the way things are done. I will just give a general way of doing it.You have to pass the multidimensional array into the function by including it with calling the function. On the receiving end you have to declare another multidimensional array so the information can be passed into it. Depending on the language, you may not be passing in a multidimensional array, instead that array may be stored in an object which you can pass instead.Hope this helps some.-Ashat-in C, when passing two dimensional arrays the compiler needs to know the width so it can calculate memory offsets.passing a 2d array of width 4:voidFunc(type array[][4]);


How do you pass a one-dimensional array to user-defined functions?

In C++ you would pass a std::array if the array is fixed-length, otherwise you'd use a std::vector. Most object oriented languages will provide some method of passing a self-contained array object to a function. In C and other non-object oriented languages you would pass a reference or pointer to the start address of the array along with a variable indicating the number of valid elements within the array. The array type will determine the size of each element.


When an array name is passed to function in c?

It 's address is received by the function . that any changes in the value of array elements in the function will result in actual change.


What is d command for squareroot in C programming?

There are no commands in C-programming, you should use function sqrt from math.h


What is an array in C-programming?

A type construction: one or more values with the same type and name.


What is a sizeof function in C programming?

Sizeof is an example.