input a string in gcc

Y

yogicoder

following is the code to accept a string from user, but i get
segmentation fault as soon i have finished entering the string i.e. as
soon as i press the 'enter' key.

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main (void)
{
	char str[50];
	printf ("Enter string:\n");
	fgets (str, sizeof str, stdin);
        //further code
}

if anybody can help me enter the string, thanx!
 
W

Walter Roberson

following is the code to accept a string from user, but i get
segmentation fault as soon i have finished entering the string i.e. as
soon as i press the 'enter' key.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main (void)
{
char str[50];
printf ("Enter string:\n");
fgets (str, sizeof str, stdin);

That part looks okay to me.
//further code
}

You are not returning a value from main(), which leads to undefined
behaviour in C89/C90.

But I suspect the error is in the "//further code" part.
 
H

Harald van Dijk

following is the code to accept a string from user, but i get
segmentation fault as soon i have finished entering the string i.e. as
soon as i press the 'enter' key.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main (void)
{
char str[50];
printf ("Enter string:\n");
fgets (str, sizeof str, stdin);

That part looks okay to me.
//further code
}

You are not returning a value from main(),

If you treat //further code as just a comment, then you already know this
is not a C90 program. If you treat it as an indicator that there is more
code, that code may very well return a value from main().
which leads to undefined
behaviour in C89/C90.

No, it doesn't. It leads to an undefined return value, but not undefined
behaviour, in C90.
But I suspect the error is in the "//further code" part.

Agreed.
 
F

Filipe Cabecinhas

yogicoder said:
following is the code to accept a string from user, but i get
segmentation fault as soon i have finished entering the string i.e. as
soon as i press the 'enter' key.

The error must be in the // further code part, as others have pointed
out. Compile your code with the -g flag and use valgrind or gdb to
see the offending line. After that try to fix it and, if you can't
(but TRY first) ask for help again.


--

- Filipe Cabecinhas

char* params[] = {"fil", "cab", "@", "gmail", ".", "com"};
puts("Real signature:");
for (int i = 0; i < sizeof(params)/sizeof(*params); ++i)
printf("%s", params);
putchar('
');
 
R

Richard Tobin

following is the code to accept a string from user, but i get
segmentation fault as soon i have finished entering the string i.e. as
soon as i press the 'enter' key.

Does this very program get a segmentation fault, or only when you
put in the "further code"?

-- Richard
 
B

Bartc

yogicoder said:
following is the code to accept a string from user, but i get
segmentation fault as soon i have finished entering the string i.e. as
soon as i press the 'enter' key.

How soon exactly? Enough time for your computer to execute a hundred million
lines of code following the fgets perhaps?

If you put in a printf or puts statement just before your '//further code'
you can establish whether the problem is with the fgets, or with the
'further code'.
 
R

Richard

yogicoder said:
following is the code to accept a string from user, but i get
segmentation fault as soon i have finished entering the string i.e. as
soon as i press the 'enter' key.

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main (void)
{
	char str[50];
	printf ("Enter string:\n");
	fgets (str, sizeof str, stdin);
//further code
}

if anybody can help me enter the string, thanx!

use a debugger and find where it happens.
 
Y

yogicoder

How soon exactly? Enough time for your computer to execute a hundred million
lines of code following the fgets perhaps?

If you put in a printf or puts statement just before your '//further code'
you can establish whether the problem is with the fgets, or with the
'further code'.

Well i didn't consider further code important because i had already
used a printf("test") statement immediately after fgets & it's not
getting executed. I get seg fault eror the instant i hit the enter
key.
 
B

Bartc

yogicoder said:
Well i didn't consider further code important because i had already
used a printf("test") statement immediately after fgets & it's not
getting executed. I get seg fault eror the instant i hit the enter
key.

Thanks, that's useful clarification. So in this program (I've added \n to
the printf):

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main (void)
{
char str[50];
printf ("Enter string:\n");
fgets (str, sizeof str, stdin);
printf("test\n");
}

