Up vote 1 down vote favorite share g+ share fb share tw.
This one is probably very simple, but I can't seem to get it working. I have this very simple snippet of code: #include #include int main(void) { char buf100; char *p = buf; strcpy(p, "Test string"); printf("%s\n", *p); } Which causes a segmentation fault when I run it. GDB outputs: Program received signal SIGSEGV, Segmentation fault.
0xb76af3b3 in strlen () from /lib/i686/cmov/libc.so.6 But I still don't get it. Comments would be appreciated, thanks. C pointers printf segmentation-fault link|improve this question asked Jun 4 '10 at 16:56Hamza307212 85% accept rate.
1 You could have looked at the GCC warning to get an idea of the problem: test. C:In function ‘main’: test. C:12: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’ – kotlinski Jun 4 '10 at 17:23 Funny enough I did not get that warning message, if I did I probably could have saved everyone some time including myself :) – Hamza Jun 4 '10 at 17:32 You might want to make sure to use the -Wall compiler flag to make sure to don't miss the warnings.
– VeeArr Jun 4 '10 at 18:53.
When you write printf("%s\n", *p); the *p will be the value at p0 which is a character. The printf however is looking for an array of chars, thus causing it to segfault. Remember that in C, strings are just arrays of characters, and arrays are effectively pointers to the first element, this is why you don't need to dereference.
To fix this remove the * to get: printf("%s\n", p).
Thanks for your clear explanation. – Hamza Jun 4 '10 at 17:04.
You're passing a character to printf; you should be passing the pointer. Char buf100; char *p = buf; strcpy(p, "Test string"); printf("%s\n", p); // p, not *p.
Ah, yes. When I deference p I get a char hence the confusion of printf. Does that sounds about right?
Thanks. – Hamza Jun 4 '10 at 17:01 +1 For being the first one to answer – neuro Jun 4 '10 at 17:03 1 When you pass *p, you're passing only a single character by value. When you pass p, you're passing a pointer to the first character in the string, so printf can access the rest of the string.
– zildjohn01 Jun 4 '10 at 17:05 I get it now, thanks zildjohn01. – Hamza Jun 4 '10 at 17:08.
Use this: printf("%s\n", p); use "p" instead of "*p.
Thanks for the fix user358443 – Hamza Jun 4 '10 at 17:05.
Replace printf("%s\n", *p); with printf("%s\n", p); When you use %s, printf expects you to pass a char*. You are passing a char instead.
Thanks - that fixed it although I don't understand why. Shouldn't we deferencing the pointer to get the contents? – Hamza Jun 4 '10 at 16:59 @Hamza - only if you wanted to print a single character.
If you wanted to print the whole string, you need to pass the pointer so that printf can access it. Otherwise all it has is the character you passed it (and not the rest of the string). That make more sense?
– Eric Petroelje Jun 4 '10 at 17:02 That was the explanation I was looking for, many thanks! It's fascinating how easy it is to forget the principles after working with high level languages for a while :) – Hamza Jun 4 '10 at 17:03 @Hamza : Your line will work and print one char if you use printf("%c\n", *p) – neuro Jun 4 '10 at 17:05.
Just pass the string(the pointer): printf("%s\n", p); If you want to print the first char, then: printf("%c\n", *p).
Thanks for the explanation AraK. – Hamza Jun 4 '10 at 17:04.
%s causes printf() to dereference *p. Suppose the string was "Test string". Then on my Solaris sparc box: (in a test program) p would be "aimed at" the address 0x54657374.
The probability of that particular address being part of your process space is next to zero. That is what caused the SIGSEGV signal (segfault).
I experienced some segmentation fault problems while using printf: printf("Segmentation fault\n").
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.