Thread: Why the programs terminate at printf statements ?

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    17

    Why the programs terminate at printf statements ?

    Hello all,

    I am confused why my program terminate at the second print statement.

    Code:
    int main() 
    {
     int  const *p=6;
    int i = 0;
    printf("\np:%d",p);
    
    printf("\np:%d",(*p));
    			
        getch();
    }
    and also why the first printf output will be 6.I think 6 will be output for the 2nd printf.

    Thanks,
    Gunjan

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    p has a value 6 (when printed as an int). The value at location 6 is undefined, so one possibility is that the program is allowed to crash when accessing *p.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You need another compiler, if that first assignment happened without any warnings or errors.

    Or you need to pay more attention to compiler warnings before complaining when bad code crashes.
    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.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    thnks both for your inputs,

    but int const*p;
    means its an constant pointer pointed to an int
    so p going to point integer i.e., on doing printf *p will suppose to print 6 not the p.

    Am i right ?

    or i missintercept the means of int const *p.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You read them right to left

    int const *p and const int *p
    p is a pointer to a const int.
    You can change p, but not *p

    int * const p;
    p is a const pointer to an int
    You can change *p, but not p (you must therefore initialise it, otherwise it is useless).

    Now do you know what
    const int * const p;
    is?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with battleship program
    By howardqd in forum C++ Programming
    Replies: 12
    Last Post: 12-15-2010, 08:40 PM
  2. please help me finish my Hangman program (C)
    By eddybro in forum C Programming
    Replies: 1
    Last Post: 11-30-2010, 08:52 AM
  3. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  4. saying hello and 1st question
    By darksys in forum C Programming
    Replies: 12
    Last Post: 10-31-2008, 02:58 PM
  5. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 06:54 AM