returning char

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sk

    #1

    returning char

    I'm trying to write a little function that acts very similar to scanf,
    but I suck at pointers and returning chars.

    My code:

    char *getline(){
    char *string;
    char c;
    int i=0;
    while((c=getcha r())!='\n'){
    string[i++]=c;
    }
    return string;
    }

    It runs after compilation, but when I use the function, I get a
    segmentation fault. Can someone help?
  • osmium

    #2
    Re: returning char

    "sk" writes:
    [color=blue]
    > I'm trying to write a little function that acts very similar to scanf, but
    > I suck at pointers and returning chars.
    >
    > My code:
    >
    > char *getline(){
    > char *string;[/color]

    string is all hat and no cattle. It is a pointer but it doesn't have
    anything to point at. Use malloc to get some space. Since it is in the
    nature or a pointer to point *somewhere* you get a fault when you try to
    reference that place - wherever it is.
    [color=blue]
    > char c;
    > int i=0;
    > while((c=getcha r())!='\n'){
    > string[i++]=c;
    > }
    > return string;
    > }
    >
    > It runs after compilation, but when I use the function, I get a
    > segmentation fault. Can someone help?[/color]


    Comment

    • Frank Schmidt

      #3
      Re: returning char


      "sk" <skazahay@purdu e.edu> schrieb im Newsbeitrag
      news:WArNf.79$o L.59@attbi_s71. ..[color=blue]
      > I'm trying to write a little function that acts very similar to scanf, but
      > I suck at pointers and returning chars.
      >
      > My code:
      >
      > char *getline(){
      > char *string;
      > char c;
      > int i=0;
      > while((c=getcha r())!='\n'){
      > string[i++]=c;
      > }
      > return string;
      > }
      >
      > It runs after compilation, but when I use the function, I get a
      > segmentation fault. Can someone help?[/color]

      What you call string is just a pointer that can point to a string - but
      actual it points to "nowhere" and there is no memory allocated for the
      string. To avoid the trouble with allocating memory inside the function and
      freeing it later... it would be much simpler to allocate the memory outside
      of getline, e.g.

      char *getline(char* string) {
      ...
      return string;
      }

      int main() {
      char input[256];
      getline(input);
      return 0;
      }


      To avoid that getline still can read more characters then the buffer can
      hold you should add also a variable that tells the maximum length of the
      string and check it against your i in the while loop.

      And at the end of getline you need to terminate your string with a 0, e.g.
      string[i] = 0; .


      Comment

      • CBFalconer

        #4
        Re: returning char

        sk wrote:[color=blue]
        >
        > I'm trying to write a little function that acts very similar to
        > scanf, but I suck at pointers and returning chars.
        >
        > My code:
        >
        > char *getline(){
        > char *string;
        > char c;
        > int i=0;
        > while((c=getcha r())!='\n'){
        > string[i++]=c;
        > }
        > return string;
        > }
        >
        > It runs after compilation, but when I use the function, I get a
        > segmentation fault. Can someone help?[/color]

        Of course it does, it is putting chars where the sun don't shine.
        And it can't return information about EOF, file system errors,
        etc. To see a way to implement a suitable function, see:

        <http://cbfalconer.home .att.net/download/ggets.zip>

        --
        "If you want to post a followup via groups.google.c om, don't use
        the broken "Reply" link at the bottom of the article. Click on
        "show options" at the top of the article, then click on the
        "Reply" at the bottom of the article headers." - Keith Thompson
        More details at: <http://cfaj.freeshell. org/google/>
        Also see <http://www.safalra.com/special/googlegroupsrep ly/>

        Comment

        • Ian Collins

          #5
          Re: returning char

          sk wrote:[color=blue]
          > I'm trying to write a little function that acts very similar to scanf,
          > but I suck at pointers and returning chars.
          >
          > My code:
          >
          > char *getline(){
          > char *string;
          > char c;
          > int i=0;
          > while((c=getcha r())!='\n'){
          > string[i++]=c;
          > }
          > return string;
          > }
          >
          > It runs after compilation, but when I use the function, I get a
          > segmentation fault. Can someone help?[/color]

          Hint - what does string point to?

          --
          Ian Collins.

          Comment

          Working...