title: Access and store value by index in a pointer
body: <p>I've got an assignment where I have to sum whole numbers up to 100 digits.</p>
  <p>They gave me this struct to represent big numbers (I think there are better ways to represent this, but I'm not allowed to modify it):</p>
  <pre><code>typedef struct {
    char* string;
    int lengthError;
  } BigNumber;
  </code></pre>
  <p>Where <code>string</code> is the number itself and <code>lengthError</code> is the length of the number or an error that is a previously defined enum.</p>
  <p>I've also have the implementation of the sum function</p>
  <pre><code>BigNumber *sum(BigNumber* num1, BigNumber* num2) {
      BigNumber* result = malloc(sizeof(BigNumber));
      int limit = getLength(num1-&gt;lengthError, num2-&gt;lengthError);
      result-&gt;string = malloc(limit);
      int digitResult;
      int index = limit -1;
      int carry = 0;
  
      while(index != -1) {
          int d1 = ((int)num1-&gt;string[index]) - ((int)'0');
          int d2 = ((int)num2-&gt;string[index]) - ((int)'0');
          digitResult = d1 + d2 + carry;
  
          if (digitResult &gt; 9) {
              digitResult = digitResult - 10;
              carry = 1;
          } else {
              carry = 0;
          }
  
          itoa(digitResult, &amp;result-&gt;string[index], 10); //I think this is the problem
          index--;
      }
  
      result-&gt;string[limit] = '\0';
      printf(&quot;result: %s\n&quot;, result-&gt;string);
      return result;
  }
  </code></pre>
  <p>I haven't finished writing that function, I know there are a lot of flaws in it, but the problem is that I can't get to sum 12 + 12. The result I get is 2.</p>
  <p>I thought approaching this problem by picking the lasts character of both numbers, transform them into an <code>int</code> and sum them having in mind the carry digit. After I got the result in <code>digitResult</code> I try to convert it to a char and store it in the corresponding position of the <code>result-&gt;string</code> pointer</p>
  <p>Once it has finished the operation, I add an <code>\0</code> at the last position of the <code>result-&gt;string</code>.</p>
  <p>So the question is, how do I make this operation to work as desired? Debugging the code, I noticed that the first time it stores the first result in result-&gt;string, following the example above this would be a number 4, it stores trash in that position instead. In the second addition, I store a number 2 correctly and that's the final result I get in when I print the result.</p>