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

void fun(char** a)
{
    strcpy(*a, "ponky" );
}

int main()
{
    char a[100] = "pinky";
    fun(&a );
    printf("\n %s \n", a );
    return 0;
}

Copying a string in a function through double pointer. Why the above program gives segmentation fault?

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • 4
    *"warning: incompatible pointer types passing `char (*)[100]` to parameter of type `char **`"* If you didn't see that warning, you need to enable more warnings, e.g. by using `-Wall` with gcc or clang. If you **did** see that warning, but ignored it, then the lesson for the day is: don't ignore warnings. – user3386109 Nov 23 '19 at 06:39
  • 1
    The correct declaration is `void fun(char *a)` and then `strcpy(a, "pony");` and call the function with `fun(a);` – user3386109 Nov 23 '19 at 06:41
  • `fun(&a );` is not a `char **` but `char *` more precisely `char (*)[100]`. – kiran Biradar Nov 23 '19 at 06:43
  • 1
    I'm pretty sure this has been asked before but I can't find an exact duplicate right now. Anyway - this should tell you everything :-) https://stackoverflow.com/questions/2528318/how-come-an-arrays-address-is-equal-to-its-value-in-c – Support Ukraine Nov 23 '19 at 07:00
  • Also helpful: https://stackoverflow.com/questions/38202077/what-does-getting-the-address-of-an-array-variable-mean – Support Ukraine Nov 23 '19 at 07:02

1 Answers1

3

note: I suspect this question to be a duplicate and I've found many similar questions but I haven't found an exact duplicate. Therefore I post an answer. Should someone find an exact duplicate I'll remove this answer.

Why the above program gives segmentation fault?

The short answer is that you have a type mismatch when calling fun. You don't give fun a "pointer to pointer to char" as it expects. Consequently, it fails big time.

But what do you then pass to fun? And how should you have found out that something was wrong?

The answer is: Set compiler warning level high and consider all warnings to be errors

For gcc that could be:

gcc -xc -Wall -pedantic -Werror main.c

(Other compilers have similar options)

On my system I get:

In function 'main':
error: passing argument 1 of 'fun' from incompatible pointer type [-Werror=incompatible-pointer-types]
   13 | fun(&a );
      |     ^~
      |     |
      |     char (*)[100]
note: expected 'char **' but argument is of type 'char (*)[100]'
    3 | void fun(char** a)
      |          ~~~~~~~^

so it's clear that something is wrong and the following line tells it all:

note: expected 'char **' but argument is of type 'char (*)[100]'

you pass char (*)[100] instead of char **

But what is char (*)[100]?

It's a "pointer to an array of char". Since fun uses it as "pointer to pointer to char" you have undefined behavior (which in your case resulted in a seg fault). That is - fun would expect *a to be a "pointer to char" but you passed "pointer to an array of char" so *a is not a "pointer to char".

It's undefined behavior so we can't tell what is going on. However, on many systems it will read the string "pinky" and interpretate it as a pointer to char which will fail big time.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63