Segmentation fault while using pointer to pointer to a char

Rndp13

While compiling and executing the below program i am getting a warning during compilation and seg fault during execution.

Warning

program.c: In function main:
program.c:17: warning: passing argument 2 of convertString from incompatible pointer type

line 17 is the call to convertString(input, &output);

attaching the debugger is can see the segfault is occuring at the below line

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400521 in convertString (input=0x7fffffffd100 "abcdefg", output=0x7fffffffd0f0) at program.c:9
9           **output = *input;
(gdb) s

#include<stdio.h>
#include<string.h>
void convertString(char *input, char **output)
{
    if(strlen(input)== 0)
    {
        return;
    }
    **output = *input;
    (*output)++;
    convertString(input+1,output);
}
int main()
{
    char input[] = "abcdefg";
    char output[sizeof(input)];
    convertString(input, &output);
    printf("%s", output);
    return 0;
}

Please help me where i am doing wrong.

Some programmer dude

The problem is that output is not a pointer, it's an array, and using the address-of operator on an array gives you a (single) pointer to the array (in your case of type char (*)[8]). For clarification: (void *) output == (void *) &output, both the decayed pointer of the array, and the pointer to the array, are equal.

So you're not really passing a pointer to a pointer to char to the function.


Besides the above, I see no reason that the function should take a pointer to a pointer, a plain single pointer should be enough. Otherwise you will get wrong result when using output in the printf call as then output would point beyond the end of the string.

Also, you don't terminate the output string.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

segmentation fault using char pointer (C)

From Dev

segmentation fault in passing a char* pointer

From Dev

Segmentation Fault While Checking Pointer

From Dev

segmentation fault in c about using malloc, and char array pointer

From Dev

Segmentation fault in C while using int pointer argument

From Dev

Assigning (const char *) pointer type - Segmentation fault

From Dev

expected segmentation fault while incrementing NULL pointer

From Dev

Segmentation fault while dereferencing the pointer in the calling function

From Dev

Segmentation fault while moving value to stack pointer

From Dev

Segmentation Fault using stack and frame pointer

From Dev

Segmentation Fault, pointer issue?

From Dev

Segmentation fault on pointer

From Dev

Segmentation fault, pointer issue

From Dev

Program gives segmentation fault while trying to malloc for a pointer member of structure

From Dev

Segmentation fault while trying to print parts of a pointer struct

From Dev

Segmentation fault in C while declaring large pointer array

From Dev

Pointer-to-a-pointer in C throws segmentation fault

From Dev

Pointer to pointer allocation then iteration causes segmentation fault

From Dev

Pointer Pointer leads to Segmentation Fault in C

From Dev

Segmentation fault - printf value from pointer to pointer

From Dev

Pointer incrementation causing segmentation fault

From Dev

Segmentation fault: Pointer to an array of string

From Dev

C array pointer segmentation fault

From Dev

Pointer of a struct with vector - Segmentation Fault

From Dev

Segmentation fault: Pointer to an array of string

From Dev

Struct pointer array segmentation fault

From Dev

Segmentation fault in double pointer looping

From Dev

Segmentation fault on malloc for double pointer

From Dev

Segmentation fault in C. Trying to swap two values using pointer

Related Related

  1. 1

    segmentation fault using char pointer (C)

  2. 2

    segmentation fault in passing a char* pointer

  3. 3

    Segmentation Fault While Checking Pointer

  4. 4

    segmentation fault in c about using malloc, and char array pointer

  5. 5

    Segmentation fault in C while using int pointer argument

  6. 6

    Assigning (const char *) pointer type - Segmentation fault

  7. 7

    expected segmentation fault while incrementing NULL pointer

  8. 8

    Segmentation fault while dereferencing the pointer in the calling function

  9. 9

    Segmentation fault while moving value to stack pointer

  10. 10

    Segmentation Fault using stack and frame pointer

  11. 11

    Segmentation Fault, pointer issue?

  12. 12

    Segmentation fault on pointer

  13. 13

    Segmentation fault, pointer issue

  14. 14

    Program gives segmentation fault while trying to malloc for a pointer member of structure

  15. 15

    Segmentation fault while trying to print parts of a pointer struct

  16. 16

    Segmentation fault in C while declaring large pointer array

  17. 17

    Pointer-to-a-pointer in C throws segmentation fault

  18. 18

    Pointer to pointer allocation then iteration causes segmentation fault

  19. 19

    Pointer Pointer leads to Segmentation Fault in C

  20. 20

    Segmentation fault - printf value from pointer to pointer

  21. 21

    Pointer incrementation causing segmentation fault

  22. 22

    Segmentation fault: Pointer to an array of string

  23. 23

    C array pointer segmentation fault

  24. 24

    Pointer of a struct with vector - Segmentation Fault

  25. 25

    Segmentation fault: Pointer to an array of string

  26. 26

    Struct pointer array segmentation fault

  27. 27

    Segmentation fault in double pointer looping

  28. 28

    Segmentation fault on malloc for double pointer

  29. 29

    Segmentation fault in C. Trying to swap two values using pointer

HotTag

Archive