473,403 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

returning char

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?
Mar 2 '06 #1
4 2037
"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?

Mar 2 '06 #2

"sk" <sk******@purdue.edu> 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; .
Mar 2 '06 #3
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/>

Mar 2 '06 #4
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.
Mar 2 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: cppaddict | last post by:
Hi, Is it considered bad form to have the subscript operator return a const reference variable? If not, what is the proper way to do it? My question was prompted by the code below, my...
10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
7
by: wonderboy | last post by:
Hey guys, I have a simple question. Suppose we have the following functions:- //-----My code starts here char* f1(char* s) { char* temp="Hi"; return temp;
4
by: jt | last post by:
I'm getting a compiler error: warning C4172: returning address of local variable or temporary Here is the function that I have giving this error: I'm returning a temporary char string and its...
5
by: shyam | last post by:
Hi All I have to write a function which basically takes in a string and returns an unknown number( at compile time) of strings i hav the following syntax in mind char *tokenize(char *) ...
29
by: Gregc. | last post by:
G'day I was wondering if someone can explain the concept of 'returning a Char from a Double' . For example, I have the following code: char getGrade(double mark) { if (mark>= 85) return...
2
by: Beorne | last post by:
I have to call a c++ library funtion returning a string with the following signature: char *get_identifier(); Usually when I have to marshal a function with a char* output parameter I do: ...
1
by: krishna81m | last post by:
In the following code, I am trying to return a char, a char* (a type of non-const without using new, what do we call this type of pointer?) and char* created using new operator. What I do not know at...
7
by: TBass | last post by:
Hi. I wrote a "tag" class to store values. The user gets to store most any type he would need. Instead of getting too complicatated, I decided I would store the value as a stringstream, then...
8
by: darren | last post by:
Hi everybody, have a quick look at this code: ===== ===== int main(void) { string msg; makeString(msg); cout << "back in main, result = " << msg << endl;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.