Segmentation error with pointer to pointer with C / Linux

In the following code, I get a segmentation error:

Set *getpar() {...}

char function(...) 
{
   Set **S;
   *S = getpar(); /* Segmentation Fault */
   ...
}

But the strange thing is that with minor changes there is no segmentation error:

Set *getpar() {...}
...
char function(...) 
{
   Set *S;       // One less '*'
   S = getpar(); // One less '*'
   ...
}

As I know, if there is a " Set **S", then it *Sis a pointer to an object Set, therefore, if the second code works fine, why should not it be the first? *SThe first code is equivalent to the Ssecond code, right? How can i solve the problem?

+3
source share
2 answers

Set ** S is not triggered, but you are looking for S in the following statement:

* S = any

, , S , , .

:

Set **S;
S = (S**)calloc(sizeof(S*),1);
*S = getpar();

, ( , ):

Set *S;
Set **T = &S;

S = getpar();

/* whatever else */
+9

** S . (), .

0

Source: https://habr.com/ru/post/1707124/


All Articles