0

Consider the following program:

#include <stdio.h>
void foo(char** string)
{
    printf("%s", string[0]);
}

int main()
{
    char* a = "blahblah";
    foo(&a);
    return 0;
}

It works fine as it is, but if I substitute

char* a = "blahblah";

with

char a[] = "blahblah";

it does not work.

I get the warning expected 'char **' but argument is of type 'char (*)[9]', and a segmentation fault. I was under the impression that char[] and char* are the same thing, so a pointer to each of them would also be the same.

(windows with mingw, gcc 4.8.1)

Thank you

greatwolf
  • 20,287
  • 13
  • 71
  • 105
Cantfindname
  • 2,008
  • 1
  • 17
  • 30
  • 4
    **Never** use a non-const pointer to point to a string literal. And pointers are not arrays. Repeat that 1000 times. – chris Dec 11 '13 at 00:38
  • 1
    I'm positive this has been asked before in a slightly different form. Try searching for difference between `char *` and `char []`. Hint, they're not really the same type. – greatwolf Dec 11 '13 at 00:40
  • "I was under the impression..." – Kerrek SB Dec 11 '13 at 00:45
  • For a sample of how they're different, try printing `&a+1 - &a` in `main` for each of them. – chris Dec 11 '13 at 00:50
  • Read the following: http://stackoverflow.com/questions/10186765/char-array-vs-char-pointer-in-c – Fiddling Bits Dec 11 '13 at 00:54
  • I understand the difference between char * and char [], but here I have char ** and I am still confused as to why it doesn't work. I do not write to the string anywhere, so it should not matter if it's a literal or not. (@chris : &(a+1) - &a gives me 1 at both cases) – Cantfindname Dec 11 '13 at 01:12
  • 1
    @Cantfindname, I literally meant `&a + 1`. The point is that it moves a different number of bytes with the array than with the pointer. And in the case of `char **`, it's just a pointer to the type. A pointer to a pointer is not a pointer to an array. – chris Dec 11 '13 at 01:33
  • Why, do you not to `void foo(char *string)`., – BLUEPIXY Dec 11 '13 at 01:36

2 Answers2

3

In most cases these two statements behave the same, with two exceptions. One exception is when applying the & operator. When you apply & to an array(e.g. a[]), you got the address of the whole array. This value is identical to the address of the first element of the array, so &a == a (their types are different tough). You can try a simple example here: http://ideone.com/96w3oa

Ok. Now we can see why you got a segmentation fault. Because &a is equal to a, your string[0] actually does an extra deference. The correct way would be:

printf("%s", string);

or

printf("%s", (&string)[0]);

if you use

char a[] = "blahblah";

For more information regarding the difference between the two statements you tried, please refer to this post (especially the post by John Bode): What is the difference between char s[] and char *s?.

Here is also a very good explanation: http://publications.gbdirect.co.uk/c_book/chapter5/arrays_and_address_of.html

Community
  • 1
  • 1
ZillGate
  • 1,173
  • 12
  • 22
0

When you are passing the array to the foo function, you should do in this manner.

#include<stdio.h>
void display(char *);

int main()
{
char arr[]="Do something";
display(arr);
return 0;
}
void display(char *string)
{
printf("%s",string);
}

else you might be getting segmentation error.while using &array_name, you actually pass the address of the starting element.

Arvind kr
  • 103
  • 1
  • 3
  • 12