segmentation fault

A

aarklon

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

wiseker

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);
 
J

Joachim Schmitz

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
 
A

aarklon

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
 
J

Joachim Schmitz

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
 
W

wiseker

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

Flash Gordon

(e-mail address removed) wrote, On 04/11/07 13:00:

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

santosh

Okay i tried this

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

Your code does not use anything from said:
#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'.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top