answersLogoWhite

0


Best Answer

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.

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

8y ago

In C, all array names will implicitly convert to a memory address at the slightest provocation. Thus when we pass an array name to a function, the value we actually pass is the start address of the array. So in order for a function to accept an array, its formal argument must be a pointer variable of the appropriate type. The address we pass is then copy-assigned to this pointer when we invoke the function.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

The address of the first element.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How is an array name interpretedwhen it is passed to a function?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What happen when a c program passes an array as a function argument?

When an array name is passed as a function argument, the address of the first element is passed to the function. In a way, this is implicit call by reference. The receiving function can treat that address as a pointer, or as an array name, and it can manipulate the actual calling argument if desired.


When is memory allocated when declaring an array in C plus plus?

It depends how the array is declared (fixed size or variable size) and where it is declared (global scope or local scope). If the array is declared in global scope (outside a function) and is fixed size, it will be allocated in static memory. If it is variable size, the pointer is stored in static memory while the array itself is allocated on the heap. The pointer in static memory points to the start address of the array in heap memory. If the array is declared in local scope (inside a function) and is fixed size, it will be allocated on the stack in whichever thread the function was called. If it is variable size, the local pointer is stored on the stack while the array is allocated on the heap. The pointer will fall from scope when the function returns so the array must not be allowed to outlive the function in which the pointer is declared. If the array must outlive the function that allocates the array, the pointer must be declared at a higher scope in the call stack and must be passed by reference to or returned by value from the function that allocates the array. If you provide your own memory manager, however, an array may be allocated wherever the memory manager's memory pool is allocated, be it in static memory, the stack or the heap. A memory manager essentially allocates an array of bytes which you can then utilise as you see fit (the array of bytes will be allocated as per the previous description for arrays in general).


The most common function of an array is to store variables however there will be times when an array is used to store objects. briefly describe four such objects?

Any name can be placed in an array, be it a primitive name or an object name, even a function pointer. Four examples of common objects that may be placed in an array include any object defined in the STL (standard template library), which includes vectors, lists, iterators and maps. All are self-explanatory, although a vector is simply an array implemented as an object. Therefore an array of vectors is nothing more than an array of arrays (effectively a multi-dimensional array). However, vectors are the "correct" way of implementing arrays in C++ (unless you specifically wish to use C-style code in your C++ projects), thus a multi-dimensional array is best implemented as a vector of vectors.


How are the strings passed to a function?

By reference. The name of the string is converted to a pointer (in C/C++) and given to the function as the address of the first element. (In Java, all objects are passed by reference, and there are no pointers.)


How do you use qsort function to sort an array of structures?

It depends what language you are using and how the function is implemented. However, generally, you need to pass 4 arguments to the function: 1. A reference to the array. 2. The lower bound of the sub-array to be sorted (usually 0). 3. The upper bound of the sub-array to be sorted (usually n-1 for an array of n elements). 4. A binary predicate to perform comparisons between the elements (usually a less-than predicate).

Related questions

How can and array be passed to a funaction?

try this: <function-name> ( <array-name> )


What happen when a c program passes an array as a function argument?

When an array name is passed as a function argument, the address of the first element is passed to the function. In a way, this is implicit call by reference. The receiving function can treat that address as a pointer, or as an array name, and it can manipulate the actual calling argument if desired.


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 signifience the name of an array?

The name of an array serves as a reference to the start address of the array and thus to the first element of the array. If the array is fixed length and within the scope of its declaration, the compiler can determine its length from the name alone. However, when an array name is passed to a function, it implicitly converts to a pointer and the size information is lost. thus the size must be passed as a separate argument. The only general purpose exceptions supported by the standard library are null-terminated character arrays (C-style strings) and null-terminated arrays of C-style strings (terminated by a double-null).


When is memory allocated when declaring an array in C plus plus?

