• 0

pointers: two asterisks


Question

I'm reading Object-Oriented programming in c++ by Roert Lafaore and see something like

void bsort(person**, int);

I'm like wtf -- never seen that before and all it really says is: The address of a pointer is a pointer to a pointer. That didn't really help. Can anyone elaborate on this?

Link to comment
Share on other sites

21 answers to this question

Recommended Posts

  • 0

That's exactly what it is. A pointer to a pointer.

For example:

int n = 5;

int *p = &n; //p contains the address of n

int **p2 = p; //p2 contains the address of p

Mainly that's good for creating arrays of arrays (arrays of C-style strings, for example).

Link to comment
Share on other sites

  • 0

This is a rather specific example but it may click the light-bulb on:

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

is equivalent to:

int main(int argc, char** argv)

Link to comment
Share on other sites

  • 0
That's exactly what it is. A pointer to a pointer.

For example:

int n = 5;

int *p = &n; //p contains the address of n

int **p2 = p; //p2 contains the address of p

Mainly that's good for creating arrays of arrays (arrays of C-style strings, for example).

Shouldn't it be:

int **p2 = &p;

with the code you posted p2 is assigned the value of p, which is the address of n, not the address of p.

Link to comment
Share on other sites

  • 0
Shouldn't it be:

int **p2 = &p;

with the code you posted p2 is assigned the value of p, which is the address of n, not the address of p.

Actually it wouldn't even be assigned, it would be a compile error.

Link to comment
Share on other sites

  • 0
Shouldn't it be:

int **p2 = &p;

with the code you posted p2 is assigned the value of p, which is the address of n, not the address of p.

Yeah, my bad. Was thinking of address-of, not address-of-address-of. *L* I do my best not to have to use that kind of code if I can at all avoid it.

Link to comment
Share on other sites

  • 0

So now my question is there any time where I MUST use it. Krestel's example made some since as seeing -- the same as -- examples if they apply help. But up till now I swear I've never seen nor used it in classes, arrays, and the few TSL templates I've used.

Link to comment
Share on other sites

  • 0
This is a rather specific example but it may click the light-bulb on:

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

is equivalent to:

int main(int argc, char** argv)

:lamp:

Thanks man, that makes a bit more since, but still don't see why you need it since pointers already are confusing enough.

Link to comment
Share on other sites

  • 0
:lamp:

Thanks man, that makes a bit more since, but still don't see why you need it since pointers already are confusing enough.

You need it for dynamic memory allocation. Now you can say like:

int **array;

array = new array[newsize][newsize]; //And now you have a 2D array with a new size

(I believe thats close to correct syntax... its been awhile, correct me if i'm wrong please)

So useing "new" you can resize your arrarys.

Link to comment
Share on other sites

  • 0
boNk so are you saying that those two mean the same thing? I appreciate all the help guys.

Nope. Basically, there are two ways to declare an array; dynamically or statically.

If you did:

int myArrary[10][10];

This would make an array with a max size of 11 (remember that the first index is '0'). Now lets say you wanted to make myArray have a max size of 100. You couldn't because its not a pointer to an integer.

Now if you did:

int **myArray[10][10];

This would make an array with max size of 11 just the same as before. In fact, its used just the same (because the '[]' operators dereference). Now lets say you wanted myArray have a max size of 100. You could, using new:

myArray = new int[99][99] //I'm pretty sure I got the syntax this time

//"new" only works on pointers to objects(variables).

And you would have a newly sized array! Just remember that the data from the old array doesn't carry over. So you would have to write a function to do so.

In all those STL classes that you use(vector, matrix, string, stack... etc). They take care of the memory for you.

I hope that clarifies some things! :)

Edited by b0nk
Link to comment
Share on other sites

  • 0

Kickin thanx, it helped a lot more than my book did. Do you have any recommendatons for learning pointers as in books, websites,etc? The books I have only dedicate a chapter (if that) or a few pages.

Link to comment
Share on other sites

  • 0
Kickin thanx, it helped a lot more than my book did. Do you have any recommendatons for learning pointers as in books, websites,etc? The books I have only dedicate a chapter (if that) or a few pages.

Only a chapter? That crazy!

Pointers are one of C++ most powerful features. Aside from dynamic memory allocation, you can make a lot of cool data structures like, linked lists and trees.

The book I learned from was Data Structures and Other Object Using C++ by Main and Savitch. However, it a bit costly.

You should check this out:

http://richardbowles.tripod.com/cpp/linklist/linklist.htm

Try to master the linked list data structure. It has many advantages over the array type data structure, and its a lot more... exciting :D

Although it is not entirely necessary to learn how to use pointers with all the STL classes out there, C++ really opens up to you after you learn how to use them.

Link to comment
Share on other sites

  • 0

ok basicaly the contense of the address held in the pointer,

well the contence of that address is another pointer

and thats about it

if you under stand pointer

PS prob havent explaned it very well

Link to comment
Share on other sites

  • 0

Why are strucutres commonly used with linked lists? Are classes allowed or do the private section of the class interfer with the ideology of the linked list?

Link to comment
Share on other sites

  • 0

So when it comes to it are these equivalent?

char* days[] = {"Mon", "Tue", "Wed", "Thu", "Fri"};

-----------------------------------------------------------

char** days = {"Mon", "Tue", "Wed", "Thu", "Fri"};

Link to comment
Share on other sites

  • 0
Why are strucutres commonly used with linked lists?
Linked lists are used for a number of reasons. Say you wanted to insert an element into the first index of an array, you would have to copy all the data from the first index on down one space and then insert the element. Now if you dealing with an array of 1000 elements, that would mean that you would have move to 1000 elements. That is pretty inefficient.

Now if you had a linked list all you would do is swap around 3 pointers.

Thats basically the main reason why people would use a linked list over an array. They are also completely dynamic, when you want to add more data to your list you just make a new node.

Basically the only drawback to a linked list is when you know exactly you want to go. With an array you would just specify the index, but with a linked list you would have to traverse the entire list until you get to where you want. A clever trick I learned was to make an array of pointers to nodes that ran parallel to the linked list. That way you get the best of both worlds! :D

So to recap that, the reason why linked lists are commonly used because you can insert data efficiently.

Are classes allowed or do the private section of the class interfer with the ideology of the linked list?

Not really sure what you mean. Linked lists tie into classes nicely.

So when it comes to it are these equivalent?

char* days[]= {"Mon", "Tue", "Wed", "Thu", "Fri"};

-----------------------------------------------------------

char** days = {"Mon", "Tue", "Wed", "Thu", "Fri"};

yep

Link to comment
Share on other sites

  • 0
Why are strucutres commonly used with linked lists? Are classes allowed or do the private section of the class interfer with the ideology of the linked list?

A class is basically a Structure (the variables) + Functions.

Structures are used because then you can set multiple variables through one pointer. Classes work equally as well. In fact you should use a Class if you use C++, and structures for C.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.