Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Functions and arrays.

  1. Sep 16, 2009 #1
    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?
     
  2. jcsd
  3. Sep 16, 2009 #2

    Mark44

    Staff: Mentor


    Code (Text):

    float array [3, 4];
     
    The line above declares a two-dimensional array with 12 elements, indexed from array[0,0] through array[2, 3].
    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 (Text):

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Functions and arrays.
Loading...