Thread: Sending array's to functions by reference or pointers

  1. #1
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209

    Sending array's to functions by reference or pointers

    Is it not possible to send the adress of the first element of an array to a function? I tried every possible combination, including:

    Code:
    char myArray[50];
    cout<<"Please enter a string: ";
    cin.get(myArray,49);
    myFunction(myArray)
    //function header
    void myFunction(char* array)
    That didn't seem to do the trick. I would think that doing that would send the address of the first element in the array. Am I wrong?

    How is it done? Using arrays of pointers??

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    73

    Make your array global

    Make your array global in this example.

    Code:
    #include <iostream>
    
    char array[50];
    void functionName(void);
    
    int main(void)
    {
    ..............
    }
    Last edited by ElWhapo; 12-26-2004 at 06:21 PM. Reason: I messed up the code tags

  3. #3
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    I know that that is solution, but is it possible without resorting to global stuff?

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Code:
    my_function(&(myarray[0]))
    Just accept a pointer to myarray's type as a parameter.

    edit: And your example should work. Would you care to give us some more detail so we cn determine the REAL source of the problem?

    edit again: Is it possible? Of course it is! There are so many standard C functions that take the first element of a char array as a parameter!
    Last edited by sean; 12-26-2004 at 06:49 PM.

  5. #5
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Here's the code. On the bolded lines, I can't figure out how to print the address of the first element to see if the adress is actually being sent. Instead it is printing the whole array. I think this might not appear to be working because I am trying to store the same contents back into array in the function.

    Code:
    #include <iostream>
    
    using namespace std;
    
    void stringReverser(char* array);
    
    int main()
    {
        char array[50];
        
        cout<<"Please enter a string to have it reversed: ";
        cin.get(array,49);
        cout<<endl;
        cout<<&array[0]<<endl;
        stringReverser(&(array[0]));
        cout<<array<<endl;
        system("PAUSE");
        
        return 0;
    }
    
    void stringReverser(char* array)
    {
        cout<<&array[0];
        char newArray[50];
        int i;
        for(i = 0;*(array+i);i++)
        {}
        for(;i>-1;i--)
        {
            cout<<*(array+i);
            newArray[i] = *(array+i);
        }
        cout<<endl;
        for(int i = 0; i<50;i++)
        {
            *(array+i)=newArray[i];
        }
    }

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    cout is overloaded so that you can send it the address of the first element of a char array, and it will print the whole array. Set up a single char value and read the value into it (then send the variable, not a pointer), or create a newstring and put a null character on the second element.

    edit: If you want to print the actual memory address, you need to assign the address to a char variable - NOT a pointer. They're handled differently by cout.

  7. #7
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    I'm getting an error when I try to assign the address to a normal char variable, 'invalid conversion from char* to char'. Here's what I did:

    Code:
    char point = &array[0];
    cout<<point<<endl;

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I'm sorry... I'm surprized that didn't work... I can't think of a solution right now... If I do I'll let you know.

  9. #9
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    That's fine... char arrays, such strange things...

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    It's not the arrays so much as the functions that handle them. If it wasn't for the generally-accepted standard of how C-Strings work, I could give you a perfect solution. It works with integers!

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Ah! That's it! Maybe we could cast it to an int and not change the variable's address...

    Maybe something like this:

    Code:
    int *integer = (int)&character[0]
    edit: I'm too tired to think, and I'm really not sure about the code above, but I'll keep thinking. Play around with it a bit... Hopefully someone else reads this thread soon.
    Last edited by sean; 12-26-2004 at 08:17 PM.

  12. #12
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Here's the code, your suggestion seems to have sent the right address, but I can't get the function to change the array at all. (I had to make a minor change in the way the address was casted.)
    The bolded is where I try to change the original array to the contents in the temporary array, however the bolded cout statement shows that nothing happened.

    Code:
    #include <iostream>
    
    using namespace std;
    
    void stringReverser(int* array);
    
    int main()
    {
        char array[50];
        
        cout<<"Please enter a string to have it reversed: ";
        cin.get(array,49);
        cout<<endl;
        int *integer = (int*)&array[0];
        cout<<integer<<endl;
        stringReverser(integer);
        system("PAUSE");
        
        return 0;
    }
    
    void stringReverser(int* array)
    {
        
        char* point = (char*)array;
        char newArray[50];
        int i;
        for(i = 0;*(point+i);i++)
        {}
        for(;i>-1;i--)
        {
            newArray[i] = *(point+i);
            cout<<newArray[i];
        }
        cout<<endl;
        for(int i = 0; newArray[i]; i++)
        {
            *(point+i) = *(newArray+i);
        }    
        cout<<point;
    }

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Is it not possible to send the adress of the first element of an array to a function?
    Without resorting to all sorts of hacks, that is the way it always happens by default.

    So
    Code:
    void myFunction(char* array); // prototype
    char myArray[50];
    cout<<"Please enter a string: ";
    cin.get(myArray,49);
    myFunction(myArray);  // identical
    myFunction(&myArray[0]);  // calls
    //function header
    void myFunction(char* array)
    Your latest attempt using int pointers will not work - try one of your earlier char* attempts.

    Also, you dont need a temporary array in order to reverse an array in place

    Reversing "hello" results in "oellh" on the first pass.
    That is, swap end pairs of chars, and work your way to the middle of the string.

  14. #14
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Well, guys, my internet went out last night, so I worked on this. I went my trusty new book (the one in my avatar), and it told me what Salem stated. I guess it wasn't that hard after all. Also, I think it didn't appear to be changing it because the 'newArray really contained the same elements as the original, and assigning the original to newArray did no good. I fixed that problem too. Now everything works:

    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    void stringReverser(char* array, int len);
    
    int main()
    {
        char array[50];
        
        cout<<"Please enter a string to have it reversed: ";
        cin.get(array,49);
        int length = strlen(array);
        stringReverser(array,length);
        cout<<"Your string reversed is: "<<array<<endl;
        system("PAUSE");
        
        return 0;
    }
    
    void stringReverser(char* array, int len)
    {
        char newArray[50];
        int i = 0;
        for(;array[i];i++)
        {
            newArray[i] = array[i];
        }
        int j = len-1;
        for(int i = 0;i<len;i++,j--)
        {
            newArray[i]=array[j];
        }
        for(int i = 0;i<len;i++)
        {
            array[i]=newArray[i];
        }            
    }

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you want to use a temporary array and a few loops, that works fine. However, you can get rid of the first for loop, because it isn't needed. Your second loop copies right over it without doing anything else first. Also, you already have a variable i, so why are you declaring it over and over again in the other loops?

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. functions using arrays
    By trprince in forum C Programming
    Replies: 30
    Last Post: 11-17-2007, 06:10 PM
  2. Pointers and reference passing
    By Denis Itchy in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2002, 01:36 AM
  3. passing arrays into functions by reference
    By Aran in forum C++ Programming
    Replies: 7
    Last Post: 05-25-2002, 09:55 PM
  4. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM
  5. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 10:33 PM