returning char

S

sk

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?
 
O

osmium

sk said:
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?
 
F

Frank Schmidt

sk said:
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; .
 
C

CBFalconer

sk said:
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/>
 
I

Ian Collins

sk said:
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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top