user1936506
user1936506

Reputation: 39

Polynomial Division , in methods

I have a strange error in the next code (it's doing a polynomial division). Not sure, but as I see, the problem is in the declaration of the methods. After the code, Iv'e listed the errors that I keep getting.

here's the code:

  1
     2  int[] mul(int d[],int t,int idxq);
     3  int[] sub(int a[],int b[]);
     4  int check(int r[],int lengthr);
     5  int[] cp(int a[]);
     6  int[] div(int n[],int d[]);
     7
     8
     9
    10  int[] mul(int d[],int t,int idxq){
    11  int i;
    12  int size=sizeof(d)/sizeof(d[0]);
    13  int c[size];
    14  for(i=size-1;i>0;i--)
    15  c[idxq+i]=d[i]+t;
    16  return c;
    17  }
    18
    19  int[] sub(int a[],int b[]){
    20
    21  int i;
    22  int j;
    23  int size=sizeof(a)/sizeof(a[0]);
    24  int fin[size];
    25  for(j=0;jsize;j++)
    26  fin[j]=a[j];
    27  for(i=0;isize;i++)
    28  fin[i]=a[i]-b[i];
    29
    30  return fin;
    31  }
    32
    33  int check(int r[],int lengthr){
    34  int i;
    35  for(i=0;i<lengthr;i++){
    36  if(r[i]!=0)
    37  return 1;
    38  }
    39  return 0;
    40  }
    41
    42
    43  int[] cp(int a[]){
    44  int size=sizeof(a)/sizeof(a[0]);
    45  int i;
    46  int fin[size];
    47  for(i=0;i<size;i++)
    48  fin[i]=a[i];
    49
    50  return fin;
    51  }
    52
    53  int[] div(int n[],int d[]){
    54  int idxq=0;
    55  int t=0;
    56  int r[sizeof(n)/sizeof(n[0])]=cp(n);
    57  int lengthr= sizeof(r)sizeof(r[0]);
    58  int lengthd= sizeof(d)sizeof(d[0]);
    59
    60  while(check()!=0 && r[lengthr-1]=d[sizeof(d[0]])
    61  t=r[lengthr-1]d[sizeof(d[0]])-1];
    62  idxq=lengthr-d[sizeof(d[0]])-1]
    63  q[idxq]=t;
    64  r=sub[r,mul(d,t,idxq)];
    65  idxq=0;
    66
    67  }
    68
    69  int main(){
    70
    71  int a[4]={-42,0,12,1};
    72  int b[2]=(-3,1);
    73  div(a,b);
    74  }

Here are the errors:

q6.c:2: error: expected identifier or  Ç ( Ç  before  Ç [ Ç  token
q6.c:3: error: expected identifier or  Ç ( Ç  before  Ç [ Ç  token
q6.c:5: error: expected identifier or  Ç ( Ç  before  Ç [ Ç  token
q6.c:6: error: expected identifier or  Ç ( Ç  before  Ç [ Ç  token
q6.c:10: error: expected identifier or  Ç ( Ç  before  Ç [ Ç  token
q6.c:19: error: expected identifier or  Ç ( Ç  before  Ç [ Ç  token
q6.c:43: error: expected identifier or  Ç ( Ç  before  Ç [ Ç  token
q6.c:53: error: expected identifier or  Ç ( Ç  before  Ç [ Ç  token
q6.c: In function  Ç main Ç :
q6.c:72: error: invalid initializer

Thanks for help! :)

Upvotes: 0

Views: 1681

Answers (4)

Shafik Yaghmour
Shafik Yaghmour

Reputation: 158469

As other have pointed out you are can not return an array from a function and you can not have variable length arrays without using dynamic allocation but you are also trying to return a pointer to a local variable from the stack, for example, in cp:

int fin[size];
 //... other code    
return fin;
       ^^^

fin is allocated on the stack of the function and will not exist once you exit. One potential solution would be to use dynamic memory via malloc and return that pointer but you then need to remember to free that memory later on:

int *fin = malloc(size*sizeof(int));

this method also has the added advantage of not requiring a const size. We now come back to the fact that you can not return an array so your function needs to return int *:

int* cp(int a[], size_t size)
                 ^^^^^^

Note use of size_t instead of int, which is the type that malloc actually take as an argument:

void *malloc(size_t size);

and they return type of sizeof.

Upvotes: 0

unwind
unwind

Reputation: 399833

Not "strange" at all.

C doesn't have arrays as first-class citizens; you can't return them from functions.

Also, this:

int size=sizeof(d)/sizeof(d[0]);

is not valid since any array passed to a function will decay into a pointer to the first element, and thus sizeof d will be sizeof (int *). You need to pass the array length as a separate argument.

Upvotes: 2

bash.d
bash.d

Reputation: 13207

You cannot return arrays from functions, but pointers if necessary. Change your first line from

int[] mul(int d[],int t,int idxq);

to

int* mul(int d[],int t,int idxq);

and continue to do this for every return value where there are [].

Additionally, you would be returning pointers to local objects, which would lead to undefined behavior in best case or crash your program with a SEGFAULT at worst.

In order to return an array from a function, you will need to allocate it on the heap using malloc() and pass the size of the original array as a function-argument. For example

int* mul(int d[],int t,int idxq, int array_size){
    int i;
    int* c = malloc(array_size*sizeof(int));
    for(i=array_size-1;i>0;i--)
    c[idxq+i]=d[i]+t;
    return c;
}

Upvotes: 0

FDinoff
FDinoff

Reputation: 31429

You can't return arrays in c.

So int[] mul(int d[],int t,int idxq); is invalid.

Also I see you have int size=sizeof(d)/sizeof(d[0]); in some of your functions. This won't generally get you the length of the array. It will only get you the length of the array if you know it at compile time.

Upvotes: 0

Related Questions