Pointers small confusion

Discussion in 'C' started by gaurav_13191, Aug 28, 2010.

  1. gaurav_13191

    gaurav_13191 New Member

    Joined:
    Aug 28, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi coders,I have a small problem regarding pointers, I am a bit weak in pointers and need to understand why the following is wrong:

    int main(void)
    {
    char *a;
    gets(a);
    return 0;
    }
    This causes an exception, please explain the error. I am using Turbo C as compiler, suggest a good and latest compiler too! :)
     
  2. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    http://www.techgeek.in
    Before doing gets on any character pointer u must first allocated memory dyanamically...like this:-

    a=(char*)malloc(10*sizeof(char));

    Now u will not get any error...
    cheers!!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice