problem passing pointers to a function

This is a discussion on problem passing pointers to a function within the C++ Programming forums, part of the General Programming Boards category; Hello folks, I'm working through the section on pointers in my ebook and I decided to try some things out. ...

  1. #1
    Trainee Geek Draylath's Avatar
    Join Date
    Nov 2011
    Location
    Cambridge
    Posts
    24

    problem passing pointers to a function

    Hello folks,

    I'm working through the section on pointers in my ebook and I decided to try some things out. I'm finding a it a bit hard going though and I've run into a problem. I'm getting compile time errors that I don't understand. I think I have done things correctly and used the syntax correctly but the compiler obviously sees things differently.

    I'll post the code and the debugging messages as well. Could someone give me a pointer (pun intended ) in the right direction. Thanks

    edit: Sorry. It's supposed to take in 5 intergers from user. These get sent to one function to get the values printed back and to a pointer to each variable and to another function to print the memory addresses of the 5 variables
    Code:
    #include <iostream>
    
    
    using namespace std;
    void givevalues(int value1, int value2, int value3, int value4, int value5);
    void giveaddress(int *p_value1,int *p_value2,int *p_value3,int *p_value4,int *p_value5);
    
    
    int main()
    {
        int value1, value2, value3, value4, value5;
    
    
        cout << "Enter 5 intergers...";
        cin >> value1;
        cin >> value2;
        cin >> value3;
        cin >> value4;
        cin >> value5;
    
    
        int *p_value1 = &value1;
        int *p_value2 = &value2;
        int *p_value3 = &value3;
        int *p_value4 = &value4;
        int *p_value5 = &value5;
    
    
    givevalues(value1, value2, value3, value4, value5);
    giveaddress(*p_value1, *p_value2, *p_value3, p_value4, p_value5);
        return 0;
    }
    
    
    void givevalues(int value1, int value2, int value3, int value4, int value5)
    {
        cout << "\t\t***VALUES***\n\n";
        cout << value1 << endl;
        cout << value2 << endl;
        cout << value3 << endl;
        cout << value4 << endl;
        cout << value5 << endl;
    }
    
    
    void giveaddress(int *p_value1,int *p_value2,int *p_value3,int *p_value4,int *p_value5)
    {
        cout << "\t\t***MEMORY ADDRESSES***";
        cout << p_value1 << endl;
        cout << p_value2 << endl;
        cout << p_value3 << endl;
        cout << p_value4 << endl;
        cout << p_value5 << endl;
    }
    /home/neil/Documents/Programming/arrayplay/main.cpp||In function ‘int main()’:|
    /home/neil/Documents/Programming/arrayplay/main.cpp|25|error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]|
    /home/neil/Documents/Programming/arrayplay/main.cpp|5|error: initializing argument 1 of ‘void giveaddress(int*, int*, int*, int*, int*)’ [-fpermissive]|
    /home/neil/Documents/Programming/arrayplay/main.cpp|25|error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]|
    /home/neil/Documents/Programming/arrayplay/main.cpp|5|error: initializing argument 2 of ‘void giveaddress(int*, int*, int*, int*, int*)’ [-fpermissive]|
    /home/neil/Documents/Programming/arrayplay/main.cpp|25|error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]|
    /home/neil/Documents/Programming/arrayplay/main.cpp|5|error: initializing argument 3 of ‘void giveaddress(int*, int*, int*, int*, int*)’ [-fpermissive]|
    ||=== Build finished: 6 errors, 0 warnings ===|
    [/code]
    Last edited by Draylath; 07-13-2012 at 01:58 PM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    giveaddress(*p_value1, *p_value2, *p_value3, p_value4, p_value5);
    should be
    giveaddress(p_value1, p_value2, p_value3, p_value4, p_value5);
    (Why do you dereference the pointers?)

    Or better yet,
    giveaddress(&value1, &value2, &value3, &value4, &value5);

    Don't forget to learn about references,
    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.

  3. #3
    Trainee Geek Draylath's Avatar
    Join Date
    Nov 2011
    Location
    Cambridge
    Posts
    24
    Thanks Elysia,

    I have it now. I actually sorted it before I read your message (that'll teach me to code on the edge of my understanding when I'm tired). Advice on references will be taken. I'll make something with those in next, methinks an adaptation of a calculator program I did a while back.

    Just to clarify I understand this. The * operator is like reading the directions and heading to the right place. if you don't have it in it's just the memory address? I think that's how it works

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    Quote Originally Posted by Draylath View Post
    Just to clarify I understand this. The * operator is like reading the directions and heading to the right place. if you don't have it in it's just the memory address? I think that's how it works
    Correct.
    The variable contain the memory address, the & operator returns the memory address of some variable, and * fetches the values stored at the memory address of a variable.
    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.

  5. #5
    Trainee Geek Draylath's Avatar
    Join Date
    Nov 2011
    Location
    Cambridge
    Posts
    24
    Many thanks Elysia

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers - Passing to function
    By kerrymaid in forum C Programming
    Replies: 2
    Last Post: 07-06-2010, 09:01 AM
  2. Replies: 9
    Last Post: 10-15-2008, 04:08 AM
  3. passing pointers to a class function
    By dyn4sty22 in forum C++ Programming
    Replies: 1
    Last Post: 04-28-2008, 07:38 PM
  4. Passing function pointers
    By keira in forum C Programming
    Replies: 6
    Last Post: 09-29-2007, 04:55 AM
  5. Passing a 2d Array of pointers to a Function
    By miclus in forum C Programming
    Replies: 6
    Last Post: 09-11-2004, 08:34 AM

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