Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This structure</p> <pre><code>char **Data[70]={NULL}; </code></pre> <p>is an array of 70 pointers to pointers to char. The compiler allocates <code>70 * sizeof(char**)</code> bytes for this array, which assuming 32-bit pointers is 280 bytes.</p> <p>If you internally think of a "pointer to char" as a string, which isn't true but it's close enough, then this is an array of 70 pointers to strings. To make some ASCII art and pretend that you have allocated and filled some values....</p> <pre><code> Array of One or more char ** char * +---------+ +---------+ | 0 | --&gt; | ptr | --&gt; "Hello, world" +---------+ +---------+ | 1 | +---------+ +---------+ | 2 | ----&gt; | ptr2 | --&gt; "Goodbye, cruel world" +---------+ +---------+ | 3 | +---------+ +---------+ | 4 | ------&gt; | ptr3[0] | --&gt; "Message 0" +---------+ +---------+ ... | ptr3[1] | --&gt; "Message 1" +---------+ +---------+ | 69 | | ptr3[2] | --&gt; "Message 2" +---------+ +---------+ </code></pre> <p>You could do the above with code like this (error checking malloc return values skipped):</p> <pre><code>char **Data[70]={NULL}; char **ptr, **ptr2, **ptr3; ptr = (char **) malloc(sizeof(char *)); *ptr = "Hello, world"; Data[0] = ptr; ptr2 = (char **) malloc(sizeof(char *)); *ptr2 = "Goodbye, cruel world"; Data[2] = ptr2; ptr3 = (char **) malloc(10 * sizeof(char *)); Data[4] = ptr3; ptr3[0] = "Message 0"; ptr3[1] = "Message 1"; ... ptr3[9] = "Message 9"; printf("%s\n", *Data[0]); printf("%s\n", Data[2][0]); printf("%s\n", Data[4][0]); printf("%s\n", Data[4][1]); ... printf("%s\n", Data[4][9]); </code></pre> <p>Think of it this way: Each entry in the array is a <code>char **</code>. Each entry can point to an arbitrary location in memory, said location(s) being <code>char *</code> and thus being able to point to a null-terminated character array aka "string."</p> <p>Note carefully the distinction between this and what you get when you allocate a 2D array:</p> <pre><code>char *Data2[10][70]={NULL}; </code></pre> <p>The allocation of <code>Data2</code> above gives you a 2-dimensional array of <code>char *</code> pointers, said 2-d array being allocated in a single chunk of memory (<code>10 * 70 * sizeof(char*)</code> bytes, or 2800 bytes with 32-bit pointers). You don't have the ability to assign the <code>char **</code> pointers to arbitrary locations in memory that you have with the single-dimensional array of <code>char **</code> pointers.</p> <p>Also note (given above declarations of <code>Data</code> and <code>Data2</code>) that the compiler will generate different code for the following array references:</p> <pre><code>Data[0][0] Data2[0][0] </code></pre> <p><strong>Here's another way to think about this</strong>: Imagine that you have several arrays of pointers to strings:</p> <pre><code>char *table0[] = { "Tree", "Bench", "Stream" }; char *table1[] = { "Cow", "Dog", "Cat" }; char *table2[] = { "Banana", "Carrot", "Broccoli" }; char **Data[3]; Data[0] = table0; Data[1] = table1; Data[2] = table2; </code></pre> <p>You have an array of pointers to "array of pointer to char". If you now print the value of <code>data[1][1]</code>, think of it like this: <code>data[1]</code> gets you a pointer to the array <code>table1</code>. Then the value <code>table1[1]</code> equals <code>"Dog"</code>.</p>
    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