PDA

View Full Version : functions and arrays.


okkvlt
Sep16-09, 01:03 PM
How do i create an array such that there is more than one index? the array declared here
float array[i];
is like a vector
but how do i create an array that is declared such as
float array[i,j]
which would be a matrix

And how do i create a c function whose input is an array and the output is an array?

Mark44
Sep16-09, 05:56 PM
How do i create an array such that there is more than one index? the array declared here
float array[i];
is like a vector
but how do i create an array that is declared such as
float array[i,j]
which would be a matrix


float array [3, 4];

The line above declares a two-dimensional array with 12 elements, indexed from array[0,0] through array[2, 3].


And how do i create a c function whose input is an array and the output is an array?
You can create a C function that has one or more parameters that are arrays, but C doesn't allow a function to return an array. You can however return a pointer to an array.

Here is an example function definition that has an array parameter.

void print_line(char[] line)
{
printf("%s", line);
}

This function doesn't actually have an array as its parameter; what is really passed in the call to print_line is a pointer to a block of memory.

The print_line function could have been written this way:
void print_line(char * line)
{
printf("%s", line);
}[/code]

In both functions, what is passed is an address in memory (i.e., a pointer) at which the first character in the string is located. There is a strong connection in C (and C++) between arrays and pointers. The name of an array all by itself evaluates to the address of the beginning of the memory used by the array. Pointers are complicated enough that I won't go into any more detail right now.

Hope that helps.