Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>CurrentData *AllCurrentData = '\0'; </code></pre> <p>This declares a pointer. This pointer is a variables that holds a number that is interpreted as an address. You initialize the pointer to '\0' (null).</p> <pre><code>getCurrentData(current, AllCurrentData); </code></pre> <p>Here you pass this pointer as parameter to function <code>getCurrentData</code>. As a pointer is a variable, this variable is passed by value, meaning that the function receives a copy of that value (the number that represents an address).</p> <p>When inside the function if you write</p> <pre><code>AllCurrentData = malloc... </code></pre> <p>you modify that copy of the pointer, so outside the function AllCurrentData will still be '\0'. You need to pass a pointer to that pointer (Inception, I know).</p> <pre><code>getCurrentData(current, &amp;AllCurrentData); getCurrentData(.., CurrentData **p_AllCurrentData) { *p_AllCurrentData = malloc(...); } </code></pre> <hr> <p><strong>Let me explain this concept on a simpler example:</strong></p> <pre><code>int *v = NULL; // here v is a pointer. This pointer has value 0 (invalid address) v = malloc(10 * sizeof(int)); // here the pointer is assigned a valid address, lets say 0x41A0 f(v); void f(int *x) { // here the pointer x receives a copy of the value of the pointer v, so x will be, like v, 0x41A0 x[0] = 4; /*this is ok as you modify the memory at the address 0x41A0, so this modification is seen from outside the function, as v points to the same address.*/ x = malloc(...); /* this is NOT ok, as x receives a new address, lets say 0xCC12. But because x has a copy of the value from v, v will still be unmodified.*/ } </code></pre> <p>so if you want to allocate a pointer inside a function this is not ok:</p> <pre><code>int *v = NULL; // here v is a pointer. This pointer has value 0 (invalid address) f(v); void f(int *x) { // here the pointer x receives a copy of the value of the pointer v, so x will be, like v, NULL x = malloc(...); /*this is NOT ok, as x receives a new address, lets say 0xCC12. But because x has a copy of the value from v, v will still be unmodified, it will still have NULL.*/ } </code></pre> <p>The right way to do is:</p> <pre><code>int *v = NULL; // here v is a pointer. This pointer has value 0 (invalid address) // as v is a variable (of type pointer to int), v has an address, lets say 0xAAAA. f(&amp;v); void f(int **p_x) { /* here the p_x is a pointer to (a pointer of int), so this pointer receives a copy of the value the address of p, so p_x is 0xAAAA.*/ *p_x = malloc(...); /* lets say malloc returns the address 0xBBBB. This will be the address of our vector. *p_x= says we modify the value at the address p_x. So the value found at the adress 0xAAAA will be 0XBBBB. But because 0xAAAA is the address of v, we effectively modified the value of v to 0xBBBB. So now v has the address of our starting vector.*/ // if you want to modify values inside this vector you need: (*p_x)[0] = ... } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload