Array question

This is a discussion on Array question within the C++ Programming forums, part of the General Programming Boards category; If I have a function as an array NumArray and it is not modified by the function, how will it ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    40

    Array question

    If I have a function as an array NumArray and it is not modified by the function, how will it appear in the parameter list? Will it be just as it is entered? Not sure I understand this totally.

    Thanks!!

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    To pass an array to a function you could do this:
    Code:
    #include<iostream>
    using namespace std;
    
    void someFunc(const int* array, int n)
    {
    for (int i=0;i<n;i++)
      cout<<array[i]<<endl;
    }
    
    int main()
    {
    int array[10];
    for (int i=0;i<10;i++)
      array[i]=i;
    someFunc(array,10);
    }
    In that example you could not modify the contents of the array and n would be the size of the array. Does that help?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    40
    Yes it helps some. Can we substitue NumArray in there somewhere? Also, can you make comments so I know what things are doing? That is what helps me learn the most!
    Thanks!!!

  4. #4
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Whenever you pass an array, you pass a pointer. Regarding functionality, the function declarations below are the same:
    Code:
    void f( int* array_in, int size_in ); // pass a pointer - actually it should point to the first element of the array 
    
    void g( int array[], int size_in ); // pass an array
    Both function have a second parameter, as you should also provide the array's size, in order to prevent step-by errors - meaning you try to read or write data pass the end of the array. Passing the array's size allows necessary safety checks which are recommended when working with arrays.

    When calling the function, you can do following:
    Code:
    int anArray[5] = {0, 1, 2, 3, 4}; // declaration and initilization
    f( &anArray[0], 5 ); // passing the address of the first element
    
    f( anArray, 5 ); // simplified syntax
    Using the second, simplified form is shorter and safer, as you surely pass the starting address of the given array (the compiler ensures that).
    As arrays are passed by pointers, not by value, their contents can be changed in the called function. In order to disallow this, you should pass a pointer to constant, as specified by JaWib.

    Speaking of classical arrays, their size does not change at runtime. Of course, you can write your own Array class, which does change dinamically, but you should also check STL vector class for example. However, this is a little bit advanced issue.
    Last edited by Carlos; 06-04-2004 at 01:21 AM.

  5. #5
    Useless Apprentice ryan_germain's Avatar
    Join Date
    Jun 2004
    Posts
    76

    const?

    Since the array is not to be changed, would it be possible just to so something like :
    Code:
    int myFunc(const int array[], int size);

  6. #6
    Useless Apprentice ryan_germain's Avatar
    Join Date
    Jun 2004
    Posts
    76
    Quote Originally Posted by ryan_germain
    Since the array is not to be changed, would it be possible just to so something like :
    Code:
    int myFunc(const int array[], int size);
    maybe i should have just read the post above before

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 06:07 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 05:27 PM

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