after you hit Enter (for test purposes, don't type anything else), it does
not print "test"? I'm asking because on my machine (and I suspect many
others) it works as expected.

What happens if you comment out the fgets line?
 
R

Richard

Bartc said:
yogicoder said:
Well i didn't consider further code important because i had already
used a printf("test") statement immediately after fgets & it's not
getting executed. I get seg fault eror the instant i hit the enter
key.

Thanks, that's useful clarification. So in this program (I've added \n to
the printf):

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main (void)
{
char str[50];
printf ("Enter string:\n");
fgets (str, sizeof str, stdin);
printf("test\n");
}

after you hit Enter (for test purposes, don't type anything else), it does
not print "test"? I'm asking because on my machine (and I suspect many
others) it works as expected.

What happens if you comment out the fgets line?

use a debugger. get a backtrace.
 
F

Filipe Cabecinhas

yogicoder said:
Well i didn't consider further code important because i had already
used a printf("test") statement immediately after fgets & it's not
getting executed. I get seg fault eror the instant i hit the enter
key.

Install valgrind, run your program with: valgrind ./prog
then show us the errors.

--

- Filipe Cabecinhas

char* params[] = {"fil", "cab", "@", "gmail", ".", "com"};
puts("Real signature:");
for (int i = 0; i < sizeof(params)/sizeof(*params); ++i)
printf("%s", params);
putchar('\n');
 
R

Richard Tobin

yogicoder said:
Well i didn't consider further code important because i had already
used a printf("test") statement immediately after fgets & it's not
getting executed. I get seg fault eror the instant i hit the enter
key.

When you're asking other people to help fix your code, it's not a good
idea to rely on what *you* think is important. After all, if you knew
what was important you'd be able to find the bug yourself.

How do you know the printf("test") statement is not being executed?
Do you really mean that "test" isn't being printed? Normally terminal
output is line-buffered, so it won't get printed until you output
a newline.

If you want help, send a complete program that fails, not a cut-down
program that you haven't tested.

-- Richard
 
Y

yogicoder

Richard said:
Anytime somebody introduces a debugging problem
by saying where the problem isn't,
that's where I look first.

well yes people you all were right. There is some error in the code
after fgets. What was happening was that the printf immediately after
fgets was also not getting executed because of some error further away
in my code. This was my first post so please don't castigate me for
this.
So here's a detailed explanation:
Basically what my code is supposed to do is accept a string from user
& reverse the position of the words, for e.g.
Input: this is a string
Output: string a is this
I have to do this in O(n) complexity.
Here's my code :
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main (void)
{

	void change (char []);
	char str[50];
	printf ("Enter string:\n");
	fgets (str, sizeof str, stdin);
	change (str);
	printf ("\nRequired string is:\n");
	fputs (str, stdout);
	return 0;
}

void change (char str[50])
{
	char *arr[20], *a;
	int i, j, k;
	i = j = 0;
	while (str[j] != '\n') {
		*arr[i] = str[j];
		if (str[j] == ' ')
			++i;
		str[++j];
		arr[i]++;
	}
	*arr[i] = '\0';
	k = j = i;
	i = 0;
	while (i < j) {
		a = arr[i];
		arr[i] = arr[j];
		arr[j] = a;
		++i;
		--j;
	}
	i = j = 0;
	while (*arr[i] != '\0') {
		str[j] = *arr[i];
		if (*arr[i] == ' ')
			++i;
		str[++j];
		arr[i]++;
	}
	str[j] = *arr[i];
	return;
}

There's some error with pointers i know, but i am not able to
understand it. You guys have really been helpful so thanks a lot! I've
spent a lot of time on this so help would be greatly appreciated.
 
R

Richard

yogicoder said:
Richard said:
Anytime somebody introduces a debugging problem
by saying where the problem isn't,
that's where I look first.

