Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your code doesn't work, because you are making several mistakes.</p> <p>1) you don't allocate enough space for the buffer <code>x</code>. </p> <pre><code>char x[]="sss"; </code></pre> <p>This means there will be 3 bytes (plus null) allocated for <code>x</code>. But in your <code>sprintf</code> you write a larger string to that location. You're lucky it doesn't just segfault (well it didn't for me).</p> <p>2) you don't copy strings into new space; instead, on every loop you overwrite the same bit of memory (the buffer <code>x</code>) with a new string. Since each of the <code>item[h]</code> is therefore pointing at the same string, they will all give you the same result in the second loop.</p> <p>Breaking this second point down a bit (and assuming you made <code>x</code> big enough initially, by putting a lot more ssssssssssss in it during initialization):</p> <pre><code>storage buffer x contains item[h] points to so when I print it I get sssssssssssssssss nothing item = item (0) storage buffer x item = item(0) item = item (1) storage buffer x item = item(1) item = item (2) storage buffer x item = item(2) item = item (3) storage buffer x item = item(3) item = item (4) storage buffer x item = item(4) item = item (5) storage buffer x item = item(5) </code></pre> <p>When I go around the loop again, item[h] still points to x, which still contains</p> <pre><code>item = item (5) </code></pre> <p>To get this to work you would need to allocate space for each string you want to store (not just pointers to it...):</p> <pre><code>#include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; int main(void){ int h = 0; char* item[6]; char *x; for (h = 0; h &lt; 6; h++) { item[h] = (char*)malloc(sizeof(char*)); x = (char*)malloc(50); // big enough sprintf(x, "%s (%d)", "item", h); item[h] = x; printf("item= %s\r\n", item[h]); } for (h = 0; h &lt; 6; h++) { printf("item22222= %s\r\n", item[h]); } } </code></pre> <p>Output:</p> <pre><code>item= item (0) item= item (1) item= item (2) item= item (3) item= item (4) item= item (5) item22222= item (0) item22222= item (1) item22222= item (2) item22222= item (3) item22222= item (4) item22222= item (5) </code></pre> <p>Of course you are going to have a hard time freeing the memory allocated like this... but in a small program you can let the cleaning up be done by the operating system on exit.</p> <p><strong>EDIT</strong> Since it seems that the <code>malloc</code> operation is causing trouble, here is a way to make the program work. It involves "preallocating" the memory used, rather than doing so dynamically. Of course this won't work for very large amounts of data, but then unless and until you discover why you have problems with even a very small <code>malloc</code>, you are pretty much out of luck getting a larger program running. So this is for instruction only.</p> <p>Recall that you are trying to write text to your "item" array. If we make it a 2D array of <code>char</code>, and preallocate the data, then we're all set. Trick: the address of <code>item[h][0]</code> can be found as either <code>&amp;item[h][0]</code> or <code>item[h]</code>.</p> <p>Here is the code:</p> <pre><code>#include &lt;stdio.h&gt; int main(void){ int h = 0; char item[6][50]; // array of 6 strings of up to 50 characters for (h = 0; h &lt; 6; h++) { sprintf(item[h], "%s (%d)", "item", h); printf("item= %s\r\n", item[h]); } for (h = 0; h &lt; 6; h++) { printf("item22222= %s\r\n", item[h]); } } </code></pre> <p>I hope you have more luck with this... it gave me the same output as before.</p> <p><strong>Another edit</strong></p> <p>Since the underlying problem (<code>malloc</code> failing) is still there, we could try to troubleshoot that. Please try running this program and see what happens:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; int main(void){ char* c; int ii; for(ii=1;ii&lt;=50;ii++) { c = malloc(ii); printf("with ii = %d, c is %p\n", ii, c); if(c!=NULL) free(c); c = malloc(sizeof(int) * ii); printf("allocating in chunks of 'sizeof(int)': c is now %p\n", c); if (c!=NULL) free(c); } } </code></pre> <p>We should see if / when <code>malloc</code> succeeds or fails (it was working in your original snippet, it seems... so now we need to find out when / why it breaks...)</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. This table or related slice is empty.
    1. 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