0

Duplicate:

Uses for multiple levels of pointer dereferences

I have a question about C and pointers.

I know when I would need a pointer, and even when I might need a pointer to a pointer. An example would be if I had a linked list, and I wanted to write a function that would remove an element of that list, to do so I would need to send a pointer to the pointer of the head of the list.

How about a pointer to a pointer to a pointer? Would there ever be a situation where that would be needed? Extra points if you have some sample code so I can really get my head around it.

Community
  • 1
  • 1
Alan H
  • 2,713
  • 4
  • 22
  • 22

4 Answers4

2

A vector is *, an array is **, a volume is ***, a timespace is ****.

Thomas L Holaday
  • 13,068
  • 5
  • 38
  • 50
0

Extremely rare, but you could imagine some kind of reference counting scenario, where you needed to get to some other object's address of pointers and change it.

Cade Roux
  • 83,561
  • 38
  • 170
  • 259
0

What about a function to remove an element from a linked list in an array of linked lists, or a function to remove an element from a linked list in an array of arrays of link... you get my point.

James
  • 351
  • 1
  • 2
  • 6
0

Pointers are arrays in a sense. Here's a straightforward scenario. Say you were building a text editor. Lines and columns are both dynamic.

You might have an "array" of lines, and since they're dynamic, that would be a pointer. Then your columns would also be dynamic and might be contained inside of your line array. So in essence:

char **lines;

You would have to malloc your line first before adding characters, but this could provide a very crude means of an editor.

lines = malloc(num_of_lines_in_my_file);
lines[0] = malloc(num_of_chars_for_line_1);

Certainly not beautiful code, but hopefully it helps a bit to answer the question.

billb
  • 3,519
  • 1
  • 29
  • 36