Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'd suggest that you make <code>ptr_friends</code> a pointer to multiple chars by using <code>malloc(size_t)</code> and then resizing the space with <code>realloc(void *, size_t)</code> everytime you want to add an ID to the friendlist. That way you can just get the numbers using <code>ptr_friends[i]</code>.</p> <p>For example :</p> <pre><code>int friends_size = 1; char *ptr_friends = malloc((size_t)1); ptr_friends[0] = john_id; // john_id is a fictional ID here </code></pre> <p>And when you want to add a friend :</p> <pre><code>ptr_friends = realloc(ptr_friends, ++friends_size); ptr_friends[friends_size-1] = mary_id; </code></pre> <p><strong>EDIT :</strong></p> <p>If you want to make a function to add a friend, for example <code>addfriend(char *,int)</code>, doing the following is an error :</p> <pre><code>void addfriend(char *ptr_friends, int *friends_size, int id) { ptr_friends = realloc(ptr_friends, (size_t) ++(*friends_size)); ptr_friends[friends_size-1] = id; } </code></pre> <p><code>ptr_friends</code> here is getting reallocated, and since the pointer can move while being reallocated, we're storing it in <code>ptr_friends</code>. But, it's the <code>ptr_friends</code> from <strong>inside</strong> the function, that means that the pointer we give to the function will <strong>not</strong> get modified, since arguments to a function are <strong>copied</strong> elsewhere beforehand. That means that you have to give a pointer to the pointer, so you can modify the pointer in the main code.</p>
 

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