数组复制C编程

时间:2014-11-18 23:13:43

标签: c arrays

我的代码为:

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

给出结果“000000300”。

我尝试做的是将“000000300”存储为另一个数组的元素,即

unsigned int array[0] = 000000300;

有关如何在C中执行此操作的任何想法?感谢〜

1 个答案:

答案 0 :(得分:-1)

如果要复制计算字符串"000000300",则需要分配一些内存并将其存储在char *数组中:

// 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);