Question about pointers

This is a discussion on Question about pointers within the C++ Programming forums, part of the General Programming Boards category; I understand the basics of how pointers work. I can get it to store the address of anything except a ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    12

    Question about pointers

    I understand the basics of how pointers work. I can get it to store the address of anything except a string. So my question is, how can I get it to store a string's address? If I try, it tells me "cannot convert 'std::string*' to 'int*' in initialization". Thanks.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Why do you try to store the address of a string (string*) in a pointer to an integer (int*)?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    12
    Oh, I though the pointer had to be an integer. That would be my problem (don't I feel dumb lol)...

    But why does it have to be the same type as the variable? Since all pointers are the same since they only store an address, shouldn't they all be the same type?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by TyPR124 View Post
    Oh, I though the pointer had to be an integer. That would be my problem (don't I feel dumb lol)...

    But why does it have to be the same type as the variable? Since all pointers are the same since they only store an address, shouldn't they all be the same type?
    Because the compiler knows (or WANTS to know) what the pointer is pointing at.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    It's possible to use a common-for-all-pointers pointer that is called void*. But this is not recommended if you can avoid it. It's usually used in C, because there's no templates there, but in C++, you should avoid them because the compiler will lose all type information about the type when you put it into a void* pointer.
    Also, you can't derference a void* pointer since the compiler doesn't know its type.
    But as matsp explained, it's because the compiler wants to know the type of object the pointer points to.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, to clarify my and Elysia's potentially confusing statements:
    1. a void * is compatible with "any pointer" in C. C++ is a bit more restrictive.
    2. The compiler enforces the setting of pointers to "equivalent types" because it doesn't think it makes sense to take an int * and let it point to a string - because a string and an integer are not "compatible types".
    3. The type of a pointer is only really there for two purposes:
    - So that the compiler can access what the pointer points to - for example, a char * fetches [usually] a byte (8 bits) if you access the "pointed to" data, whilst an int * fetches a 32-bit value.
    - For type checking - so you don't mistakenly set a pointer to point to something unsuitable. If you set a pointer to int to point to an array of char, you won't get a sensible integer out of that - so the compiler tells you when you "mix things up that aren't supposed to be mixed up".

    You can, if you really think you know better than the compiler, use something called casts [1] to tell the compiler: "Look, I know that you don't think this makes sense, but I do really want to set this int pointer to the address of a string".

    [1] The name cast is more of "casting a spell" to "magically convert this as is necessary" than "cast of plaster" that holds your broken leg in place.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Casting a spell is a pretty good analogy ("Remember the wizard Baruffio, who said f instead of s, and ended up with a buffalo on his chest."), but the word comes from casting iron or similar substances into a different form.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    I thought it was typecasting, like when you typecast somebody. Or do I have them mixed up?

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    12
    Well thanks for all the answers... I think I understand now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array pointers question?
    By ben2000 in forum C Programming
    Replies: 4
    Last Post: 07-26-2007, 01:31 AM
  2. A question on Pointers & Structs
    By FJ8II in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2007, 10:56 PM
  3. simple pointers question
    By euphie in forum C Programming
    Replies: 4
    Last Post: 05-25-2006, 01:51 AM
  4. Very stupid question involving pointers
    By 7smurfs in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 05:15 PM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM

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