将结构的两个成员加在一起 [英] Adding two members of a struct together

查看:37
本文介绍了将结构的两个成员加在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在主要我实现了这些函数,并且 loadCryptoVariable 和 loadHwConfigVariable 每个返回一个 40 位值

in main I have these functions implemented, and loadCryptoVariable and loadHwConfigVariable each return a 40 digit value

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

但是,我不知道如何将两者加在一起..(旁注:我知道我不应该像那样 malloc 并使用更明确的方式来做到这一点,但是,我只是在尝试找出此刻的添加)

However, I can't figure out how to add the two together..(side note: I am aware I shouldn't malloc like that and use a more defined way to do it, however, I'm just attempting to figure out the add at the moment)

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

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

     return result;
}

推荐答案

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

I'm not sure I understand the question, but as I read it, you would need to iterate through the array. For example:

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

这篇关于将结构的两个成员加在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