What this exactly means?

This is a discussion on What this exactly means? within the C Programming forums, part of the General Programming Boards category; Hello, I'm new here I have a question about this statement: *(int *)0 = 0; Could anybody explain what this ...

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    13

    What this exactly means?

    Hello, I'm new here

    I have a question about this statement:

    *(int *)0 = 0;

    Could anybody explain what this snippets exactly does? And why it compiles but gives a segfault when executing?

    Thanks a lot in advance!

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    it is equivalent to the following code
    Code:
    int * pTemp = NULL;
    *pTemp = 0;
    Could you see why it segfaults?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    13
    Becouse you are trying so set the value 0 in an unknown memory address?

  4. #4
    Registered User
    Join Date
    Sep 2013
    Posts
    13
    But I've never seen that snippet before so i thought it was malformed too..but C compiler get it right :/ I thought it wouldn't either compile, why that statement is "syntatically" correct by the way?

    EDIT:

    Oh...0 is seen as NULL by C compilers?! (i'm using gcc)

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    0 is a null pointer constant.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Sep 2013
    Posts
    13
    Quote Originally Posted by laserlight View Post
    0 is a null pointer constant.
    so 0 is equal to:

    int *ptr = NULL

    ?

    and if I do:

    int *ptr = (int *)0;

    is perfectly correct?
    Last edited by riemann; 09-08-2013 at 11:37 AM.

  7. #7
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    1,085
    It is correct but it means the address stored is the NULL address. If you want to actually set the value then you allocate and dreference. But yes, what you wrote is logically equivalent.

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Next time post your code in [code]/*your program*/[/code].
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  9. #9
    Registered User
    Join Date
    Sep 2013
    Posts
    13
    Quote Originally Posted by std10093 View Post
    Next time post your code in [code]/*your program*/[/code].
    Sure, sorry for that!

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Quote Originally Posted by riemann View Post
    Sure, sorry for that!
    No problem, since you are new.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what does it means?
    By enakta13 in forum C Programming
    Replies: 1
    Last Post: 09-27-2012, 12:55 PM
  2. what does ROL means
    By bubblegum in forum C Programming
    Replies: 0
    Last Post: 03-08-2008, 08:20 AM
  3. What does the following means?
    By EeeK in forum C Programming
    Replies: 2
    Last Post: 10-27-2003, 09:15 PM
  4. Anyone know what this means?
    By bigblack in forum C++ Programming
    Replies: 1
    Last Post: 04-15-2002, 07:19 PM
  5. Does anyone know what this means
    By killerasp in forum C++ Programming
    Replies: 3
    Last Post: 02-08-2002, 12:41 AM

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