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

8
  • 4
    Never use a non-const pointer to point to a string literal. And pointers are not arrays. Repeat that 1000 times. Commented Dec 11, 2013 at 0: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. Commented Dec 11, 2013 at 0:40
  • "I was under the impression..." Commented Dec 11, 2013 at 0:45
  • For a sample of how they're different, try printing &a+1 - &a in main for each of them. Commented Dec 11, 2013 at 0:50
  • 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. Commented Dec 11, 2013 at 1:33

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.