Hi again everyone.

This is going to sound really dumb but I have to ask it because I'm not very advanced at C++.

If I have a function such as Funct(int *arg1, int *arg2) , how do I use this function in a program.
Can I just enter the values such as Funct(1,2) or do I have to use variables like &arg1 = 1 and &arg2 = 2 and then use the pointer variables as arguments? (I'm just getting confused now).

I told you this was a newbie question.

Thanks. Hope to hear from somebody soon.

Recommended Answers

All 4 Replies

Hey, pointers are pointers to memory locations (You want to do something specific to that memory block etc..)

If we're talking about passing arrays to functions, we use pointers:

void Funct(int *element)
{
    cout << element[0];
}

int main(int argc, char *argv[]) {


    int *elements = new int[10];

    elements[0] = 10;

    Funct(elements);

}

If we want to pass in a value that it's memory location can be edited, then we can do this:

void Funct(int &element)
{
    element = 50;
}

int main(int argc, char *argv[]) {

    int element = 0;
    cout << element << endl;
    Funct(element);
    cout << element << endl;
}

// "0" before calling Funct
// "50" after calling Funct

Hope this helps :) Anymore questions, feel free to ask!

To extend a little what phorce wrote, the pointers don't have to be pointers to arrays, but can be pointers to a single object such as a single integer. What exactly to pass to Func depends on how Func() is written. It could be either of the following, depending on the content of Func().

The second example below is very similar to the first example that phorce posted, except phorce allocated the array dynamically and I did not. Allocating the arrays dynamically is usually done when the size of the array is not known then the program is compiled, something else determines its size. Each approach is ok, as long as you realize that dynamic allocations is baggaged with the cause of many bugs and long hours debugging.

// pass pointer to a single integer
int main()
{
   int num = 1;
   Func(&num));
}

// pass an array
int main()
{
   int num[5] = {1,2,3,4,5};
  Func(num);
 }

If I have a function such as Funct(int *arg1, int *arg2) , how do I use this function in a program.

You use this function the same way you use any function, you pass the specified parameter(s) to it. If the two parameters are pointers (to an int) then those two pointers must be in existance somewhere in the calling function (main) in order to be passed to the function. For integers:

void foo(int, int)  // function prototype

int main()
{
   int x, y;  // x and y exist in main.
   foo(x,y);  //copies of x and y are passed to foo.
}

For pointers:

void foo(int*, int*)  //function prototype - foo accepts two pointers to type int.

int main()
{
   int x = 0, y = 0;
   int* ptr1 = &x;  //pointer to type int exists in main.
   int* ptr2 = &y;  //pointer to type int exists in main.

   foo(ptr1, ptr2);  //copies of the two pointers are passed to foo.
   return 0;
 }

 void foo(int* p1, int* p2)
 {
   *p1 = 10;
   *p2 = 20;
 }

Above, the function foo() assigns x back in main() the value of 10 and y the value of 20. So a copy the two pointers in main, ptr1 and ptr2, are simply passed to foo(), and since they have the addresses of x and y they can alter the content of those variables from within foo() (although they are called p1 and p2 in foo).

The term "pointer to arrays" could be somewhat misleading, depending on how you interpret it. Just as a heads-up: pointers and array aren't the same thing. One example that illustrates this:

#include <iostream>
using namespace std;

int main (void)
{
    const char* pointer = "Hello world!";
    const char  array[] = "Hello world!";

    // The address "pointer" is pointing to
    cout << "pointer:  " << (void*)pointer << endl; 

    // The address "pointer"'s value is stored at.
    cout << "&pointer: " << &pointer << endl;   

    // The memory address of the first element in "array".
    cout << "array:  " << (void*)array << endl;  

    // Also the memory address of the first element in "array"! There is no pointer stored in memory that points towards the array.
    // Could be confusing in a sense, as array == &array.
    cout << "&array: " << &array << endl;        

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.