Thread: arrays in functions where the size is a formal argument

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    5

    Cool arrays in functions where the size is a formal argument

    HI there,

    I'm new to both this forum and to C programming so apologies in advance if my question is answered in the FAQ or elsewhere but I could not find it.

    I would like to do the following:

    within a function (other than main) definition I would like to declare an array of size equal to some number that is a formal argument of the function itself. So if "n" is a formal argument of FUNCT, say, I would like to locally (within FUNCT) declare an array of size n.

    This seems a fairly reasonable thing to do and I am sure there must be a trick - but I can't get it to work. I have tried dynamic memory allocation using malloc to no avail.

    Thanks in advance for any comments anyone may have.

    Boggle

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    I am not a C programmer (I'm a C++ programmer) but I think you can do this
    Code:
    void f(int n)
    {
        int* ptr = (int*)malloc(sizeof(int) * n);
    }
    However I think I have read against casting the result of malloc... Oh and don't forget free() ing memory.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    You should not cast malloc in C as FAQ suggests, and you HAVE to check the return value

    Code:
    void f(int n)
    {
       int* array = malloc(n* sizeof *array);
       if(array)
       {
          /* do your stuff here */
          ...
          free(array);
       }
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    5
    Thanks guys.

    One question about the sample code you provided vart - why the use of the "if" statement?

    Also, I am not sure what you mean by the return value.

    I am very grateful for the help.

    Cheers.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    @boggle, malloc returns a NULL pointer if it fails. You don't want to continue on thinking the memory has been allocated and really it hasn't.

    However, if malloc succeeds it returns a pointer to the start of the newly allocated block of memory (ie not NULL). Just remember anything non-zero in C is true, and thing 0 is false.

    A more explicit way of putting it is:
    Code:
    /* ... */
    if(array != NULL)
    {
    /* ... */

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    5
    you guys rock!! Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. function formal parameter pointer style for arrays
    By curlious in forum C++ Programming
    Replies: 5
    Last Post: 07-08-2003, 06:18 PM
  3. Functions returning char arrays
    By turmoil in forum C Programming
    Replies: 3
    Last Post: 05-27-2003, 01:43 AM
  4. Arrays and Functions
    By WellyJ in forum C Programming
    Replies: 6
    Last Post: 03-19-2003, 09:38 AM
  5. functions and arrays
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 02-19-2002, 09:23 AM