Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder BrickInTheWall's Avatar
    Join Date
    Mar 2009
    Location
    Munich, Germany
    Posts
    139
    Thanks
    1
    Thanked 13 Times in 13 Posts

    I'm confused...pointers

    Are these both valid pointer declarations?

    Code:
    int *somepointer;
    int* anotherpointer;
    or is there a difference between the two?

    the first one is basically a dereferenced pointer right?

    Cheers,
    Chris
    Last edited by BrickInTheWall; 07-02-2009 at 10:59 PM.

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    There is no difference between those two lines functionality wise, they both do the exact same thing. And they are both valid declarations.
    OracleGuy

  • #3
    Regular Coder BrickInTheWall's Avatar
    Join Date
    Mar 2009
    Location
    Munich, Germany
    Posts
    139
    Thanks
    1
    Thanked 13 Times in 13 Posts
    Quote Originally Posted by oracleguy View Post
    There is no difference between those two lines functionality wise, they both do the exact same thing. And they are both valid declarations.
    ok so it's more of a personal preference thing?

    Initializing them would work as follows?

    Code:
    int variable1 = 5;
    int *somepointer = variable1;
    
    int variable2 = 5;
    int* anotherpointer = &variable2;
    edit: though I do think the second way would be better when having pointers as function arguments

  • #4
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by BrickInTheWall View Post
    ok so it's more of a personal preference thing?
    Yeah, it is just a style thing.

    Quote Originally Posted by BrickInTheWall View Post
    Initializing them would work as follows?

    Code:
    int variable1 = 5;
    int *somepointer = &variable1;
    
    int variable2 = 5;
    int* anotherpointer = &variable2;
    Close, see change above.
    OracleGuy

  • #5
    Regular Coder BrickInTheWall's Avatar
    Join Date
    Mar 2009
    Location
    Munich, Germany
    Posts
    139
    Thanks
    1
    Thanked 13 Times in 13 Posts
    Quote Originally Posted by oracleguy View Post
    Yeah, it is just a style thing.

    Close, see change above.
    thanks I tried it and it worked...it just kind of confuses me because
    Code:
    int *somePointer;
    looks like an integer to me and I would think I could pass a normal variable to it as is looks like a dereferenced pointer.

  • #6
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    It might look like that in a way, but it isn't and if it bothers you, just write it with the star next to the data type.
    OracleGuy

  • #7
    Regular Coder BrickInTheWall's Avatar
    Join Date
    Mar 2009
    Location
    Munich, Germany
    Posts
    139
    Thanks
    1
    Thanked 13 Times in 13 Posts
    I'll keep doing it the way I always did thanks...but it's good to know so I'll understand it when I see it in someone else's code...thanks for putting up with me


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •