segmentation fault

Discussion in 'C Programming' started by aarklon@gmail.com, Nov 4, 2007.

  1. Guest

    char *f()
    {
    char *s = malloc(8);
    strcpy(s,"good bye");
    }

    int main(void)
    {

    printf("\n %s",*f()='A');

    } why program is giving segmentation fault
    but when %s format specifier is changed to %c there is no
    segmentation fault??
    , Nov 4, 2007
    #1
    1. Advertising

  2. wiseker Guest

    wrote:
    > char *f()
    > {
    > char *s = malloc(8);
    > strcpy(s,"good bye");
    > }
    >
    > int main(void)
    > {
    >
    > printf("\n %s",*f()='A');
    >
    > } why program is giving segmentation fault
    > but when %s format specifier is changed to %c there is no
    > segmentation fault??
    >

    char *s = malloc(9);
    or char *s = malloc(strlen("good bye") + 1);
    wiseker, Nov 4, 2007
    #2
    1. Advertising

  3. <> schrieb im Newsbeitrag
    news:...
    > char *f()
    > {
    > char *s = malloc(8);
    > strcpy(s,"good bye");
    > }
    >
    > int main(void)
    > {
    >
    > printf("\n %s",*f()='A');
    >
    > } why program is giving segmentation fault
    > but when %s format specifier is changed to %c there is no
    > segmentation fault??

    What did the compiler warn you about? You invoce undefined behavoir all over
    the place and I don't really understand what you're trying to achieve.
    Guess you want something like this:

    #include <stdio.h> /* for printf() */
    #include <stdlib.h> /* for malloc() */
    #include <string.h> /* for strcpy() */

    char *f(void)
    {
    char *s = malloc(9); /* "good bye" is 9 chars, incl. the terminating \0
    */
    if ( s )
    strcpy(s, "good bye");
    return s;
    }

    int main(void)
    {
    printf"%s\n", f());
    return 0;
    }


    Hmm, what does printf() if the malloc() fails (and therfor f() returns
    NULL)?

    Bye, Jojo
    Joachim Schmitz, Nov 4, 2007
    #3
  4. Guest

    On Nov 4, 7:51 am, "Joachim Schmitz" <>
    wrote:
    > <> schrieb im Newsbeitragnews:...> char *f()
    > > {
    > > char *s = malloc(8);
    > > strcpy(s,"good bye");
    > > }

    >
    > > int main(void)
    > > {

    >
    > > printf("\n %s",*f()='A');

    >
    > > } why program is giving segmentation fault
    > > but when %s format specifier is changed to %c there is no
    > > segmentation fault??

    >
    > What did the compiler warn you about? You invoce undefined behavoir all over
    > the place and I don't really understand what you're trying to achieve.
    > Guess you want something like this:
    >
    > #include <stdio.h> /* for printf() */
    > #include <stdlib.h> /* for malloc() */
    > #include <string.h> /* for strcpy() */
    >
    > char *f(void)
    > {
    > char *s = malloc(9); /* "good bye" is 9 chars, incl. the terminating \0
    > */
    > if ( s )
    > strcpy(s, "good bye");
    > return s;
    >
    > }
    >
    > int main(void)
    > {
    > printf"%s\n", f());
    > return 0;
    >
    > }
    >
    > Hmm, what does printf() if the malloc() fails (and therfor f() returns
    > NULL)?
    >
    > Bye, Jojo


    Okay i tried this

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

    char *f()
    {
    char *s = malloc(9);
    assert(s != NULL);
    strcpy(s,"good bye");

    }

    int main(void)
    {
    printf("\n %s",*f()='A');
    }

    but still i am getting segmentation fault
    , Nov 4, 2007
    #4
  5. <> schrieb im Newsbeitrag
    news:...
    > On Nov 4, 7:51 am, "Joachim Schmitz" <>
    > wrote:
    >> <> schrieb im
    >> Newsbeitragnews:...>
    >> char *f()
    >> > {
    >> > char *s = malloc(8);
    >> > strcpy(s,"good bye");
    >> > }

    >>
    >> > int main(void)
    >> > {

    >>
    >> > printf("\n %s",*f()='A');

    >>
    >> > } why program is giving segmentation fault
    >> > but when %s format specifier is changed to %c there is no
    >> > segmentation fault??

    >>
    >> What did the compiler warn you about? You invoce undefined behavoir all
    >> over
    >> the place and I don't really understand what you're trying to achieve.
    >> Guess you want something like this:
    >>
    >> #include <stdio.h> /* for printf() */
    >> #include <stdlib.h> /* for malloc() */
    >> #include <string.h> /* for strcpy() */
    >>
    >> char *f(void)
    >> {
    >> char *s = malloc(9); /* "good bye" is 9 chars, incl. the terminating
    >> \0
    >> */
    >> if ( s )
    >> strcpy(s, "good bye");
    >> return s;
    >>
    >> }
    >>
    >> int main(void)
    >> {
    >> printf"%s\n", f());
    >> return 0;
    >>
    >> }
    >>
    >> Hmm, what does printf() if the malloc() fails (and therfor f() returns
    >> NULL)?
    >>
    >> Bye, Jojo

    >
    > Okay i tried this
    >
    > #include<stdio.h>
    > #include<stdlib.h>
    > #include<limits.h>
    > #include<assert.h>
    > #include<string.h>
    >
    > char *f()
    > {
    > char *s = malloc(9);
    > assert(s != NULL);
    > strcpy(s,"good bye");
    >
    > }
    >
    > int main(void)
    > {
    > printf("\n %s",*f()='A');
    > }
    >
    > but still i am getting segmentation fault
    >

    What did your compiler warn you about (at highest warning level)?
    Why do you thing that *f()='A' is a string (wich is what %s is about in
    printf()?
    What are you trying to achieve with your code?

    Your f() doesn't return anything (missing return statement)

    Bye, Jojo
    Joachim Schmitz, Nov 4, 2007
    #5
  6. wiseker Guest

    wrote:
    > On Nov 4, 7:51 am, "Joachim Schmitz" <>
    > wrote:
    >> <> schrieb im Newsbeitragnews:...> char *f()
    >>> {
    >>> char *s = malloc(8);
    >>> strcpy(s,"good bye");
    >>> }
    >>> int main(void)
    >>> {
    >>> printf("\n %s",*f()='A');
    >>> } why program is giving segmentation fault
    >>> but when %s format specifier is changed to %c there is no
    >>> segmentation fault??

    >> What did the compiler warn you about? You invoce undefined behavoir all over
    >> the place and I don't really understand what you're trying to achieve.
    >> Guess you want something like this:
    >>
    >> #include <stdio.h> /* for printf() */
    >> #include <stdlib.h> /* for malloc() */
    >> #include <string.h> /* for strcpy() */
    >>
    >> char *f(void)
    >> {
    >> char *s = malloc(9); /* "good bye" is 9 chars, incl. the terminating \0
    >> */
    >> if ( s )
    >> strcpy(s, "good bye");
    >> return s;
    >>
    >> }
    >>
    >> int main(void)
    >> {
    >> printf"%s\n", f());
    >> return 0;
    >>
    >> }
    >>
    >> Hmm, what does printf() if the malloc() fails (and therfor f() returns
    >> NULL)?
    >>
    >> Bye, Jojo

    >
    > Okay i tried this
    >
    > #include<stdio.h>
    > #include<stdlib.h>
    > #include<limits.h>
    > #include<assert.h>
    > #include<string.h>
    >
    > char *f()
    > {
    > char *s = malloc(9);
    > assert(s != NULL);
    > strcpy(s,"good bye");
    >
    > }
    >
    > int main(void)
    > {
    > printf("\n %s",*f()='A');
    > }
    >
    > but still i am getting segmentation fault
    >

    I have missed something.

    The reason is you printf a character with string format.
    wiseker, Nov 4, 2007
    #6
  7. Flash Gordon Guest

    wrote, On 04/11/07 13:00:

    <snip>

    > Okay i tried this
    >
    > #include<stdio.h>
    > #include<stdlib.h>
    > #include<limits.h>
    > #include<assert.h>
    > #include<string.h>
    >
    > char *f()
    > {
    > char *s = malloc(9);
    > assert(s != NULL);
    > strcpy(s,"good bye");
    >
    > }
    >
    > int main(void)
    > {
    > printf("\n %s",*f()='A');
    > }
    >
    > but still i am getting segmentation fault


    %s takes a pointer to a string you are passing a character.
    --
    Flash Gordon
    Flash Gordon, Nov 4, 2007
    #7
  8. santosh Guest

    wrote:

    > On Nov 4, 7:51 am, "Joachim Schmitz" <>
    > wrote:
    >> <> schrieb im
    >>

    Newsbeitragnews:...>
    >> char *f()
    >> > {
    >> > char *s = malloc(8);
    >> > strcpy(s,"good bye");
    >> > }

    >>
    >> > int main(void)
    >> > {

    >>
    >> > printf("\n %s",*f()='A');

    >>
    >> > } why program is giving segmentation fault
    >> > but when %s format specifier is changed to %c there is no
    >> > segmentation fault??

    >>
    >> What did the compiler warn you about? You invoce undefined behavoir
    >> all over the place and I don't really understand what you're trying
    >> to achieve.


    > Okay i tried this
    >
    > #include<stdio.h>
    > #include<stdlib.h>
    > #include<limits.h>


    Your code does not use anything from <limits.h>

    > #include<assert.h>
    > #include<string.h>
    >
    > char *f()
    > {
    > char *s = malloc(9);
    > assert(s != NULL);
    > strcpy(s,"good bye");


    You don't return anything after telling the compiler that the function
    returns a char * value. Place a

    return s;

    here.

    > }
    >
    > int main(void)
    > {
    > printf("\n %s",*f()='A');


    What are you trying to do here? The 's' format specifier expects a char
    * argument and you are passing an int argument here. Character
    constants are of type int in C.

    Also because you do not return a sensible value from 'f' the compiler is
    going to access some random garbage, invoking undefined behaviour.

    Also another

    return 0;

    here.

    > }
    >
    > but still i am getting segmentation fault


    What are trying to do? Overwrite the string returned from 'f' with
    another string literal? If so, do:

    int main(void)
    {
    char *p;
    p = f();
    printf("%s\n", p);
    free(p);
    p = "A";
    printf("%s\n", p);
    return 0;
    }

    And _return_ _a_ _sensible_ _value_ from 'f'.
    santosh, Nov 4, 2007
    #8
    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. Alex Hunsley
    Replies:
    17
    Views:
    852
  2. Pud
    Replies:
    0
    Views:
    564
  3. Replies:
    0
    Views:
    518
  4. Ivan Vecerina
    Replies:
    0
    Views:
    477
    Ivan Vecerina
    Jun 29, 2003
  5. Vasileios Zografos

    Re: segmentation fault exception handling

    Vasileios Zografos, Jun 30, 2003, in forum: C++
    Replies:
    5
    Views:
    15,577
    Pete Becker
    Jul 1, 2003
Loading...

Share This Page