Re: A interesting problem of C programming

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 





On Wed, Jun 18, 2008 at 6:54 PM, vaibhav khatavkar <khatavkarvaibhav86@xxxxxxxxx> wrote:
Hi Wang Yu ,
 
             I saw ur code ...
                  
                                                                                                            
    char *p;                                                                                                                              
    p = 0;                                                                                                                                
    printf("%s", p);                                                                                                                      
    printf("\n");   
                                                                                                                      
In ur first code snippet  "%s" prints string located at p .. and thats null .. so we get output null ... (Note that : there are 2 printf statements)
             

The out put is (null)

Now when u changed it like this ... 
But, if I change into:

#include <stdio.h>                                                                                                                                                                                                                  
int main()                                                                                                                                
{                                                                                                                                         
                                                                                                                                          
    char *p;                                                                                                                              
    p = 0;                                                                                                                                
    printf("%s\n", p);                                                                                                                                                                                                                                         
    return 0;                                                                                                                             
}

The output will be Segment fault!

The segmentation fault comes just because in printf statement u have written "%s\n" .. compiler is unable to resolve it ... when u add a space between %s and \n the output is null and not segmentation fault ..

     But why the complier is unable to resolve it when p is null? and it works when add a space after it?

I don't know why....
Thanks!
--
National Research Center for Intelligent Computing Systems
Institute of Computing Technology, Chinese Academy of Sciences



--
with regards,
vaibhav



--
National Research Center for Intelligent Computing Systems
Institute of Computing Technology, Chinese Academy of Sciences

[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux