how do you read this pointer?

This is a discussion on how do you read this pointer? within the C Programming forums, part of the General Programming Boards category; how do you read this pointer? Code: *(char *)ptr...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    28

    how do you read this pointer?

    how do you read this pointer?

    Code:
    *(char *)ptr

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    the value at the location pointed to by ptr when ptr is interpeted as a pointer to a character.

    In a long winded way of course

  3. #3
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Basically (I'm pretty sure) it would be the same as this:
    *((char *)ptr)
    It converts ptr into a char * and is the value at that converted pointer.
    Do not make direct eye contact with me.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I wouldn't use the world convert. Casting is a temporary change in interpretation while conversion implies some level of permanence.

    Edit: Short winded version:

    treat ptr as a pointer to a char and retrieve the value at the location pointed to.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >I wouldn't use the world convert.

    I would.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    One of the few situations where one may convert a pointer of one type to another type and then get the value I could think of is
    Code:
    #include <stdlib.h>
    
    typedef struct
    {
    	double d;
    	char c;
    } bstruct;
    
    
    
    
    
    
    int
    main(void)
    {
    	bstruct *b;
    	double db;
    	b=malloc(sizeof *b);
    	b->d=1.2;
    	b->c='a';
    	/*some programming statements later*/
    	db= *((double *) b);
    	printf("%f\n",db);
    	/*some more statements*/
    	free(b);
    	return 0;
    }
    This will work for only for the first member of a structure. Similar things mighr be done for a union too. Another example I can think of is casting the pointer to an object to an unsigned char * and then looking at the bytes that make up the object
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    
    
    int
    main(void)
    {
    	double d=1.5;
    	unsigned char *pc;
    	size_t i;
    	pc=(unsigned char *)&d;
    	for(i=0;i<sizeof d;i++,pc++)
    		printf("%X ",(unsigned) *pc);
    	return 0;
    }
    Any other examples?
    Last edited by pinko_liberal; 05-26-2004 at 01:16 PM.
    The one who says it cannot be done should never interrupt the one who is doing it.

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    @pinko: Do you mean something like this?
    Code:
    float x = 5.6f;
    printf("%d\n", *(int *)&x);
    The output won't be 5 I can tell you that

    @Dave: I read the link and understand what you are trying to say. Now if you go back and re-read my post I think you'll see why I favor against using the word convert, especially for pointers.
    Look at the example I just gave. Did x change value? No. Did the bit pattern change? Nope. Only thing that changed is how we are interpeting the bit pattern.
    I'll admit that there are some cases where casting is more like convertion and in those cases it might be the better term. This isn't one of those cases.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Thantos
    @Dave: I read the link and understand what you are trying to say. Now if you go back and re-read my post I think you'll see why I favor against using the word convert, especially for pointers.
    Look at the example I just gave. Did x change value? No. Did the bit pattern change? Nope. Only thing that changed is how we are interpeting the bit pattern.
    On your implementation, perhaps there was no noticeable change. But this is not necessarily always the case. Different objects can have different pointers that do indeed have different representations. For example, a char* could have a different representation than an int*. Program for the DS9000, not a friendly PC.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You care to prove your statement with something other then a vain attempt at humor?

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Spend some time Googling comp.lang.c: here's a start.

    While there, you may discover that the DS9000 is a reference to an imaginary machine that implements the C standard, but undefined behavior would always result in disaster: a most rigorous test environment for C code where faulty assumptions die a cruel death.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    On an imaginary machine
    Code:
     int x = 0;
    could result in an error. Show me something in reality and I might care.

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    This is the last time I do the Googling for you.

    http://groups.google.com/groups?hl=e...3DN%26tab%3Dwg

    [edit]And this explains why I'm feeling deja vu.
    Last edited by Dave_Sinkula; 05-26-2004 at 04:14 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    [QUOTE=Thantos]@pinko: Do you mean something like this?
    Code:
    float x = 5.6f;
    printf("%d\n", *(int *)&x);
    [QUOTE]

    I was wondering what kind of pointer conversions are compatible ,other than conversions to void *
    The one who says it cannot be done should never interrupt the one who is doing it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 02:17 PM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. unable to read double A[0] and A[1] when n=1
    By sweetarg in forum C Programming
    Replies: 2
    Last Post: 10-25-2005, 12:35 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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