simple question regarding pointers..

This is a discussion on simple question regarding pointers.. within the C++ Programming forums, part of the General Programming Boards category; Hey guys.. I'm hoping for some clarification on this situation i created. i was playing around with pointers and this ...

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

    simple question regarding pointers..

    Hey guys.. I'm hoping for some clarification on this situation i created.

    i was playing around with pointers and this situation came up.
    (please forgive me if im just being absent minded and it's a syntax error.)

    Code:
    char* b_stringp;
    b_stringp = "abcd";
    cout << b_stringp << endl;
    the above code does just as expected and outputs "abcd"

    then i tried to do a similar thing with a int
    Code:
    int* b_int;
    b_int = 234;
    cout << b_int << endl;
    im a little confused as to why this code didn't work?? it seems to have the same logic as the previous code.

    then i thought to fix this problem would be to dynamically allocate memory.
    Code:
    int* a_int;
    a_int = new int;
    a_int = 234;
    cout << a_int << endl;
    and realized i had to put an astrix infront

    this code tho did work
    Code:
    int* a_int = new int;
    *a_int = 234;
    cout << *a_int << endl;
    from my understading this actualy makes a pointer that points to a area of memory labeled 234.

    anyways i was hoping someone could clarify the above for me.. also show me how to properly make an int * and on another line dynamically allocate memory to it. and then insert data into it and "cout" it.. hope i can get some clarifying here

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    [EDIT as per Todd's post]I would have to say a pointer represents an address, and points to a location in memory.[/EDIT]

    When you code something like a string literal (i.e. "I am awesome") your compiler will place that string literal inside your executable and interpret a pointer to that string literal as the address of that memory location.

    Think of it like putting it in a filing cabinet and knowing its label. So when you are dealing with a string that is all well in good. But when you say something like int *x = 52 you are basically saying that the label inside the filing cabinet is 52, give me whatever is inside that folder. So instead of printing out "52" it goes to the memory address 0x34 and tries to read whatever int may reside there (which will likely cause an access voilation).
    Last edited by master5001; 09-24-2008 at 04:22 PM.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by master5001 View Post
    Well a pointer points to an address in memory.
    I would have to say a pointer represents an address, and points to a location in memory.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yeah I think that is a better wording. I was trying to work in some genious analogy. Needless to say, Elysia's house address analogy was a little more coherently formed.

    **Changed with due credit**
    Last edited by master5001; 09-24-2008 at 04:23 PM.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    char* b_stringp;
    b_stringp = "abcd";
    cout << b_stringp << endl;
    is different from the code where you use int * in the sense that char * is a special case of pointers - they are generally seen as strings, and cout will take a char * and print the letters point to by the pointer, rather than printing the value of the pointer.

    If we really want to, we can set a pointer to a value that is not a valid memory location - but of course, that means that if we dereference the pointer, the code will crash. For example:
    Code:
    int *ptr = reinterpret_cast<int *>(234);
    cout << ptr << endl;
    That will print 234 (but in hex, so it will be something like 0xEA [0xF0 == 240, subtract 6 => 0xEA - I haven't verified with a calculator], possibly with a load of zeros at the front and no 0x).

    Of course, if we do *ptr = 7 after that, it will give an "invalid memory access", unless it's DOS or some embedded OS that lacks the bigger OS's memory protection which prevent errant pointers from being used.

    --
    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.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    47
    thanks for the reply guys..

    did anyone have a way of
    showing me how to properly make an int * and on another line dynamically allocate memory to it. and then insert data into it and "cout" it

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by MegaManZZ View Post
    thanks for the reply guys..

    did anyone have a way of
    showing me how to properly make an int * and on another line dynamically allocate memory to it. and then insert data into it and "cout" it
    Yes. You did, in fact. Copying and pasting from your post:
    Code:
    int* a_int;
    a_int = new int;
    *a_int = 234;
    cout << *a_int << endl;

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    This may help some:
    Memory Functions - Still Learning
    Remember to use new/delete instead of malloc/free.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers Question
    By HAssan in forum C Programming
    Replies: 2
    Last Post: 09-08-2008, 10:17 AM
  2. simple question about pointers and casting
    By steve1_rm in forum C++ Programming
    Replies: 3
    Last Post: 03-28-2008, 02:25 PM
  3. Simple Half-Life Coding Question
    By bengreenwood in forum Game Programming
    Replies: 1
    Last Post: 11-07-2007, 01:18 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Pointers Question.....Null Pointers!!!!
    By incognito in forum C++ Programming
    Replies: 5
    Last Post: 12-28-2001, 10:13 PM

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