Creating Multi-Indexed Arrays & Functions w/ Array Inputs & Outputs

  • Thread starter Thread starter okkvlt
  • Start date Start date
  • Tags Tags
    Arrays Functions
AI Thread Summary
To create a multi-dimensional array in C, such as a matrix, you can declare it using syntax like float array[3][4], which defines a two-dimensional array with 12 elements, indexed from array[0][0] to array[2][3]. When it comes to functions in C that handle arrays, while you cannot return an array directly, you can return a pointer to an array. For instance, a function can accept an array as a parameter, which is effectively a pointer to the first element of the array. An example function definition is void print_line(char *line), where the parameter is a pointer to a character array, allowing manipulation of string data. This highlights the close relationship between arrays and pointers in C programming.
okkvlt
Messages
53
Reaction score
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?
 
Technology news on Phys.org
okkvlt said:
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].
okkvlt said:
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top