input a string in gcc

Discussion in 'C Programming' started by yogicoder, Jun 8, 2008.

  1. yogicoder

    yogicoder Guest

    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!
     
    yogicoder, Jun 8, 2008
    #1
    1. Advertising

  2. In article <>,
    yogicoder <> wrote:
    >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.
    --
    "Allegories are in the realm of thoughts, what ruins are in
    the realm of things." -- Walter Benjamin
     
    Walter Roberson, Jun 8, 2008
    #2
    1. Advertising

  3. On Sun, 08 Jun 2008 07:38:48 +0000, Walter Roberson wrote:
    > In article
    > <>,
    > yogicoder <> wrote:
    >>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.
     
    Harald van Dijk, Jun 8, 2008
    #3
  4. yogicoder <> writes:

    > 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('
    ');
     
    Filipe Cabecinhas, Jun 8, 2008
    #4
  5. In article <>,
    yogicoder <> wrote:
    >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
    --
    In the selection of the two characters immediately succeeding the numeral 9,
    consideration shall be given to their replacement by the graphics 10 and 11 to
    facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)
     
    Richard Tobin, Jun 8, 2008
    #5
  6. yogicoder

    Bartc Guest

    "yogicoder" <> wrote in message
    news:...
    > 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'.

    --
    Bartc
     
    Bartc, Jun 8, 2008
    #6
  7. yogicoder

    Richard Guest

    yogicoder <> writes:

    > 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.
     
    Richard, Jun 8, 2008
    #7
  8. yogicoder

    yogicoder Guest

    On Jun 8, 4:10 pm, "Bartc" <> wrote:
    > "yogicoder" <> wrote in message
    >
    > news:...
    >
    > > 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'.
    >
    > --
    > Bartc


    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.
     
    yogicoder, Jun 8, 2008
    #8
  9. yogicoder

    Bartc Guest

    "yogicoder" <> wrote in message
    news:...
    > On Jun 8, 4:10 pm, "Bartc" <> wrote:
    >> "yogicoder" <> wrote in message
    >>
    >> news:...
    >>
    >> > 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'.
    >>
    >> --
    >> Bartc

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

    --
    Bartc
     
    Bartc, Jun 8, 2008
    #9
  10. yogicoder

    Richard Guest

    "Bartc" <> writes:

    > "yogicoder" <> wrote in message
    > news:...
    >> On Jun 8, 4:10 pm, "Bartc" <> wrote:
    >>> "yogicoder" <> wrote in message
    >>>
    >>> news:...
    >>>
    >>> > 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'.
    >>>
    >>> --
    >>> Bartc

    >>
    >> 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.
     
    Richard, Jun 8, 2008
    #10
  11. yogicoder <> writes:

    > 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');
     
    Filipe Cabecinhas, Jun 8, 2008
    #11
  12. In article <>,
    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.


    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
    --
    In the selection of the two characters immediately succeeding the numeral 9,
    consideration shall be given to their replacement by the graphics 10 and 11 to
    facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)
     
    Richard Tobin, Jun 8, 2008
    #12
  13. yogicoder

    yogicoder Guest

    On Jun 8, 7:17 pm, pete <> wrote:
    > Richard Tobin wrote:
    > > In article <>,
    > > 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.

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

    >
    > Anytime somebody introduces a debugging problem
    > by saying where the problem isn't,
    > that's where I look first.
    >
    > --
    > pete


    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.
     
    yogicoder, Jun 8, 2008
    #13
  14. yogicoder

    Richard Guest

    yogicoder <> writes:

    > On Jun 8, 7:17 pm, pete <> wrote:
    >> Richard Tobin wrote:
    >> > In article <>,
    >> > 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.

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

    >>
    >> Anytime somebody introduces a debugging problem
    >> by saying where the problem isn't,
    >> that's where I look first.
    >>
    >> --
    >> pete

    >
    > 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.[/color]
    
    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.
     
    Richard, Jun 8, 2008
    #14
  15. yogicoder

    Flash Gordon Guest

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

    <snip>

    > 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);[/color]
    
    fgets returns a value for a reason. You should check it.
    [color=blue]
    > 	change (str);
    > 	printf ("\nRequired string is:\n");
    > 	fputs (str, stdout);
    > 	return 0;
    > }
    > 
    > void change (char str[50])[/color]
    
    You should be aware that this does not mean the function takes an array. 
    It is the same as
    void change(char *str)
    [color=blue]
    > {
    > 	char *arr[20], *a;[/color]
    
    Where are your checks that you don't write beyond element arr[19]?
    Sensible names would also help.
    [color=blue]
    > 	int i, j, k;
    > 	i = j = 0;
    > 	while (str[j] != '\n') {[/color]
    
    What if there is no new line in the string? There are situations where 
    you won't have one.
    [color=blue]
    > 		*arr[i] = str[j];[/color]
    
    Urm, where does arr[i] point to? Hint, you have not assigned anything to 
    it yet.
    [color=blue]
    > 		if (str[j] == ' ')
    > 			++i;
    > 		str[++j];[/color]
    
    What is the above meant to achieve? All it will actually achieve is 
    incrementing j.
    
    <snip>
    [color=blue]
    > 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.[/color]
    
    Well, I've given you a few hints, see if you can do any better now.
    -- 
    Flash Gordon
     
    Flash Gordon, Jun 8, 2008
    #15
  16. yogicoder <> wrote:
    > On Jun 8, 7:17 pm, pete <> wrote:
    > 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>[/color]
    [color=blue]
    > 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;
    > }[/color]
    [color=blue]
    > void change (char str[50])
    > {
    >         char *arr[20], *a;
    >         int i, j, k;
    >         i = j = 0;
    >         while (str[j] != '\n') {
    >                 *arr[i] = str[j];[/color]
    
    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.
    [color=blue]
    >                 if (str[j] == ' ')
    >                         ++i;
    >                 str[++j];[/color]
    
    That's a rather strange way of writing '++j;' since all
    this is doing is incrementing 'j'.
    [color=blue]
    >                 arr[i]++;
    >         }[/color]
    
    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?
    [color=blue]
    >         *arr[i] = '\0';
    >         k = j = i;
    >         i = 0;
    >         while (i < j) {
    >                 a = arr[i];
    >                 arr[i] = arr[j];
    >                 arr[j] = a;
    >                 ++i;
    >                 --j;
    >         }[/color]
    
    This looks as if you want to reverse the arr array.
    [color=blue]
    >         i = j = 0;
    >         while (*arr[i] != '\0') {
    >                 str[j] = *arr[i];[/color]
    
    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
    -- 
      \   Jens Thoms Toerring  ___      
       \__________________________      http://toerring.de
     
    Jens Thoms Toerring, Jun 8, 2008
    #16
  17. yogicoder <> writes:
    <snip>
    > 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 []);[/color]
    
    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.
    [color=blue]
    > 	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])[/color]
    
    Your know the 50 has no effect, right?
    [color=blue]
    > {
    > 	char *arr[20], *a;[/color]
    
    All of the 20 pointers in arr are left uninitialised.  They don't
    point anywhere valid.
    [color=blue]
    > 	int i, j, k;
    > 	i = j = 0;
    > 	while (str[j] != '\n') {
    > 		*arr[i] = str[j];[/color]
    
    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.
    [color=blue]
    > 		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;[/color]
    
    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.
    [color=blue]
    > 	}
    > 	i = j = 0;
    > 	while (*arr[i] != '\0') {
    > 		str[j] = *arr[i];
    > 		if (*arr[i] == ' ')
    > 			++i;
    > 		str[++j];
    > 		arr[i]++;
    > 	}
    > 	str[j] = *arr[i];
    > 	return;
    > }[/color]
    
    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.
    
    -- 
    Ben.
     
    Ben Bacarisse, Jun 8, 2008
    #17
  18. yogicoder

    Willem Guest

    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
     
    Willem, Jun 8, 2008
    #18
  19. yogicoder

    Bartc Guest

    "yogicoder" <> wrote in message
    news:...

    > 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;
    }
     
    Bartc, Jun 8, 2008
    #19
  20. On 8 Jun, 17:39, Willem <> wrote:
    > 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");

    --
    Nick Keighley
     
    Nick Keighley, Jun 9, 2008
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    464
  2. Kevin P. Fleming

    C99 structure initialization in gcc-2.95.3 vs gcc-3.3.1

    Kevin P. Fleming, Nov 6, 2003, in forum: C Programming
    Replies:
    2
    Views:
    680
    Kevin P. Fleming
    Nov 6, 2003
  3. Replies:
    5
    Views:
    390
    Nathan Addy
    Sep 17, 2005
  4. ashnin

    GCC 3.4.3 and GCC 4.1.2

    ashnin, Jul 7, 2008, in forum: C++
    Replies:
    1
    Views:
    588
    Michael DOUBEZ
    Jul 7, 2008
  5. kas
    Replies:
    1
    Views:
    342
    red floyd
    Apr 22, 2010
Loading...

Share This Page