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

  • Thread starter Thread starter okkvlt
  • Start date Start date
  • Tags Tags
    Arrays Functions
Click For Summary
SUMMARY

This discussion focuses on creating multi-indexed arrays in C, specifically two-dimensional arrays, and the nuances of passing arrays to functions. A two-dimensional array can be declared using syntax like float array[3][4];, which creates a matrix with 12 elements. Additionally, while C does not allow functions to return arrays directly, they can return pointers to arrays, as demonstrated in the print_line function, which accepts a character array as a pointer.

PREREQUISITES
  • Understanding of C programming syntax
  • Familiarity with pointers in C
  • Knowledge of array data structures
  • Basic understanding of function parameters in C
NEXT STEPS
  • Learn about multi-dimensional arrays in C
  • Explore pointer arithmetic in C
  • Study function return types and memory management in C
  • Investigate dynamic memory allocation for arrays in C
USEFUL FOR

C programmers, software developers, and computer science students looking to deepen their understanding of array manipulation and function design in C.

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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 17 ·
Replies
17
Views
4K
Replies
6
Views
3K
  • · Replies 31 ·
2
Replies
31
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
2
Views
2K
Replies
17
Views
2K