well yes people you all were right. There is some error in the code
after fgets. What was happening was that the printf immediately after
fgets was also not getting executed because of some error further away
in my code. This was my first post so please don't castigate me for
this.
So here's a detailed explanation:
Basically what my code is supposed to do is accept a string from user
& reverse the position of the words, for e.g.
Input: this is a string
Output: string a is this
I have to do this in O(n) complexity.
Here's my code :
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main (void)
{

	void change (char []);
	char str[50];
	printf ("Enter string:\n");
	fgets (str, sizeof str, stdin);
	change (str);
	printf ("\nRequired string is:\n");
	fputs (str, stdout);
	return 0;
}

void change (char str[50])
{
	char *arr[20], *a;
	int i, j, k;
	i = j = 0;
	while (str[j] != '\n') {
		*arr[i] = str[j];
		if (str[j] == ' ')
			++i;
		str[++j];
		arr[i]++;
	}
	*arr[i] = '\0';
	k = j = i;
	i = 0;
	while (i < j) {
		a = arr[i];
		arr[i] = arr[j];
		arr[j] = a;
		++i;
		--j;
	}
	i = j = 0;
	while (*arr[i] != '\0') {
		str[j] = *arr[i];
		if (*arr[i] == ' ')
			++i;
		str[++j];
		arr[i]++;
	}
	str[j] = *arr[i];
	return;
}

There's some error with pointers i know, but i am not able to
understand it. You guys have really been helpful so thanks a lot! I've
spent a lot of time on this so help would be greatly appreciated.[/QUOTE]

Run your code in gdb or similar. It will then show you the line of the
error. It really is that simple and you will learn a lot by stepping
through the code and examining your variables.
 
F

Flash Gordon

yogicoder wrote, On 08/06/08 15:46:

well yes people you all were right. There is some error in the code
after fgets. What was happening was that the printf immediately after
fgets was also not getting executed because of some error further away
in my code. This was my first post so please don't castigate me for
this.
So here's a detailed explanation:
Basically what my code is supposed to do is accept a string from user
& reverse the position of the words, for e.g.
Input: this is a string
Output: string a is this
I have to do this in O(n) complexity.
Here's my code :
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main (void)
{

	void change (char []);
	char str[50];
	printf ("Enter string:\n");
	fgets (str, sizeof str, stdin);[/QUOTE]

fgets returns a value for a reason. You should check it.
[QUOTE]
change (str);
	printf ("\nRequired string is:\n");
	fputs (str, stdout);
	return 0;
}

void change (char str[50])[/QUOTE]

