Passing an array of integers as function parameter in C

Please help me, i'm stuck at this. I have a function which must sum two arrays of integers of size n and i must pass as arguments for the function the first element of each array Is this enven close?

 12 void sumVect(int * v[], int * w[] ,int n)
 13 //adds the second vector to the first vector; the vectors v and w have exactly n components
 14 {
 15         int i;
 16         for(i=0;i<n;i++)
 17         {
 18                 *v[i]+=*w[i];
 19         }
 20 
 21 }

I will give you the whole code

#include <stdio.h>

void sumVect(int * v[], int * w[] ,int n)
//adds the second vector to the first vector; the vectors v and w have exactly n components
{
    int i;
    for(i=0;i<n;i++)
    {
        *v[i]+=*w[i];
    }

}


int main()
{   
    int i,j;
    int n = 4;  //the size of a vector
    int k = 5;  //the number of vectors
    int v[k][n];    //the vector of vectors

    printf("How many vectors? k=");
    scanf("%d",&k);

    printf("How many components for each vector? n=");
    scanf("%d",&n);

    for(i=1;i<=k;i++)   //read the content of each vector from the screen
    {
        printf("\nv%d   |\n_____|\n",i);

        for(j=0;j<n;j++)
        {
            printf("v%d[%d]=",i,j);
            scanf("%d", &v[i][j]);
        }
    }

    for(j=1;j<k;j++)
    {
        sumVect(&v[j], &v[j+1], n);
        for(i=0;i<n;i++)
        {
            printf("%d",v[j][i]);
        }
    }


    return 0;
}

Upvotes: 1

Views: 14110

Answers (3)

phoxis
phoxis

Reputation: 61910

Note that you are passing type int [] but your formal parameters in the function is int *[] . Therefore your signature should be:

void sumVect(int v[], int w[] ,int n)

or

void sumVect(int *v, int *w,int n)

Also you should access them in the function like

v[i] + w[i] because v and w both are int * or int [] (depends on the formal paremeter)

Also when calling you should do:

sumVect(v[j], v[j+1], n);

or

sumVect(&v[j], &v[j+1], n);

This is because as as v[j] is type of int [] , &v[i] and v[i] will evaluate to the same address.

Upvotes: 4

Iain Rist
Iain Rist

Reputation: 996

Arrays and pointers are generally treated as the same thing in c. An array variable holds the pointer to the array an the [] notation is a shortcut for pointer arithmatic.

Note that n should be an unsigned type.

void sumVect(int * v, int * w, uint n)
{
    while (n--)
    {
        *v++ += *w++;
    }
}

Upvotes: 1

Jurlie
Jurlie

Reputation: 1014

This is the corrected version of your function sumVect.

void sumVect(int * v, int * w ,int n)
//adds the second vector to the first vector; the vectors v and w have exactly n components
{
    int i;
    for(i=0;i<n;i++)
    {
        v[i]+=w[i];
    }

}

But whole your needs a number of corrections. Look at this code:

int n = 4;  //the size of a vector
int k = 5;  //the number of vectors
int v[k][n];    //the vector of vectors

printf("How many vectors? k=");
scanf("%d",&k);

printf("How many components for each vector? n=");
scanf("%d",&n);

Here is the allocation of 2-dimentional array of size 5x4, and then your program asks the user what the size of the array actually should be. The simplest way to fix it is to use heap allocation of array. Here is fixed code:

int n = 4;  //the size of a vector
int k = 5;  //the number of vectors
int **v = NULL;    //the vector of vectors

printf("How many vectors? k=");
scanf("%d",&k);

printf("How many components for each vector? n=");
scanf("%d",&n);
v = malloc(sizeof(int*), k);
for( int i = 0; i < k; ++i)
   v[i] = malloc(sizeof(int), n);

In the end you should free the allocated memory in the similar way, but let it be your homework.

Upvotes: 1

Related Questions