Thread: pointers to arrays

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    15

    pointers to arrays

    Hey! I have some problems with arrays and pointers... The code below should explain what I'm trying to do but it seems I'm missing something? Please help!!! Ie, how do I pass a pointer to an array as argument?

    Code:
    void test(float fff[]) {
       cout << fff.length;
    }
    
    int main(void) {
       float f[] = {1,2,3,4,5};
       test(&f);
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    Code:
    #include <iostream>
    using namespace std;
    
    void test(float* fff, int length) {
       cout << fff[0];  //access first element of array f
    }
    
    int main() 
    {
       float f[5] = {1,2,3,4,5};
       test(f, 5);
    }
    The trick is allow your function to accept a pointer to the type of array and then also send the number of arrays as the 2nd param. Remember that the name of an array is also an address to the first element. So in this example f = &f[0], that's why we don't use the address of operator when sending the array as an argument...
    Hope that helped at all...

    One last thing...you can't call length() on your array...
    Last edited by Chaplin27; 03-07-2005 at 10:31 PM.

  3. #3
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    Is there a function you can call on an array to find out it's length?

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Not a dynamic array. But if it's an array of constant size, sizeof will work. Though it's not suggested....best to avoid having to do that. It also doesn't work just on pointers.

    Oh, and please note,

    Code:
    #include <iostream>
    using namespace std;
    
    void myFunc(float* arrayPtr)
    {
        cout << sizeof(arrayPtr) << endl;
    }
    
    int main()
    {
        float array[10];
        cout << "main: " << sizeof(array) << endl;
    
        myFunc(array);
        return 0;
    }
    Test that out to see what I mean.
    Last edited by jverkoey; 03-07-2005 at 11:08 PM. Reason: yummy syntax colorizing!

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    yeah I was trying to figure out if there was a function to call to get a length...if you use stl containers there's functions for those...why avoid sizeof? Just out of curiosity?

    ***
    EDIT:

    Oh ok...because it's actually adding up the amount of bytes...not the number of elements...

  6. #6
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Quote Originally Posted by Chaplin27
    why avoid sizeof? Just out of curiosity?
    IMO it's a bad habit to get in to with arrays.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    Quote Originally Posted by jverkoey
    IMO it's a bad habit to get in to with arrays.
    Yeah I can understand why...good call on the code, opened my eyes...

    ***
    EDIT: yay...my 100th post

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>why avoid sizeof?
    Avoid sizeof (when using arrays, it's fine to use on single variables), simply because it's EXTREMELY easy to use it in the wrong place, especially since the compiler won't catch you if you do. It's usually just as easy to do this:
    Code:
    const int sizeOfArray = 10;
    float myArray[sizeOfArray];
    ...
    for(int i = 0; i < sizeOfArray; ++i)
       myArray[i] = i;
    
    //Instead of:
    float myArray[10];
    ...
    for(int i = 0; i < sizeof(myArray) / sizeof(*myArray); ++i)
       myArray[i] = i;
    This way, if you want to change the size of the array you can change the constant and it will be changed everywhere else - just as if you used sizeof/sizeof and changed the actual size of the array in the declaration instead, but safer and simpler too in its own way. You can pass the length of the array etc. to any function that needs it, and this method will work for both dynamic and static arrays too.

    **EDIT**
    >>because it's actually adding up the amount of bytes...not the number of elements...
    Although that's one reason (convenience), the real reason is simply that if you try using sizeof on a dynamic array, you'll end up with the size of the pointer (usually 4 bytes) no matter what the size of the array itself is. If you don't realize this, then you have a nasty and hidden bug just waiting to happen.
    Last edited by Hunter2; 03-07-2005 at 11:20 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    IMO it's a bad habit to get in to with arrays.
    Yeah I can understand why...good call on the code, opened my eyes...
    Why is using sizeof a bad habit? I realize you can't get the size of an array using sizeof on a pointer to a dynamically created array, but using:

    sizeof my_array / sizeof my_array[0]

    gives you the size of a statically created array of any type perfectly well and good--as far as I can tell. It also seems like it can be easier to use in certain situations. For instance, if you are testing out some code on an array, and you are adding and deleting elements from the array as you test your code, you don't have to do anything else. If instead, you use a constant for the array size, then every time you add or delete an element from the array, you also have to change the constant--which is too much of a pain.
    Last edited by 7stud; 03-08-2005 at 01:48 AM.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Its a better Idea just to use vectors actually.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>testing out some code on an array, and you are adding and deleting elements from the array as you test your code, you don't have to do anything else.
    I don't follow you here. How are you 'adding' and 'deleting' elements from the array? If you're speaking of dynamic arrays, then you have just proven the exact point that I was trying to make; and if you're speaking of static arrays, then you will not be able to add/remove elements at all anyway, and therefore sizeof will simply give you the size of the array in bytes - and you'll have to perform a costly division operation too, if you want to get the length of the array.

    If instead, you use a constant for the array size, then every time you add or delete an element from the array, you also have to change the constant--which is too much of a pain.
    Changing the constant takes exactly the same amount of time, typing and work as changing the hardcoded array size.

    >>Its a better Idea just to use vectors actually.
    While this is generally true, there are a few rare cases (dealing with API calls that cannot accept vectors) in which arrays are necessary.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    if you're speaking of static arrays, then you will not be able to add/remove elements at all anyway
    Code:
    int myArray[] = {1,2,3,4};
    You can can add or remove elements to the array at will, and

    sizeof myArray / sizeof myArray[0]

    will always give you the correct array size.

  13. #13
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Quote Originally Posted by Hunter2
    >>Its a better Idea just to use vectors actually.
    While this is generally true, there are a few rare cases (dealing with API calls that cannot accept vectors) in which arrays are necessary.
    Yea, thats true. You have to work with whats given.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  14. #14
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    @7stud:
    I see what you mean now. That's a good point Of course, if you do that then to get the best of both worlds you can do:
    Code:
    int a[] = {1, 2, 3, 4};
    const int sizeOfA = sizeof(a) / sizeof(*a);
    To add a qualifier to my previous statement, then, I feel that it is unsafe practice to use sizeof with arrays except in tightly controlled situations where it is immediately clear that the 'array' is truly an array and not a pointer.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  15. #15
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    >>Its a better Idea just to use vectors actually.
    While this is generally true, there are a few rare cases (dealing with API calls that cannot accept vectors) in which arrays are necessary.
    Code:
    void somefunc(int *arr, int size);
    
    void someother func()
    {
      std::vector<int> vInt;
      // Code that fills vInt
      someFunc(&vInt[0], vInt.size());
    }
    Quote Originally Posted by CPP Standard 23.2.4.1
    [...]The elements of a vector are stored contiguously, meaning that if v is a vector<T, Allocator> where T is some type other than bool, then it obeys the identity &v[n] == &v[0] + n for all 0 <= n < v.size().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with returning arrays using pointers
    By cuba06 in forum C Programming
    Replies: 9
    Last Post: 11-23-2007, 10:40 AM
  2. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  3. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  4. pointers and arrays..
    By ahming in forum C Programming
    Replies: 1
    Last Post: 04-24-2004, 03:12 AM
  5. Stack functions as arrays instead of node pointers
    By sballew in forum C Programming
    Replies: 8
    Last Post: 12-04-2001, 11:13 AM