Thread: Modifying an array from within a function

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Guest
    Guest

    Modifying an array from within a function

    Hello, this is my first post. I'm fairly new to programming and i have some uncertainty regarding the manipulation of arrays through a function that they are passed to.

    The context is a matrix transformation, where i want to call a function with an array m[4][4] and a few transformation parameters as its arguments.

    Code:
    void matrixTransform(double *m, double rx, double ry, double rz, double tx, double ty, double tz)
    {
        /* Here i do a few mathematical calculations.
            Essentially, i want to set m = m * mt, where mt is an array[4][4]
            that is built from the (rx, ry, ...) parameters on each function
            call.
        */
    }
    I have browsed related questions here on the forum and elsewhere on the web, but they don't address my confusion, so i though i'd go through it step by step to finally get clarity.

    My question is regarding the proper syntax for handling arrays passed to functions. (1) Do i understand it correctly, that an asterisk needs to be placed before first parameter m, because the function is to expect an array (pointer)?

    (2) And are these two lines equivalent in meaning/behavior?
    Code:
    void matrixTransform(double *m) {...}
    void matrixTransform(double m[]) {...}
    (3) I have also seen the use of
    Code:
    function(double **var)
    in relation to array handling, which confused me even more.

    (4) Since my code does an (abstractly described) m = m * mt assignment, i suspect i need to create a copy of m within the function, so that i can read from it's original contents, while writing new ones to it.

    e.g.
    Code:
    void matrixTransform(double *m)
    {
        /* (again, pseudo-code because it's redundant to type it all out)
            these are 2D arrays of size [4][4]. */
        m2 = m;
        m = m2 * mt;
    }
    (5) What would be a robust but fast method to create this copy? A simple declaration of m2 followed by a for-loop to transfer m's contents to it?




    Regarding array manipulation, if i write this code:
    Code:
    void matrixTransform(double *m)
    {
        m[2][3] = 5;
    }
    (6) Have i effectively changed a value in m's original address space? I imagine this would be most desirable for an array that iteratively gets its values modified, but where size (16 x 8 bytes) stays constant.

    (7) Also, am i right in thinking the function can be of type void, as i'm not returning a value but manipulating a pre-existing array?


    Sorry for all the questions. Not 'getting it', i simply don't want to use trial and error in C; it's too easy to mess up.

    Adrian
    Last edited by Guest; 01-01-2013 at 05:15 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modifying a program to use a function
    By cnoviceirish in forum C Programming
    Replies: 2
    Last Post: 12-06-2012, 01:23 PM
  2. K&R Exercise 1-16: Modifying a getline() function
    By edw211 in forum C Programming
    Replies: 5
    Last Post: 06-15-2011, 03:58 PM
  3. modifying bubblesort function
    By tenhavenator in forum C Programming
    Replies: 3
    Last Post: 11-14-2009, 12:54 AM
  4. Modifying an element in an array
    By Martin_T in forum C Programming
    Replies: 3
    Last Post: 11-04-2009, 09:10 PM
  5. Modifying a char array in VC++ 6 ide.
    By clu82 in forum Windows Programming
    Replies: 5
    Last Post: 03-18-2003, 01:56 PM