I have this code in func
I am getting pointer to array of pointers. The purpose of this function is to write strings to two allocated spaces for chars
. Allocated in main. I am getting segFault at this line
memcpy(*c[1],"hi",sizeof("hi"));
this is full code
void func(char (**c)[])
{
memcpy(*c[0],"hello",sizeof("hel"));
memcpy(*c[1],"hi",sizeof("hi"));
}
int main()
{
char (*arr)[2]=malloc(sizeof(char[2][10]));
func(&arr);
printf("%s\n",arr[0]);
printf("%s\n", arr[1]);
return 0;
}
I have allocated the array of pointers using
char (*arr)[2]=malloc(sizeof(char[2][10]));
now to pass the address of two allocated string spaces I am calling like this
func(&arr);
the point of passing the address of array of pointers variable arr
so I can see the changes to string spaces in main(...)
, CHANGES THAT MADE IN FUNC
but this line causing trouble
memcpy(*c[1],"hi",sizeof("hi"));
segFault
Can anyone please inform me what I am doing wrong this answer is also related to array of pointers. https://stackoverflow.com/a/69551741/4808760
Output I am expecting is
hell
hi
Valgrind dump
valgrind --leak-check=full -s ./a.out
==7397== Memcheck, a memory error detector
==7397== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==7397== Using Valgrind-3.17.0 and LibVEX; rerun with -h for copyright info
==7397== Command: ./a.out
==7397==
==7397== Invalid write of size 2
==7397== at 0x4849F23: memcpy@GLIBC_2.2.5 (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==7397== by 0x1091F2: func (test.c:8)
==7397== by 0x10922A: main (test.c:18)
==7397== Address 0x54698dcdde89a700 is not stack'd, malloc'd or (recently) free'd
==7397==
==7397==
==7397== Process terminating with default action of signal 11 (SIGSEGV)
==7397== General Protection Fault
==7397== at 0x4849F23: memcpy@GLIBC_2.2.5 (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==7397== by 0x1091F2: func (test.c:8)
==7397== by 0x10922A: main (test.c:18)
==7397==
==7397== HEAP SUMMARY:
==7397== in use at exit: 30 bytes in 1 blocks
==7397== total heap usage: 1 allocs, 0 frees, 30 bytes allocated
==7397==
==7397== LEAK SUMMARY:
==7397== definitely lost: 0 bytes in 0 blocks
==7397== indirectly lost: 0 bytes in 0 blocks
==7397== possibly lost: 0 bytes in 0 blocks
==7397== still reachable: 30 bytes in 1 blocks
==7397== suppressed: 0 bytes in 0 blocks
==7397== Reachable blocks (those to which a pointer was found) are not shown.
==7397== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==7397==
==7397== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
==7397==
==7397== 1 errors in context 1 of 1:
==7397== Invalid write of size 2
==7397== at 0x4849F23: memcpy@GLIBC_2.2.5 (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==7397== by 0x1091F2: func (test.c:8)
==7397== by 0x10922A: main (test.c:18)
==7397== Address 0x54698dcdde89a700 is not stack'd, malloc'd or (recently) free'd
==7397==
==7397== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)