Thread: Referencing Arrays : Syntax

  1. #1
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68

    Referencing Arrays : Syntax

    Basically, im stuck on what im doing because i dont know what the syntax for a reference to an int array is. What i need it for is to initialise arrays to a give value.

    what i have now is...
    Code:
    int InitialiseArray ( int &[] ArgArrayName , int ArgInitValue )
    {
        int Element;
        for (Element = 0 ; Element < sizeof ArgArrayName ; Element ++ )
            ArgArrayName [Element] = ArgInitValue;
        return 0;
    }
    and for calling i am using
    Code:
    int Foo [6];
    InitialiseArray( &Foo, 0);
    But the syntax for calling the array is wrong (compiler says : main.cpp
    parse error before `,' token (the inside the function argument parentheses)
    , and my compiler, for some reason, whinges about the two arguments not being declared.
    Ph33r the sphericalCUBE

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >i dont know what the syntax for a reference to an int array is
    int (&array)[SIZE]

    >InitialiseArray( &Foo, 0);
    That's a pointer to an array, not a reference. Using references you can remove the address-of operator in the call. But note that the size of the array you pass must match the argument, and an empty size in the argument doesn't count.
    My best code is written with the delete key.

  3. #3
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68
    thanks a ton

    is there a way around the array size specification using pointers?

    Until then, i mught just make some different functions and route them through a switch.



    i always seem to miss out on brackets
    Ph33r the sphericalCUBE

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    161
    Code:
    int InitialiseArray ( int &[] ArgArrayName , int ArgInitValue )
    There, you are declaring an array of references to int-- this is not allowed because references must be initialized to reference something and they may not be changed. Those are two things that arrays cannot provide.

    Given that, it would probably be beneficial for you to know that you do not need to pass arrays as references because arrays are always passed by reference-- they actually decay into pointers.

    So, you can prototype your function like this:
    Code:
    int InitialiseArray ( int ArgArrayName[], int ArgInitValue )
    // or
    int InitialiseArray ( int* ArgArrayName , int ArgInitValue )
    Also, because arrays decay to pointers, you do not need to take the address of it in the function call. So you can call it like this:
    Code:
    InitialiseArray( Foo, 0);
    But, there is one obvious flaw inside your function. Take a look at this code:
    Code:
    for (Element = 0 ; Element < sizeof ArgArrayName ; Element ++ )
    Unfortunately, the fact that arrays decay into pointers is exactly what is causing your problem here. sizeof will not return the number of elements in the array. Instead, it will return the size of the ArgArrayName variable which will be whatever the size of a pointer type is on your machine (probably 4 bytes). To fix this, you'll need to pass the size of your array along with the other parameters in this function and use that value as the condition for your for loop.

    Just as an aside-- if this is your actual code, you can replace the whole thing with one simple statement:
    Code:
    int Foo[6] = { 0 };
    That will create an array of 6 integers and set them all to 0.

    -tf

  5. #5
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68
    So that DOES work with member arrays!

    *bash self in head*

    I was trying to do something like....
    Code:
    class FooFoo {
    int Foo [6] = {0,0,0,0,0,0};
    {;
    Well, you learn something every day.
    Thanks to those who helped.
    Ph33r the sphericalCUBE

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    161
    > So that DOES work with member arrays!

    Doh! No, sorry.. it won't work for members. For class members, you'll have to use a for loop like you've done or you can use memset:

    Code:
    class FooClass
    {
    public:
      FooClass() { memset(arr, 0, 6); }
    
    private:
      Foo arr[6];
    };
    memset is in the <cstring> header and the prototype is:
    void * memset ( void * buffer, int c, size_t num );

    -tf

  7. #7
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68
    thanks. Never used <cstring> before, normally i use strings as opposed to char[]'s.

    That saves me a lot of time.
    Ph33r the sphericalCUBE

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 07-14-2009, 08:16 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  4. Help with arrays and pointers please...
    By crazyeyesz28 in forum C++ Programming
    Replies: 8
    Last Post: 03-17-2005, 01:48 PM
  5. DJGPP assembly syntax ills...
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-11-2001, 02:54 AM