Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > returning char

Reply
Thread Tools

returning char

 
 
sk
Guest
Posts: n/a
 
      03-02-2006
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?
 
Reply With Quote
 
 
 
 
osmium
Guest
Posts: n/a
 
      03-02-2006
"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?



 
Reply With Quote
 
 
 
 
Frank Schmidt
Guest
Posts: n/a
 
      03-02-2006

"sk" <(E-Mail Removed)> 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[i] = 0; .


 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      03-02-2006
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/>

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      03-02-2006
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
char *fred; char * fred; char *fred; any difference? Ben Pfaff C Programming 5 01-17-2004 07:37 PM
The difference between char a[6] and char *p=new char[6] ? wwj C Programming 24 11-07-2003 05:27 PM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments