casting contents of pointer?

This is a discussion on casting contents of pointer? within the C Programming forums, part of the General Programming Boards category; I'm using a pointer to type int inside a function and trying to shift the contents right 8 and cast ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    8

    casting contents of pointer?

    I'm using a pointer to type int inside a function and trying to shift the contents right 8 and cast to a char, to extract a byte for the upper address.
    I get a run-time error.

    This seem logical to me, can I not cast a dereferenced pointer?

    Code:
    funct1(unsigned int *address)
    {
       funct2( (unsigned char) ((*address)>>8) ) )//upper address
       //funct2 expects byte
      
       func2( (unsigned char) *address )//lower byte
    }

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    You can do that. The question is, should you do that?
    I believe the error lies in some other part of the code you are not showing us.
    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
    Nov 2008
    Posts
    8
    It's embedded with c18. If you want I could still post a rather large chunk; however I think the rest would be better suited to a microchip forum.

    I posted here because it is a simple snippet in C.

    Thanks!

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Then let me ask you - does the function want the high or low bits of the pointed-to contents or of the address itself?
    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
    Nov 2008
    Posts
    8
    it should be the high bits(byte) for the first funct2 call, then the low bits(byte) for the second funct2 call.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    8
    the contents not the address - i should have used a different variable name as int address points to a int which contains an address that is used to store to a physical address in funct2

    eg. the chip expects an upper and lower address to access data stored.
    Last edited by nutsNguts; 11-10-2008 at 03:03 AM.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Assuming sizeof(int) == 2 (sizeof(int) == 4 on my machine, so I used short which is 2 bytes), and assuming that the result you expect from this code is:

    s 0x1234 short
    lower 0x34 char
    upper 0x12 char

    Code:
    int main()
    {
    	short s = 0x1234;
    	char lower = (char)s;
    	char upper = char(s >> 8);
    }
    Then the code is right and there is no error here.
    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.

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    8
    int == 4 for c18

    I found the error at that line by debugging, but I believe the error is within funct2. It may be a timing or config. issue.

    Thanks very much!

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    > I get a run-time error.
    That's useful - if you assume we're all mind readers.

    > It's embedded with c18.
    Again, more useful information not in the original post.
    Are you working down at the bare metal, or is there some kind of OS / runtime associated with that?


    > can I not cast a dereferenced pointer?
    I dunno - usually.

    One example of where you cannot dereference a pointer to an int is if you've been doing some hooky pointer arithmetic on say a char array before hand.
    Some machines only allow you to access an int on an aligned address, and the penalty for trying otherwise is a bus error.

    Eg.
    Code:
    char buff[10];
    unsigned int *hooky = (unsigned int*)&buff[1];  // probably an odd address
    funct1( hooky );
    On some machines, you're just going to get your ass handed back to you on a plate as soon as you try and dereference the pointer.

    Or maybe something else weird happens, like it just truncates the address, and gives you that instead. It's not a crash, but it isn't what you want either.


    So yeah, maybe not all the code, but some kind of context in which to judge the question would certainly help. Posting a single line and asking "what's wrong with this line" is seldom productive.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Pointer type casting
    By vlrk in forum C Programming
    Replies: 2
    Last Post: 08-11-2008, 07:33 AM
  3. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  4. How can I set a file pointer to zero? Casting?
    By INFERNO2K in forum C Programming
    Replies: 6
    Last Post: 07-06-2005, 12:04 AM
  5. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM

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