Pointer Help.

This is a discussion on Pointer Help. within the C++ Programming forums, part of the General Programming Boards category; Hi, So, i'm just working through pointers and I have the following very basic piece of code: Code: char ch ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    329

    Pointer Help.

    Hi,

    So, i'm just working through pointers and I have the following very basic piece of code:

    Code:
     char ch = 'c';
      char* pc = &ch;
    
      cout << pc << endl;
      cout << *pc;
    I am expecting *pc to print out the value of ch, which it does. Also, I am expecting pc to print out the memory address, but using the console output all I see is what can best be described as heiroglyphics? Should it not be a Hex address?

    Thanks.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    A char* is interpreted as a C-style string. A string is an array consisting of N characters and a '\0' at the end. Clearly you don't have a string, so you gibberish instead. Whatever lies in the memory address at the point that you're printing it. You're lucky it doesn't crash.
    You have to cast it to some other type to get it to work, eg:
    Code:
    cout << reinterpret_cast<int*>(pc) << endl;
    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.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by Elysia View Post
    A char* is interpreted as a C-style string. A string is an array consisting of N characters and a '\0' at the end. Clearly you don't have a string, so you gibberish instead. Whatever lies in the memory address at the point that you're printing it. You're lucky it doesn't crash.
    You have to cast it to some other type to get it to work, eg:
    Code:
    cout << reinterpret_cast<int*>(pc) << endl;
    Cheers, i think I understand now.

    Also, could you advice on this code from the book I am using. It is showing how to use pointer to create a doubly linked list and I am completely lost.

    This is the struct:
    Code:
    struct Link {
    		string value;
    		Link* prev;
    		Link* succ;
    		Link(const string&v, Link* p = 0; Link*s = 0)
    			:value(v), prev(p), succ(s) {}
    	};
    I understand that the value is the name of the link, with prev and succ being pointers to the predecessor and successor link.

    Now the bits I don't understand are twofold, regarding inserting into the list. The book shows two ways of doing this, a long way, followed by a function.

    The long way is:
    Code:
    Link* norse_gods = new Link("tHOR", 0, 0);
    	norse_gods = new Link("Odin", norse_gods, 0);
    	norse_gods->succ->prev = norse_gods;
    	norse_gods = new Link("Freia", norse_gods, 0);
    	norse_gods->succ->prev = norse_gods;
    And the function is:
    Code:
    Link* insert(Link*p, Link*n) //insert n before p
    	{
    		n->succ=p;
    		p->prev->succ = n;
    		n->prev=p->prev;
    		p->prev = n;
    		return n;
    	}
    The parts that I am completely flummoxed with, in particular, is norse_gods->succ->prev = norse_gods and also understanding how the function works?

    Do you have any tips regarding this?

    Thanks,

    Darren.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    I cannot really see the purpose of the "long" way because it's broken.
    Memory leaks. Dereferencing uninitialized pointers.

    But the function is straight forward.
    Here's a little schematic of how things look (the first low indicates the next pointer and the second row the previous pointer):

    prev --> next
    prev <-- next

    Now, we want to insert an element in between these. So the first thing we do is connect the new element's next pointer to the next element in the list which it will appear before (next in this example):

    prev --> next
    prev <-- next

    new --> next
    ? <-- new

    Now, the element before new, prev must be set to point to new with its next pointer. How do we find prev? Remember that next's prev pointer points there. So we first go next->prev to get prev in the example. Then we set the next pointer there to new, so in essence it becomes next->prev->next = new. So we get:

    prev --> new --> next
    prev <-- next

    new --> next
    ? <-- new

    Now we make sure that new's prev pointer points to prev. Remember again that next->prev points there, so we set new->prev = next->prev, and we get:

    prev --> new --> next
    prev <-- next

    new --> next
    prev <-- new

    Finally, we set next's prev pointer to the inserted node new, and so we get:

    prev --> new --> next
    prev <-- new <-- next

    new --> next
    prev <-- new

    And we're done. "next" is p in the function and "new" is n. The previous element prev is p->prev.
    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.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    That's great. Thanks for taking the time.

    So, if I understand correctly, where it says (in the function), p->prev->succ=n; is that saying that Link p, set its previous Link, succ pointer to n? Is that correct?

    Cheers

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    It sets the pointer previous to p's successor to n, yes.
    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.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Great. Thanks again.

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    void* is the generic pointer of pointers. If you just want a memory address to display, that is the appropriate type to cast the pointer.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    I disagree. void* is an evil construct inherited from C. It's dangerous and has no place in C++. At all times, you will want a variable to have a type. Only char* is a special type, but it can be cast into something else. Although usually we don't need to print the address of pointers.
    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.

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    This particular use of void* is completely benign. It doesn't make sense to cast to a useable pointer type just for an address.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    *shrug*
    I avoided it just because void* is evil.
    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. Pointer to a function pointer
    By @nthony in forum C Programming
    Replies: 3
    Last Post: 05-30-2010, 05:13 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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