Question:

Dynamic arrays in C?

by  |  earlier

0 LIKES UnLike

I have an array being passed to a function (type: int, size: 20) and I would like to know how to make this function directly affect the array and not the copy being made for the function. Someone once told me that by passing an array, you're automatically passing a pointer of its location and thus the array, once edited in a function, remains edited elsewhere. This is not the case (I know from my coding). However, I'm a bit shaky how we used to make arrays dynamic (back when I took computer science) and remember something like some appendage of "&" in parameters? Does this make sense or am I getting mixed up with other things? Anyway, if this is somewhat correct please elaborate on necessary steps to making dynamic arrays, otherwise just tell me flat-out.

 Tags:

   Report

1 ANSWERS


  1. Here is how arrays work my friend.

    int myArray[20];

    int a = 5;

    Every variable has a position in memory, right?  Lets say for instance, the variable a has a memory location of 30.  So at the memory location of 30, the value 5 resides.  

    Now lets look at how arrays work.  

    Arrays work the same as the way our variable a worked.  However, the indexes move up in memory.  For instance, lets say the array begins at memory location 50.  (These memory locations are just random numbers I'm throwing out, it's not really relevant for this explanation). So myArray[0] is at location 50.  What is myArray[1], then?  51!  So if you pass the memory address of the first index of myArray, there is no need to pass the rest of the addresses, right?  Because all you do to access the rest of the values of myArray is move up using the index values, such as myArray[2], myArray[3], etc.  Because it has the original address to know where to start in order to access each memory location.

    So in order to use the array in a function, all you have to do is pass the memory address of the first index, myArray[0].  We pass the address by specifying the array without any indexes, such as myArray.  (To specify the address of an individual array, you do: &myArray[1]).

    So to pass the address of an array, you set up the function as this:

    int myFunction(char *myArray)

    {

    /* STUFFFFF */

    return 0;

    }

    to pass the array as an argument, you do:

    myFunction(myArray);

    the parameter for myFunction is indeed a pointer variable, that holds the memory location of myArray, which is the same as: &myArray[0]. (notice that &myArray is incorrect).

    When you pass a variable to a function, it copies the value of that variable, unless you do:

    myFunction(char *blah)

    {

    return 0;

    }

    and then to call it:

    myFunction(char &a);

    where "a" is the variable I mentioned earlier.  This would pass the memory location of a, and thus would access it's true value, and is able to alter it.  When passing arrays you do the same thing.  

    You might want to post the code that you thought didn't alter your array values.

You're reading: Dynamic arrays in C?

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.