Functions and arrays.

  • Thread starter okkvlt
  • Start date
  • #1
53
0
How do i create an array such that there is more than one index? the array declared here
float array;
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?
 

Answers and Replies

  • #2
34,025
5,679
How do i create an array such that there is more than one index? the array declared here
float array;
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

Code:
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.
Code:
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.
 

Related Threads on Functions and arrays.

  • Last Post
Replies
8
Views
5K
  • Last Post
Replies
1
Views
2K
Replies
10
Views
4K
Replies
6
Views
3K
Replies
3
Views
3K
Replies
7
Views
2K
  • Last Post
2
Replies
29
Views
68K
Top