It depends how the array is declared (fixed size or variable size) and where it is declared (global scope or local scope). If the array is declared in global scope (outside a function) and is fixed size, it will be allocated in static memory. If it is variable size, the pointer is stored in static memory while the array itself is allocated on the heap. The pointer in static memory points to the start address of the array in heap memory. If the array is declared in local scope (inside a function) and is fixed size, it will be allocated on the stack in whichever thread the function was called. If it is variable size, the local pointer is stored on the stack while the array is allocated on the heap. The pointer will fall from scope when the function returns so the array must not be allowed to outlive the function in which the pointer is declared. If the array must outlive the function that allocates the array, the pointer must be declared at a higher scope in the call stack and must be passed by reference to or returned by value from the function that allocates the array. If you provide your own memory manager, however, an array may be allocated wherever the memory manager's memory pool is allocated, be it in static memory, the stack or the heap. A memory manager essentially allocates an array of bytes which you can then utilise as you see fit (the array of bytes will be allocated as per the previous description for arrays in general).


The most common function of an array is to store variables however there will be times when an array is used to store objects. briefly describe four such objects?

Any name can be placed in an array, be it a primitive name or an object name, even a function pointer. Four examples of common objects that may be placed in an array include any object defined in the STL (standard template library), which includes vectors, lists, iterators and maps. All are self-explanatory, although a vector is simply an array implemented as an object. Therefore an array of vectors is nothing more than an array of arrays (effectively a multi-dimensional array). However, vectors are the "correct" way of implementing arrays in C++ (unless you specifically wish to use C-style code in your C++ projects), thus a multi-dimensional array is best implemented as a vector of vectors.


When does an array behave like a pointer in C plus plus -- provide sample code to support each case?

An array never behaves like a pointer.Understand that a pointer is a variable -- no different to any other variable, other than its type. like any other variable, it is allocated its own memory (4 bytes in a 32-bit system). Those 4 bytes can store any 32-bit value, from 0x00000000 to 0xFFFFFFFF, but because it is declared to be a pointer, that value actually represents a memory location, somewhere within the 4GB address space. 0x00000000 is a reserved memory location -- what we refer to as NULL.An array is not the same as a pointer in any sense. The array name refers to the actual memory (the starting address) where the array resides. But the array name is not a variable -- unlike a pointer, it is not separate from the memory it refers to -- it is merely an alias that refers to the memory allocated to the array itself.That said, pointers and arrays can appear to be the same. For instance, the strlen() function expects you to pass a const char pointer, and yet you can pass an array name instead and it works just fine:char cArray[12];char * p = cArray;strlen( p ); // Pass a pointerstrlen( &cArray[0] ); // Pass pointer to array element via AddressOf operatorstrlen( cArray ); // Pass an array nameAll three of these functions work exactly the same so it would be easy to assume the array is a type of pointer. But it is not. To understand what's really going on here you have to understand how pointers and arrays are actually passed to function.When you pass a pointer to a function, you don't pass the pointer itself, you pass the memory address stored in the memory allocated to the pointer. In other words, pointers are passed by value, not by reference. Similarly, when you pass an array name, you pass the starting address of the array.That's all there is to it! The function's parameter, a pointer, is allocated its own memory. It is a temporary variable, local to the function. All it expects is a memory location so whether you pass a pointer or an array name, that's exactly what it receives.


How are the strings passed to a function?

By reference. The name of the string is converted to a pointer (in C/C++) and given to the function as the address of the first element. (In Java, all objects are passed by reference, and there are no pointers.)


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);


Passing an array name to a pointer assigns the first memory location of the array to the pointer variable?

Yes, passing an array name to a pointer assigns the first memory location of the array to the pointer variable. An array name is the same as a pointer to the first location of the array, with the exception that an array name is a r-value, while a pointer is an l-value.


How do you use qsort function to sort an array of structures?

It depends what language you are using and how the function is implemented. However, generally, you need to pass 4 arguments to the function: 1. A reference to the array. 2. The lower bound of the sub-array to be sorted (usually 0). 3. The upper bound of the sub-array to be sorted (usually n-1 for an array of n elements). 4. A binary predicate to perform comparisons between the elements (usually a less-than predicate).


An array is a list of data items that all have the same type and the same name?

An array element has the same type as the array name.