variables and astrisks

This is a discussion on variables and astrisks within the C Programming forums, part of the General Programming Boards category; I see it at times when I get build errors and I see it here as well. Code: int example; ...

  1. #1
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582

    variables and astrisks

    I see it at times when I get build errors and I see it here as well.

    Code:
    int example; // a normal variable
    int *examplepointer; // a basic pointer
    int **unknown; // what is this?
    The first two are commonly seen. The top is a normal variable. The one below it is a pointer, but the second one, though I'm not sure of the formatting, I see errors like this:

    "cannot convert something to (**void)"

    What does the ** represent, what is it for, and how is it used?
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Well one would think, a pointer to a pointer

    Consider:
    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
    	int example; 		/* a normal variable */
    	int *examplepointer; 	/* a pointer */
    	int **unknown; 		/* pointer to a pointer */
    	int ***unknown_even_more;	/* pointer to a pointer to a pointer :) */
    
    	example = 5;
    	*examplepointer = example;
    	unknown = &examplepointer;
    	unknown_even_more = &unknown;
    
    	printf("example = &#37;d, examplepointer = %d, unknown = %d, unknown_even_more = %d", example, *examplepointer, **unknown, ***unknown_even_more);
    	
    	return 0;
    }
    I gather you see the trend ? :P
    Last edited by zacs7; 05-10-2007 at 07:27 AM.

  3. #3
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    A pointer to a pointer? Strange. What sort of case would that be used for? I understand the use of pointers to a variable, but a pointer to a pointer doesn't seem to have any use as it seems rather bizarre. That could be why it's quite rarely seen. The tutorial on this site didn't cover or mention this as I just checked barely 20 minutes ago.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You can also have a pointer to a pointer to a pointer to a pointer... I don't know how many you can have but meh...

    It can be used for example when allocating 2d arrays on the heap, or 3d (pointer to a pointer to a pointer), 4d etc..
    Last edited by zacs7; 05-10-2007 at 07:43 AM.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    > A pointer to a pointer? Strange. What sort of case would that be used for?
    Like argv of main perhaps - that is such an animal.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21