Thread: Arrays and Functions

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Wink Arrays and Functions

    Consider:
    Code:
    void function(long n1[5]);
    A function protoype.

    what are the maximum number of values that I can change back in the main using the parameter I pass in??

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    For a single dimension array, the number in the brackets is absolutely meaningless - you may just as well leave it empty for all that the compiler cares.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Thumbs up

    Quote Originally Posted by matsp View Post
    For a single dimension array, the number in the brackets is absolutely meaningless - you may just as well leave it empty for all that the compiler cares.

    --
    Mats
    Excellent Mats you really are a c maestro

    Furthermore, does this mean that it is basically a pointer and I will only be able to alter what it points to?

    Thanks

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    55
    Yes you can alter it.
    Usually, you also pass the size of the array to a function:
    Code:
    void show( int n[], int size)
    {
        while (size--) printf( "%d ", *n++);
        printf( "\n");
    }

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by strokebow View Post
    Excellent Mats you really are a c maestro

    Furthermore, does this mean that it is basically a pointer and I will only be able to alter what it points to?

    Thanks
    It is a pointer yes. And you can alter anything related to that address [and if you really want to, and know where it is, you can even reach data outside of what it actually points to...]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I note that this is the C++ section, so ideally you should be using std::vector or std::tr1::array, rather than manually passing the size of an array. Unless you are using C, of course.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays, functions, HELP
    By beginner1 in forum C Programming
    Replies: 4
    Last Post: 05-20-2009, 03:29 PM
  2. Manipulating Character arrays in functions.
    By kbro3 in forum C++ Programming
    Replies: 11
    Last Post: 08-16-2008, 02:24 AM
  3. functions using arrays
    By trprince in forum C Programming
    Replies: 30
    Last Post: 11-17-2007, 06:10 PM
  4. Arrays and Functions
    By KunoNoOni in forum Game Programming
    Replies: 12
    Last Post: 10-04-2005, 09:41 PM
  5. Arrays out-of-bounds in functions only?
    By KneeLess in forum C Programming
    Replies: 5
    Last Post: 11-03-2004, 06:46 PM