Why does this appear when compiling? "Segmentation fault (core dumped)"

Asked

Viewed 30 times

0

#include <stdlib.h>

int funcao(int **piParametro)
{
    
printf("%p\n",&piParametro);
printf("%p\n",piParametro);
printf("%p\n",*piParametro);
printf("%d\n",**piParametro);

return 0;
}

int main()
{
    
int *piVariavel;

*piVariavel = (int*) malloc(sizeof(int));
*piVariavel = 20;

printf("%p\n",&piVariavel);
printf("%p\n",piVariavel);
printf("%d\n",*piVariavel);

funcao( &piVariavel );

return 0;

} 
  • here is the problem: *piVariable = (int*) malloc(sizeof(int));

  • which should be "piVariable = (int*) malloc(sizeof(int));"

  • I’ll explain: *piVariable, is to access the memory point to which the pointer points, as you did not allocate anything, before accessing, this pointer ta with an invalid value, and instead of you assign the pointer malloc to it, you were trying to assign to the point to which he pointed

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.