Like Tree1Likes
  • 1 Post By oogabooga

Problem in declaring array size inside a function

This is a discussion on Problem in declaring array size inside a function within the C Programming forums, part of the General Programming Boards category; I'm passing an array (pointer) as an argument of a function, but I know the size of the array only ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    13

    Problem in declaring array size inside a function

    I'm passing an array (pointer) as an argument of a function, but I know the size of the array only inside the function, so I'd need something like this:

    Code:
    #include <stdio.h>
    
    void foo(int *vec)
    {
        int n = 3;
        vec = malloc(n*sizeof(int));
        vec[0] = 41;
        vec[1] = 10;
        vec[2] = 47;
        printf("vec[1]: %d", vec[1]);   
    }
    
    
    int main()
    {
        int *vec;
        foo(vec);
        printf("vec[1]: %d", vec[1]);
        getch();
        return 0;  
    }
    The first printf gives me the right output, but the second one seems to crash the program. If I knew the size of the array before executing the function I would use int vec[n] to declare the array and delete the malloc line, but unfortunately that's not the case.

    Any ideas?

    Thanks.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You are changing vec inside that function through malloc() so you need to pass a int ** if you want to see the changes back in main.

    It's like saying why doesn't:

    void foo(int x) change my int I passed from main.
    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.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You can have foo return the size or have another int * passed as parameter which keeps track of the size.
    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 claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Also compile with your warnings on, you are probably getting implicit declaration of getch(), which is a ........ function from an old crappy library for DOS.
    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.

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Also free the memory you allocate
    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.

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    13
    Thank you claudiu, but I think I didn't get it... I tried this:

    Code:
    #include <stdio.h>
    
    void foo(int **vec)
    {
        int n = 3;
        vec = malloc(n*sizeof(int**));
        *vec[0] = 41;
        *vec[1] = 10;
        *vec[2] = 47;
        printf("vec[1]: %d", vec[1]);
        free(vec); 
    }
    int main()
    {
        int *vec;
        foo(&vec);
        printf("vec[1]: %d", vec[1]);
        _getch();
        return 0;  
    }
    But it's not working, even though I don't get any errors or warnings. What's wrong?
    I didn't know that getch() was deprecated, and my compiler (Dev-C++) doesn't show any warning when I use it. Is it ok to use _getch() instead?

    Merci!

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Dev-C++ is another obsolete crap IDE that hasn't been maintained in the past 8 years or so. Get rid of it. Use getchar() instead of all the getch() varieties.

    Your function doesn't work because you are freeing the memory as soon as you allocate it. You need to free it at the end of main after you are done using it.
    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.

  8. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    13
    Oops, my bad.
    But it's not working yet =P. Actually the black screen closes just after it opens. You think the code is wrong or it's a problem with the compiler?

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Did you put in getchar() before return 0? What's your latest code?
    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.

  10. #10
    Registered User
    Join Date
    Apr 2012
    Posts
    13
    I did. Here it's:

    Code:
    #include <stdio.h>
    
    void foo(int **vec)
    {
        int n = 3;
        vec = malloc(n*sizeof(int*));
        *vec[0] = 41;
        *vec[1] = 10;
        *vec[2] = 47;
        printf("vec[1]: %d", vec[1]);
    }
    int main()
    {
        int *vec;
        foo(&vec);
        printf("vec[1]: %d", vec[1]);
        free(vec);
        getchar();
        return 0;  
    }

  11. #11
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    In foo, it should be
    Code:
    *vec = malloc(n*sizeof(int*));
    claudiu likes this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  12. #12
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Code:
    *vec = malloc(n*sizeof(**vec));
    (*vec)[0] = 41;
    (*vec)[1] = 10;
    (*vec)[2] = 47;
    Also need to be mindful of operator precedence here.
    Last edited by hk_mp5kpdw; 04-27-2012 at 10:47 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,397
    I prefer:
    Code:
    *vec = malloc(n * sizeof(**vec));
    Also, this:
    Code:
    *vec[0] = 41;
    *vec[1] = 10;
    *vec[2] = 47;
    should be:
    Code:
    (*vec)[0] = 41;
    (*vec)[1] = 10;
    (*vec)[2] = 47;
    It may be easier to just write:
    Code:
    int *temp = malloc(n * sizeof(*temp));
    /* ... access temp[0] etc */
    *vec = temp;
    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

  14. #14
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    My example was wrong, it should have been:
    Code:
    *vec = malloc(n*sizeof(int));  // sizeof(int) not sizeof(int*)
    But, like laserlight, I prefer
    Code:
    *vec = malloc(n * sizeof **vec);
    since that will be the right type even if the base type is changed.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  15. #15
    Registered User
    Join Date
    Apr 2012
    Posts
    13
    Thank you guys, that's exactly what I need!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of array - why function gives size ONE only
    By noob123 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2009, 04:20 PM
  2. declaring a variable inside a loop
    By Emerald in forum C Programming
    Replies: 7
    Last Post: 08-25-2009, 01:11 AM
  3. declaring variables inside loops
    By samus250 in forum C Programming
    Replies: 21
    Last Post: 04-30-2008, 01:51 PM
  4. declaring a large array problem !
    By Array_Troubled in forum C++ Programming
    Replies: 10
    Last Post: 01-08-2003, 05:02 AM
  5. help with declaring array in which the user specifies size
    By ibanezrgking in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2002, 09:05 AM

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