returning char

Discussion in 'C Programming' started by sk, Mar 2, 2006.

  1. sk

    sk Guest

    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=getchar())!='\n'){
    string[i++]=c;
    }
    return string;
    }

    It runs after compilation, but when I use the function, I get a
    segmentation fault. Can someone help?
     
    sk, Mar 2, 2006
    #1
    1. Advertising

  2. sk

    osmium Guest

    "sk" writes:

    > 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;


    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.

    > char c;
    > int i=0;
    > while((c=getchar())!='\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, Mar 2, 2006
    #2
    1. Advertising

  3. "sk" <> schrieb im Newsbeitrag
    news:WArNf.79$oL.59@attbi_s71...
    > 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=getchar())!='\n'){
    > string[i++]=c;
    > }
    > return string;
    > }
    >
    > It runs after compilation, but when I use the function, I get a
    > segmentation fault. Can someone help?


    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 = 0; .
     
    Frank Schmidt, Mar 2, 2006
    #3
  4. sk

    CBFalconer Guest

    sk wrote:
    >
    > 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=getchar())!='\n'){
    > string[i++]=c;
    > }
    > return string;
    > }
    >
    > It runs after compilation, but when I use the function, I get a
    > segmentation fault. Can someone help?


    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.com, 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/googlegroupsreply/>
     
    CBFalconer, Mar 2, 2006
    #4
  5. sk

    Ian Collins Guest

    sk wrote:
    > 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=getchar())!='\n'){
    > string[i++]=c;
    > }
    > return string;
    > }
    >
    > It runs after compilation, but when I use the function, I get a
    > segmentation fault. Can someone help?


    Hint - what does string point to?

    --
    Ian Collins.
     
    Ian Collins, Mar 2, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    597
  2. wwj
    Replies:
    24
    Views:
    2,573
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    505
    Tristan Miller
    Jan 17, 2004
  4. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    625
    Jack Klein
    May 9, 2005
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,152
    Ian Collins
    May 9, 2006
Loading...

Share This Page