Thread: Doubt in structure

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    53

    Doubt in structure

    Consider the below program

    Code:
    struct str
    {
          char *p;
          int i;
    }t;
    
    main()
    {
        scanf("%s %d",t.p,&t.i);
        printf("%s",t.p);
    }
    the output is : Runtimer Error

    The reason is given that as we are trying to store the string at the address to which str(of t) points to. Since the pointer is not initialized it holds garbage value and so the reslult will be undefined. Hence the error.


    Then what about this

    Code:
    void main()
    {
             char *p;
             scanf("%s",p);
             printf("%s",p);
    }
    Here, though the pointer variable p is not initialized, it is accepting the input from the user and printing it without showing any error..


    May I know the reason in detail??
    Last edited by karthik537; 01-20-2009 at 08:36 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt regarding C structure in linux
    By Bargi in forum C Programming
    Replies: 2
    Last Post: 01-23-2007, 06:18 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. structure doubt...
    By incognito54 in forum C Programming
    Replies: 6
    Last Post: 04-21-2004, 11:54 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM