arrays in functions where the size is a formal argument

This is a discussion on arrays in functions where the size is a formal argument within the C Programming forums, part of the General Programming Boards category; HI there, I'm new to both this forum and to C programming so apologies in advance if my question is ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,454
    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);
       }
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 08:38 AM
  5. functions and arrays
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 02-19-2002, 08:23 AM

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