pointers

Discussion in 'C' started by *nick, Sep 1, 2010.

  1. *nick

    *nick New Member

    Joined:
    Sep 1, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    when i run this, i get an error, am i using pointers incorrectly?

    Code:
    void print_array(char *s)
    {
      s = (char *)malloc(6);
    
      s[0] = 'T';
      s[1] = '\0';
    
      printf("%s",s);
    
    }
    
     
    main()
    
    {
    
      char *word;
    
      print_array(word);
    
      printf("%s",word); // fails on this line
    
    }
     
  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
    obviously you are using the pointers incorrectly...you better know that the pointers are needed to be allocated dyanamically if you use it for arrar purpose...this thing you did in the function part but when it returns from the function it loses its scope. If you want to be error free try to use pointer of a pointer then only you can utilize the concept of pass by address. The array construction would remain intact even after returning from the function.
     

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