views:

299

answers:

6

Hello folks,

This one is probably very simple, but I can't seem to get it working.

I have this very simple snippet of code:

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

int main(void)
{

  char buf[100];
  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.

+9  A: 

You're passing a character to printf; you should be passing the pointer.

char buf[100];
char *p = buf;

strcpy(p, "Test string");

printf("%s\n", p);     // p, not *p
zildjohn01
Ah, yes. When I deference p I get a char hence the confusion of printf. Does that sounds about right? Thanks.
Hamza
+1 For being the first one to answer
neuro
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
I get it now, thanks zildjohn01.
Hamza
+2  A: 

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.

Eric Petroelje
Hi Eric, thanks - that fixed it although I don't understand why. Shouldn't we deferencing the pointer to get the contents?
Hamza
@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
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
@Hamza : Your line will work and print one char if you use printf("%c\n", *p)
neuro
+1  A: 

just pass the string(the pointer):

printf("%s\n", p);

If you want to print the first char, then:

printf("%c\n", *p);
AraK
Thanks for the explanation AraK.
Hamza
+3  A: 

Use this:

printf("%s\n", p);

use "p" instead of "*p"

Ehsan
Thanks for the fix user358443
Hamza
+3  A: 

When you write

printf("%s\n", *p);

the *p will be the value at p[0] 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);
Il-Bhima
Hello Il-Bhima, thanks for your clear explanation.
Hamza
A: 

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

jim mcnamara