I am trying to write a function to add two arrays. The catch is that the arrays have to parts of a structure. So here is what I have so far:

#include <stdio.h>

struct poly {
        array1[];
        array2[];
        length;
        };

void polyadd(int array1[],int array2[],int length) {      
        int i;
        struct poly p;
        for(i=0;i<length;i++) {
                p.array1[i]=array1[i] + array2[i];
                printf(" %d ", p.array1[i]);
        }
}

int main(void) {
        struct poly name1;
        name1.length = 4;
        name1.array1[4] = {3,0,7};
        name1.array2[4] = {0,15,0,-4};
        polyadd(name1.array1,name1.array2,name1.length);
        return 0;
}

And here are the error messages I am getting:

structadd.c:4: error: expected specifier-qualifier-list before âarray1â
structadd.c: In function âpolyaddâ:
structadd.c:13: error: âstruct polyâ has no member named âarray1â
structadd.c:14: error: âstruct polyâ has no member named âarray1â
structadd.c: In function âmainâ:
structadd.c:20: error: âstruct polyâ has no member named âlengthâ
structadd.c:21: error: âstruct polyâ has no member named âarray1â
structadd.c:21: error: expected expression before â{â token
structadd.c:22: error: âstruct polyâ has no member named âarray2â
structadd.c:22: error: expected expression before â{â token
structadd.c:23: error: âstruct polyâ has no member named âarray1â
structadd.c:23: error: âstruct polyâ has no member named âarray2â
structadd.c:23: error: âstruct polyâ has no member named âlengthâ

>array1[];
>array2[];

A type and a size are both required.

>length;
A type is required.

>name1.array1[4] = {3,0,7};
>name1.array2[4] = {0,15,0,-4};

Alas, arrays can't be assigned to like that. You need to do this during the initialization of the structure instance, or manually assign to each element:

#include <stdio.h>

struct poly {
  int array1[4];
  int array2[4];
  int length;
};

void polyadd(int array1[],int array2[],int length) {      
  struct poly p;
  int i;

  for(i=0;i<length;i++) {
    p.array1[i]=array1[i] + array2[i];
    printf(" %d ", p.array1[i]);
  }
}

int main(void) {
  struct poly name1 = {
    { 3, 0, 7 },      /* array1 */
    { 0, 15, 0, -4 }, /* array2 */
    4                 /* length */
  };

  polyadd(name1.array1,name1.array2,name1.length);

  return 0;
}

Well, just my two cents, I only lurk in these forums because I otherwise don't get enough practice at C, which will soon be a significant part of my job for the first time in many years.

First, you don't give any type to the arrays. Also, unless you define an array to be a specific size, it can only be the last thing in your struct. With two dynamic arrays, your structure needs pointers to the arrays.

>With two dynamic arrays, your structure needs pointers to the arrays.
You're the first one in this thread to mention dynamic arrays. Why over-engineer the problem and confuse the OP?

>With two dynamic arrays, your structure needs pointers to the arrays.
You're the first one in this thread to mention dynamic arrays. Why over-engineer the problem and confuse the OP?

Because the OP created the arrays without a size.

You're the first one in this thread to be rude. Why drive off responders?

>Because the OP created the arrays without a size.
And without a type. Are we also to assume that he wants a variant type?

>You're the first one in this thread to be rude.
If you think that's rude, you're going to have a heart attack when I really get going. Grow some thicker skin or you'll end up with an ulcer.

>Why drive off responders?
In my experience, those who get driven off by perceived rudeness weren't worth a damn to begin with. Those that stick around end up being valuable peers. Which are you?

Someone who ignores sociopaths, once he knows who they are.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.