You should be aware that this does not mean the function takes an array. 
It is the same as
void change(char *str)
[QUOTE]
{
	char *arr[20], *a;[/QUOTE]

Where are your checks that you don't write beyond element arr[19]?
Sensible names would also help.
[QUOTE]
int i, j, k;
	i = j = 0;
	while (str[j] != '\n') {[/QUOTE]

What if there is no new line in the string? There are situations where 
you won't have one.
[QUOTE]
*arr[i] = str[j];[/QUOTE]

Urm, where does arr[i] point to? Hint, you have not assigned anything to 
it yet.
[QUOTE]
if (str[j] == ' ')
			++i;
		str[++j];[/QUOTE]

What is the above meant to achieve? All it will actually achieve is 
incrementing j.

[QUOTE]
There's some error with pointers i know, but i am not able to
understand it. You guys have really been helpful so thanks a lot! I've
spent a lot of time on this so help would be greatly appreciated.[/QUOTE]

Well, I've given you a few hints, see if you can do any better now.
 
J

Jens Thoms Toerring

yogicoder said:
well yes people you all were right. There is some error in the code
after fgets. What was happening was that the printf immediately after
fgets was also not getting executed because of some error further away
in my code.

No, you told us that you put in there

printf( "Test" );

and, if you have a closer look you will see that there's no
'\n' at the end of the string you print. But per default the
stdout stream is line buffered, i.e. when there's no '\n'
it doesn't get passed on to the console but stays in the
internal buffer of printf() until a '\n' is found later (or
until that buffer gets full). To remedy the situation either
do the obvious, i.e. add a '\n' or call 'fflush( stdout );'
to force the buffer to get flushed (or print to stderr which
per default isn't buffered).
So here's a detailed explanation:
Basically what my code is supposed to do is accept a string from user
& reverse the position of the words, for e.g.
Input: this is a string
Output: string a is this
I have to do this in O(n) complexity.
Here's my code :
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>[/QUOTE]
[QUOTE]
int main (void)
{
void change (char []);
char str[50];
printf ("Enter string:\n");
fgets (str, sizeof str, stdin);
change (str);
printf ("\nRequired string is:\n");
fputs (str, stdout);
return 0;
}[/QUOTE]
[QUOTE]
void change (char str[50])
{
char *arr[20], *a;
int i, j, k;
i = j = 0;
while (str[j] != '\n') {
*arr[i] = str[j];[/QUOTE]

Here you got a serious problem. arr is an array of 20 pointers
to char. And all the elements of this array aren't initialized,
i.e. they point to ramdom places in memory. And here you now
try to write a char into such a location, which is a perfect
recipe for getting a segmentation fault.

Think of pointers like of checks - if they aren't backed by
money in the bank (point to memory you own) you're in a lot
of trouble.

Perhaps you meant to write

             arr[ i ] = str + j;

That would look more reasonable since that would set the
i-th element of arr to point to the j-th char in str.
[QUOTE]
if (str[j] == ' ')
++i;
str[++j];[/QUOTE]

That's a rather strange way of writing '++j;' since all
this is doing is incrementing 'j'.
[QUOTE]
arr[i]++;
}[/QUOTE]

I guess the whole purpose of the previous part is to built up
list of pointers to the starts of words in the string. Is
that about correct?
[QUOTE]
*arr[i] = '\0';
k = j = i;
i = 0;
while (i < j) {
a = arr[i];
arr[i] = arr[j];
arr[j] = a;
++i;
--j;
}[/QUOTE]

This looks as if you want to reverse the arr array.
[QUOTE]
i = j = 0;
while (*arr[i] != '\0') {
str[j] = *arr[i];[/QUOTE]

If my above assumptions are correct then you again get into
trouble here (even if all the bugs from above would be corrected).
If arr is an array of pointers, pointing into str, then changing
str from the pointers in arr will mess up str completely. You
can't split up a string like that into pieces and rearrange the
pieces. Just take a piece of paper, write down the string and
indicate by arrows where the elements of arr point to. Then
try to use your :algorithm" by hand, exchanging the letters as
you try to do here, and you will see why it can't work - as long
as you remember that the elements of arr just point into the
string and aren't copies of the words of the string. Just try
it with a very simple sentence like "I am here".

Don't try to use a debugger here. It's absolutely useless
as long as you don't fully understand what is going one
here and will only get you more confused.

                        Regards, Jens
 
B

Ben Bacarisse

yogicoder said:
Basically what my code is supposed to do is accept a string from user
& reverse the position of the words, for e.g.
Input: this is a string
Output: string a is this
I have to do this in O(n) complexity.

Nice problem.
Here's my code :
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main (void)
{

	void change (char []);[/QUOTE]

change is not a very good name.  By the way, some people don't like
having function prototypes anywhere but at file scope.  I don't mind.
Check what style you instructor expects.
[QUOTE]
char str[50];
	printf ("Enter string:\n");
	fgets (str, sizeof str, stdin);
	change (str);
	printf ("\nRequired string is:\n");
	fputs (str, stdout);
	return 0;
}

void change (char str[50])[/QUOTE]

Your know the 50 has no effect, right?
[QUOTE]
{
	char *arr[20], *a;[/QUOTE]

All of the 20 pointers in arr are left uninitialised.  They don't
point anywhere valid.
[QUOTE]
int i, j, k;
	i = j = 0;
	while (str[j] != '\n') {
		*arr[i] = str[j];[/QUOTE]

and here you use of them.  Immediate "boom".  I think you probably
intended something quite different, but I can't tell.  You can put a
pointer into one quote safely, e.g. arr[i] = str, but you can
de-reference arr[i] with a * until it points somewhere.
[QUOTE]
if (str[j] == ' ')
			++i;
		str[++j];
		arr[i]++;
	}
	*arr[i] = '\0';
	k = j = i;
	i = 0;
	while (i < j) {
		a = arr[i];
		arr[i] = arr[j];
		arr[j] = a;
		++i;
		--j;[/QUOTE]

Hmmm...  You need to do some more planning first.  Here you are
swapping pointers around.  I don't think that will help much.

If your plan is to point to the start of each word with arr[x], you
don't need to reverse the sequence of these in the array.  Just access
it from the end rather than the start.
[QUOTE]
}
	i = j = 0;
	while (*arr[i] != '\0') {
		str[j] = *arr[i];
		if (*arr[i] == ' ')
			++i;
		str[++j];
		arr[i]++;
	}
	str[j] = *arr[i];
	return;
}[/QUOTE]

General points: It would be better if the function did not assume
there was a newline at the end.  I would go for a solution that did
not have another fixed size in it (the 20 elements of arr).  The test
for spaces is probably better done with isspace (from ctype.h) since
that includes other word-separators.
 
W

Willem

yogicoder wrote:
) Well i didn't consider further code important because i had already
) used a printf("test") statement immediately after fgets & it's not
) getting executed. I get seg fault eror the instant i hit the enter
) key.

Try putting this instead:
fprintf(STDERR, "test\n");

(Or at least flush stdout after printing.)


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
B

Bartc

well yes people you all were right. There is some error in the code
after fgets. What was happening was that the printf immediately after
fgets was also not getting executed because of some error further away
in my code. This was my first post so please don't castigate me for
this.

You didn't mention the call to change() between fgets() and printf()...
So here's a detailed explanation:
Basically what my code is supposed to do is accept a string from user
& reverse the position of the words, for e.g.


There are some problems with this code as others have said. Among other
things, limiting to only 20 words (a 50 character string could have up to 24
or so).

I put a version below that may or may not be helpful. There is no array of
pointers, but it does have to temporarily create a string the same size as
the input. But it has no other limits on string length or number of words.

Your input string has a \n at the end; where the string is unchanged (0 or 1
words) this \n stays.

The malloc allocation is a tight fit; an extra couple of bytes here won't
hurt.


/* reverse words in str in-place. separaters are spaces and tabs */
void change (char *str)
{
char *newstr;
char c, *p,*q, *r, *s;
int i,j,k,n,nwords;

if (str==NULL) return;
n=strlen(str);
if (n<=2) return; /* More than 2 words not possible, esp as last
char might be \n */

newstr=malloc(n+1); /* Build new string elsewhere */
if (newstr==NULL) return; /* Memory problem */

q=newstr; /* Point to start of dest string */
r=NULL; /* Point to last char of current word */
nwords=0;

for (p=str+n-1; p>=str; --p) {/* Scan source string starting from the end */
c=*p;
if (c==' ' || c=='\t' || c=='\n') {
if (r) { /* start of word encountered */
s=p+1;
for (i=0; i<(r-p); ++i) *q++ = *s++;
*q++=' ';
*q=0;
r=NULL;
++nwords;
}
}
else /* Assume c is part of word */
if (r==NULL) r=p; /* Remember last char of this word */
}

if (nwords) {
if (r) {
s=str;
for (i=0; i<(r-str+1); ++i) *q++ = *s++;
*q=0;
}
strcpy(str,newstr);
};

free(newstr);
return;
}
 
N

Nick Keighley

yogicoder wrote:
) Well i didn't consider further code important because i had already
) used a printf("test") statement immediately after fgets & it's not
) getting executed. I get seg fault eror the instant i hit the enter
) key.

Try putting this instead:
 fprintf(STDERR, "test\n");

fprintf (stderr, "test\n");
 

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,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top