Dynamic Array Passed to a function by Reference

This is a discussion on Dynamic Array Passed to a function by Reference within the C Programming forums, part of the General Programming Boards category; Just a quick question to satisfy my curiosity: Well, I managed to create an array using dynamic memory allocation (MxN), ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    6

    Dynamic Array Passed to a function by Reference

    Just a quick question to satisfy my curiosity:

    Well, I managed to create an array using dynamic memory allocation (MxN), as shown:

    matrix1=(double **)malloc(M*sizeof(double*)){
    for (i1=0;i1<1;i1++)
    matrix1[i1]=(double*)malloc(N*sizeof(double)); }

    and say I have already filled up the contents of this array completely and correctly, and that I can free up the memory I've reserved later on

    i then pass this array to a function by calling the function through this function call:
    print_elements(&M,&N,matrix1);

    with the function being called being a simple printing function:

    void print_elements(int* M, int* N, double** print){
    int rows_p, cols_p;
    printf("\n\nYour matrix is: \n");
    for(rows_p=0;rows_p<*M;rows_p++){
    for(cols_p=0;cols_p<*N;cols_p++){
    printf("%.2lf ", print[rows_p][cols_p]); }
    printf("\n");}}

    my question is, did I pass the array matrix1 by reference? I know that arrays are passed to a function by default by reference but I just wanted to know if it is indeed the case and if I even did it the way it really should be done?

    appreciate any help
    Last edited by patriots21; 11-21-2010 at 08:47 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    As Bayint already pointed out, technically no. It can be argued that in C something "like" passing by reference can be achieved but purists will argue (rightfully so IMO) that this is not the case.

    C doesn't have references so it doesn't make sense from that point of view. There is a significant difference between a reference and a pointer and C++ makes that difference very clear. A reference is an alias for something and that something must exist in order for the reference to exist. A pointer is just a type of data that stores the address of some data.

    On another note, don't cast malloc in C.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    6
    I see, thanks for that question-answer thing link, seem really informative, the lot of it.

    another thing, how come I shouldn't use malloc (I assume that also includes realloc and the rest) in C, kindly enlighten me

    edit: one more thing, how come my function (and the program i just checked with) worked when i declared it with
    void print_elements(int* M, int* N, double** print)
    instead of void print_elements(int* M, int* N, double print[][no. of columns]) ----I assume this is the one I needed to use had I not use dynamic allocation
    Last edited by patriots21; 11-21-2010 at 09:41 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Quote Originally Posted by patriots21
    another thing, how come I shouldn't use malloc (I assume that also includes realloc and the rest) in C
    You should not use dynamic memory allocation if you don't need it because it creates more work for you to do, and along with more work comes more mistakes.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by patriots21 View Post
    I see, thanks for that question-answer thing link, seem really informative, the lot of it.

    another thing, how come I shouldn't use malloc (I assume that also includes realloc and the rest) in C, kindly enlighten me

    edit: one more thing, how come my function (and the program i just checked with) worked when i declared it with
    void print_elements(int* M, int* N, double** print)
    instead of void print_elements(int* M, int* N, double print[][no. of columns]) ----I assume this is the one I needed to use had I not use dynamic allocation
    I didn't say you shouldn't use malloc, I said you shouldn't cast malloc.

    See: Cprogramming.com FAQ > Casting malloc

    As for your functions that change would make no difference regardless of whether memory was malloc-ed or statically allocated. I suggest you read a bit more about passing arrays as function parameters in C.

    Cprogramming.com FAQ > Pointers And Arrays (intermediate)
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    6
    heey thanks for all the links, already halfway though the second one, really appreciate it
    edit: and my bad for missing the one about malloc
    Last edited by patriots21; 11-21-2010 at 10:36 AM.

  8. #8
    Registered User
    Join Date
    Aug 2010
    Posts
    6
    well i can't seem to get the latter method of passing the array to the function to work with malloc.
    (void print_elements(int* M, int* N, double print[][no. of columns])), works fine when I allocate memory statically though.
    maybe i'm just doing something wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing a dynamic array to a function
    By esmeco in forum C Programming
    Replies: 15
    Last Post: 06-05-2010, 05:25 PM
  2. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  3. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  4. Dynamic Array Allocation function
    By P4R4N01D in forum C++ Programming
    Replies: 6
    Last Post: 05-15-2009, 03:04 AM
  5. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21