-1

I have the code as:

mid = 3;
for(i=0;i<9;i++)
{
   if(i == 9-mid)
      num[i] = mid;
   else
      num[i] = 0;
   printf("%d", num[i]);
}

which gives the result of "000000300".

What I try to do is to store "000000300" as an element of another array, i.e.

unsigned int array[0] = 000000300;

Any ideas of how to do this in C? Thanks~

1 Answers1

-1

If you want to copy the calculated string "000000300" you will need to allocate some memory and store it in a char * array:

// num is a char array containing "000000300".
char *stored = (char *)malloc(strlen(num) + 1);
if (stored == NULL) {
    // This means that there is no memory available.
    // Unlikely to happen on modern machines.
}
strcpy(stored, num);
DanielGibbs
  • 9,910
  • 11
  • 76
  • 121
  • Any way to scan the num array and pass the value "000000300" to an unsigned int array? – Shumin Xu Nov 18 '14 at 23:45
  • You could try and parse the string as an int (see http://stackoverflow.com/a/3421555/343486) but having leading 0's will cause problems as it will be interpreted as an octal number. – DanielGibbs Nov 19 '14 at 13:40