Like Tree1Likes
  • 1 Post By AndrewHunter

typecasting a char pointer

This is a discussion on typecasting a char pointer within the C Programming forums, part of the General Programming Boards category; Hello people, How do u add 2 numbers without using the plus operator. This is 1 of the many methods ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    18

    typecasting a char pointer

    Hello people,
    How do u add 2 numbers without using the plus operator.
    This is 1 of the many methods i came across.
    I allllmost got it, but not quite.

    This is the code i followed
    -------------------------
    Code:
    #include< stdio.h >
    int main()
    {
    int a=30000,b=20,sum;
    char *p;
    p=(char *)a;
    sum= (int)&p[b]; //adding a & b
    printf("%d",sum);
    return 0;
    }

    This is my understanding until now
    ---------------------------------
    1)The number 30000 is converted to a char pointer. So if u inspect p, it comes to 0x7530.
    2)And b==20==0x14.
    3)Now p[b] == *(p+b) , and &p[b] == &*(p+b) == p+b
    4)Now p+b holds 0x7530 + 0x0014 = 0x7544
    5)Then typecast 0x7544 into an int == 30020 == CORRECT SUM

    My only doubt is , how can we convert an integer to a pointer like that. ( i mean , typecasting 30000 to 0x7530)
    I mean, thats weird.
    Is the answer that i just have to believe it.
    Can some1 lend more light on this topic and probably even tell me a quick line about typecasting ?
    Last edited by tubby123; 07-12-2011 at 12:29 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. All of that is terribly wrong.

    p[ b ] is *(p + b) is p[ 20 ] is *(p + 20) is 20 bytes past p which is no where you should be trying to access.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Your little 'magic' trick only worked because a char is 1 byte. Hence a direct similarity to what you set out to do, e.g. add 20. What in reality you are doing is playing around with memory that isn't allocated and thus not yours to play with. Listen to quzah. There are a lot of 'tricks' you can do in programming languages using knowledge of how compilers work and playing odds. These all generally result in undefined behavior and should not be used for any real program.
    Salem likes this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    What that code does will probably generate the right machine instructions to end up giving you the result you want. However it's really not nice.

    However when you do that kind of thing, you're basically like the guy at the top of this ladder... http://www.funatiq.com/images/crazy-men-and-ladders.jpg
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    This is a dodgy little trick that exploits the fact that the size of a char is (usually) 1. As Quzah says, it's bad form to do anything involving memory you haven't allocated.

    In this case it's pretty harmless though, because nowhere does this code change or look at information at those unallocated memory locations- just their addresses.

    If you want a really cheesy way of avoiding the + operator:
    Code:
    c = a - ( 0 - b );

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by TheBigH View Post
    char is (usually) 1
    Always.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer and typecasting question
    By A34Chris in forum C Programming
    Replies: 4
    Last Post: 04-28-2011, 09:14 AM
  2. Copy char array to char pointer
    By Suseela in forum C Programming
    Replies: 9
    Last Post: 08-06-2009, 01:49 PM
  3. Pointer Arithmetic and Typecasting
    By pobri19 in forum C Programming
    Replies: 2
    Last Post: 03-20-2009, 12:06 AM
  4. Replies: 11
    Last Post: 08-28-2008, 05:10 AM
  5. Typecasting a void* to a function pointer
    By Procyon in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2004, 05:43 PM

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