c - 将结构的两个成员相加

标签 c struct

    typedef struct Int40
{
  // a dynamically allocated array to hold a 40
  // digit integer, stored in reverse order
  int *digits;
} Int40;

在 main 中我实现了这些函数,loadCryptoVariable 和 loadHwConfigVariable 各自返回一个 40 位的值

Int40 *p;
  Int40 *q;
  Int40 *r;
    p = loadCryptoVariable("cryptoVarFile");
      q = loadHWConfigVariable(0);
     r = kw26Add( p, q);

但是,我无法弄清楚如何将两者相加。弄清楚此刻的添加)

Int40 *kw26Add(Int40 *p, Int40 *q)
{
    Int40 *result;
    result = malloc(300);
    result->digits = malloc(300);

    result->digits = p->digits + q->digits;

     return result;
}

最佳答案

我不确定我是否理解这个问题,但在我阅读它时,您需要遍历数组。例如:

for (int i = 0; i < 40; ++i)
    result->digits[i] = p->digits[i] + q->digits[i];

关于c - 将结构的两个成员相加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49205149/

相关文章:

c++ - Linux函数以一定的间隔调用

c++ - 如何从 lua_topointer 访问 lua 的对象?

c++ - 'null' 和 'NULL' 相同吗?

struct - 在Rust中传递对结构的引用

c - 在分配新结构时遇到问题

C函数指针无效值问题

c - 什么是预处理器定义禁用警告 "GetVersionExW' : was declared deprecated"

Swift:ViewModel 应该是结构体还是类?

c - sizeof() 函数如何用于 C 中的结构?

c - 有一个没有定义的结构是什么意思?