Why the programs terminate at printf statements ?

This is a discussion on Why the programs terminate at printf statements ? within the C Programming forums, part of the General Programming Boards category; Hello all, I am confused why my program terminate at the second print statement. Code: int main() { int const ...

  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,454
    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%.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    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.
    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. help with battleship program
    By howardqd in forum C++ Programming
    Replies: 12
    Last Post: 12-15-2010, 07:40 PM
  2. please help me finish my Hangman program (C)
    By eddybro in forum C Programming
    Replies: 1
    Last Post: 11-30-2010, 07:52 AM
  3. Replies: 2
    Last Post: 03-05-2009, 09: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, 05:54 AM

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