Adding two members of a struct together

2019-08-02 14:41发布

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

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

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;
}

标签: c struct
1条回答
甜甜的少女心
2楼-- · 2019-08-02 15:18

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];
查看更多
登录 后发表回答