For one of my assignments in my C book, I have to find a way to treat characters as integers. So, I wrote a program that takes an array of characters that represent integers, add them up, and output the result. In the following program, I attempt to ‘add’ the strings ‘123’ and ‘321’ which should result in ‘444’.
To do this, I first find the distance from ‘3’, ‘2’, and ‘1’ from ‘0’, respectively. For example, ’3’ is 51 in ASCII and ‘0’ 48, so the difference (or distance) is the 3 ASCII code which translates to ETX. Refer to the table below.
The following is the implementation. Print statements are included for debugging purposes:
#include <stdio.h>
int main (int argc, char *argv[]) {
char *num1 = "123";
char *num2 = "321";
const char Z = 48; //value of zero
char from_z = 0;
char total[] = { 0 };
int i = 0;
//main for loop
for (i = 0; num1[i] != '\0' || num2[i] != '\0'; i++) {
from_z = num2[i] - Z; //distance from '0'
total[i] = num1[i] + from_z;
//print out contents of current index and previous
//to compare
printf("i total[%d]: %c\n", i, total[i]);
printf("i - 1: total[%d]: %c\n", i-1, total[i-1]);
}
printf("i: %d\n", i);
total[i] = '\0';
return 0;
}
The Bug:
When I assign and print the newly assigned index and the previously assigned one, during the second iteration, the value of total[1] changes from ‘4’ to ‘\x01’. Yet, total[0] and total[2] still contain ‘4’. Check the output below. (Of course ‘\x01’ would print out nothing)
i total[0]: 4
i - 1: total[-1]:
i total[1]: 4
i - 1: total[0]: 4
i total[2]: 4
i - 1: total[1]:
i: 3
As you can see, total[1] was first ‘4′, and then randomly became blank. Using the debugger, I could tell that the contents of total[1] is \x01.
What fixed it:
Having a static array where I defined total to have 4 indices like so: char total[4] = { 0 };, the problem was fixed. But I’d still like to know why it doesn’t work for dynamic arrays.
>Solution :
char total[] = { 0 };
is not a dynamic array — it is a fixed-size array with a single element. Writing to any element other than total[0] is writing outside the bounds of the array and invokes undefined behavior.
C doesn’t have "dynamic" arrays that automatically grow as you write new elements to them. If you need a resizable array, you need to allocate memory with either malloc or calloc:
#include <stdlib.h>
...
size_t totalSize = 1;
char *total = calloc( totalSize, sizeof *total );
and then manually resize it as necessary using realloc:
if ( i == totalSize )
{
char *tmp = realloc( total, sizeof *total * (totalSize * 2) );
if ( tmp )
{
total = tmp;
totalSize *= 2;
}
}
Make sure to clean up after yourself when you’re done:
free( total );