473,958 Members | 40,307 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Mar 2 '06 #2

"sk" <sk******@purdu e.edu> schrieb im Newsbeitrag
news:WArNf.79$o L.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=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?


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


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

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


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
2160
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 problematic attempt to implement a subscript operator that returns a const reference. The dubious code is marked at the end. <code>
10
3189
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 in levels of indirection, so I feel it must have something to do with the way I am representing the array in the call and the return. Below I have commented the problem parts. Thanks in advance for any help offered. Pete
7
2332
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
3171
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 not liking it. How can I fix this? char *dequeue(struct node **first) { char temp;
5
5495
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 *) is it ok?
29
2398
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 ('A');
2
4970
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: static extern int get_identifier2( StringBuilder Ack_Msg, int msg_len);
1
1733
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 all is how variables are created in what type of memory and how they are deleted in the following three cases when calling and exiting test, test1 and test2 functions. I also note that I see a different result while using char and char* which I...
7
3332
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 overload the SetValue method. .... protected:
8
2231
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
10066
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11697
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9986
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8363
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7521
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6439
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
5049
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4630
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3650
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.