Thread: type casting in pointers

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    5

    Question type casting in pointers

    Code:
    #include<stdio.h>
    
    int main(){
    
    int *ptr=( int *)1000;
    
    ptr=ptr+1;
    printf(" %u",ptr);
    
    
    return 0;
    }
    what does int *ptr=(int *)100 mean... i know its something to do with typecasting.
    well output is 102... it's clear tat ptr initially is 100 and after ptr+1 it is now 102. But how is it represented in memory. please clear my doubts. thank you

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It means "pretend that this number is really the address of a pointer to an integer". This will answer your second question:
    Code:
    printf( "sizeof int = %d\n", sizeof( int ) );

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

  3. #3
    1337
    Join Date
    Jul 2008
    Posts
    135
    Probably your sizeof(int) is 2 bytes. What is your platform?

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    It is the compiler, guaranteed to be Turbid Crap
    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.

  5. #5
    Registered User slash.hack's Avatar
    Join Date
    Sep 2011
    Posts
    21
    I'm not very experienced with pointers but looking upon your code, it seems that by

    int *ptr=( int *)1000;

    you are assigning address for an integer type data.

    ptr=ptr + 1;

    caused the address to add up as per the pointer arithmetic.

    printf(" %u",ptr);

    I'm pretty sure you know what this statement does, its trying to echo the pointed address stored in the pointer variable ptr.
    Compiling it with Codeblocks echoed out the result 1004 and with Turbo C the result was 1002. This is because Codeblocks works full 32 bit as per my PC and Turbo C in 16 bit.

    By the way, this is an amateur explanation and I have interpreted in regard to my understanding.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    5
    win7 using turboc++ 1.4

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Aryan Sahu View Post
    win7 using turboc++ 1.4
    My condolences ... twice.

    You won't have much choice but to live with Win7... but you really should get a better compiler...
    If you're only interested in C programming (no C++) your best bet is probably Pelles C... smorgasbordet - Pelles C

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > win7 using turboc++ 1.4
    Free Developer Tools - Visual Studio 2010 Express | Microsoft Visual Studio
    smorgasbordet - Pelles C
    Code::Blocks
    Yes, we all know your educational institutions are stuck in the stone age of computing. Feel free to play along with them, but get some real education for yourself in the meantime by learning what is used in the REAL WORLD. The "certificate" you get at the end will only be good enough to get you your first job. It will be useless from that point on, as what will count is what you know about all the modern stuff.

    Don't be one of these
    India Graduates Millions, but Too Few Are Fit to Hire - WSJ.com
    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.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You've kidding right?
    TurboC++ 1.4 was released in 1991, that's 4 years before even Windows 95 was released. Twice that long ago they were still using punch cards to program! It is literally listed as Antique software and is only available in embarcadero's software museum.
    I mean seriously, how does that even run on a modern PC?!
    Must be a history buff...
    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"

  10. #10
    1337
    Join Date
    Jul 2008
    Posts
    135
    Quote Originally Posted by iMalc View Post
    I mean seriously, how does that even run on a modern PC?!
    Windows 7 is backward compatible

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by valthyx View Post
    Windows 7 is backward compatible
    Neither Turbo C nor the 16 bit executables it creates will run on any 64 bit version of Windows.
    You will simply get an error saying the program image is invalid.

  12. #12
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by CommonTater View Post
    You won't have much choice but to live with Win7...
    Of course you have ! It is all about choices.

  13. #13
    Registered User slash.hack's Avatar
    Join Date
    Sep 2011
    Posts
    21
    Yes, its a pain in the .... when it comes to running Turbo C in a 64 bit PC. My friends had a lot of trouble trying to get it to run, they even tried DosBox, until I suggested the DevC++, but I use CodeBlocks though

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by slash.hack View Post
    Yes, its a pain in the .... when it comes to running Turbo C in a 64 bit PC. My friends had a lot of trouble trying to get it to run, they even tried DosBox, until I suggested the DevC++, but I use CodeBlocks though
    Code::Blocks is not a compiler... it is an Integrated Developer's Environment... basically a glorified text editor. Code:Blocks is supplied from the developer's site with a copy of MinGW which is a 32 bit only compiler.

    Dev C++ is another IDE. It used either CygWin or MinGw and produced 32 bit executables but has been abandoned for most of a decade by it's developer. The compiler supplied with DevC++ is now superceded by at least 3 generations of development by the Gnu team.

    So basically you moved your friends from a programming anachronism to abandonware.
    You did a little better for yourself, but you're still stuck on 32 bit code in a 64 bit world.

    AND... be aware that using C++ compilers for C code can lead to some highly non-standard practices if one is not watchful of the coding standards used... One example is that a C++ compiler will complain if you do not typecast the return value of malloc() but in C this is considered bad practice since malloc() simply returns an address.

    As for DosBox... well, that's one of those stupid things we wish we could dis-invent. Who in their right mind wants to run DOS in this day and age?

  15. #15
    Registered User
    Join Date
    Sep 2011
    Posts
    5
    guys... vl i would really appreciate if u could help me understand what int *p=(int *)100 mean. Discussion about the platforms were very much helpful .Thnakxx a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advantages of c++ type casting over c type casting
    By kaibalya2008 in forum C++ Programming
    Replies: 10
    Last Post: 05-05-2009, 11:09 AM
  2. type casting in C++
    By WDT in forum Networking/Device Communication
    Replies: 9
    Last Post: 10-15-2007, 10:18 AM
  3. difference between type conversion and type casting
    By Bargi in forum C Programming
    Replies: 1
    Last Post: 01-23-2007, 03:17 AM
  4. Type Casting
    By joshdick in forum C++ Programming
    Replies: 6
    Last Post: 09-06-2003, 08:30 PM
  5. type casting
    By henry in forum C++ Programming
    Replies: 1
    Last Post: 04-02-2003, 07:39 AM