tags:

views:

583

answers:

4

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.

A: 

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
A: 

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
A: 

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
+1  A: 

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

Thomas L Holaday