• Advertisement
Sign in to follow this  

question about pointers

This topic is 3627 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a question on pointers. I have been reading a book on c++,and it was showing me alittle about overloading functions. I understand the overloading part of it, but there's only one thing I don't understand.On one of the overloaded functions it has a ** to one of the parameters.Whats the reason for that? Her is the code. //code begin // print_arrs.cpp #include <iostream> using namespace std; void print_arr(int *arr, int n); void print_arr(double *arr, int n); void print_arr(char **arr, int n); int a[] = {1, 1, 2, 3, 5, 8, 13}; double b[] = {1.4142, 3.141592}; char *c [] = {"Inken", "Blinken", "Nod"}; int main() { print_arr(a, 7); print_arr(b,2); print_arr(c, 3); return 0; } void print_arr(int *arr, int n) { for (int i = 0; i < n; i++) cout << arr << " " ; cout << endl; } void print_arr(double *arr, int n) { for (int i = 0; i < n; i++) cout << arr << " " ; cout << endl; } void print_arr(char **arr, int n) { for (int i = 0; i < n; i++) cout << arr << " " ; cout << endl; } // code end

Share this post


Link to post
Share on other sites
Advertisement
void print_arr(int *arr, int n);

It's a function that take an int* arr and an int n.

Arguments:
int n: It's easy to understand only an int.
int *arr: It's a pointer to int, you pass the int's address memory


void print_arr(char **arr, int n);

It's a function that take an int *arr and an int n
int n: It's esasy to understand, only an int.
int **arr: It's a pointer to an int pointer, you pass the POINTER TO INT's adress memory.

Example:

int* a = 0;
int j = 6;
int n = 5;

a = j; // A points to j

// First Function
print_arr(a, n); // 1st argument: you pass the int's address memory pointed by a
// j adress memory

// Second Function
print_arr(&a, n); // 1st argument: you pass the POINTER TO INT's adress memory
// a adress memory

One adress is for a int's memory location and the other adress is for pointer to int's memory location

Share this post


Link to post
Share on other sites

'*' means a pointer to, or conventionally, the address of. I single '*' means a pointer to or address of the variable supplied. It could also be the start of an array of such variables.

'**' means the same thing, but once removed. Its a pointer to a pointer, or clearer: a pointer to an address of a variable. It could also be the start of an array of addresses for a given variable type. This means 'char **p' makes p a pointer to a char*, or an array of char* values.

Does this make sense?

Share this post


Link to post
Share on other sites
One reason you might pass a char** to a function is so you can do something like this:

void print_arr(char **arr, int n);
char bob[12][6];
print_arr(bob, 17);

See? You pass 2D arrays as **, 1D arrays as *.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement