0

I have this code:

#include <stdio.h>
#include <stdlib.h>

int inserimentoarray(int* arrayuno[], int* arraydue[]);
int calcolo(int* arrayuno[5], int* arraydue[5], int* arraytre[5]);

int arrayuno[5], arraydue[5], arraytre[5];

int main (/*int arrayuno[5], int arraydue[5], int arraytre[5]*/)
{
    int cont=0;
    inserimentoarray(arrayuno,arraydue);
    calcolo(arrayuno[5],arraydue[5],arraytre[5]);
    for (cont=0;cont<5;cont++) {
        printf("%d    +    %d     =     %d",arrayuno[cont],arraydue[cont],arraytre[cont]);
    }
    return 0;
}

void inserimentoarray(int* arrayuno[], int* arraydue[])
{
    int cont=0;
    for (cont=0;cont<5;cont++) {
        scanf("%d",&arrayuno[cont]);
        scanf("%d",&arraydue[cont]);
    }
}

void calcolo(int* arrayuno[5], int* arraydue[5], int* arraytre[5])
{
    int cont=0;
    for (cont=0; cont<5; cont++) {
        arraytre[cont]=arrayuno[cont];
    }
}

How can I make this work? It's my first using of functions in C and I don't know how to pass correctly an array from a function to another.

2 Answers 2

2

Here is a quick solution

void addArrays(const int s1[], const int s2[], int dest[], int n)
{
    int i;
    for(i = 0; i < n; ++i)         // iterate over the array indices
       dest[i] = s1[i] + s2[i];    // save sum of the two source arrays in destination array
}

s1 stands for source1, s2 for source2 and dest for destination. nis the number of array elements you arrays consist of.

Sign up to request clarification or add additional context in comments.

Comments

1

The major insight should probably be the type equivalence of an array variable with a pointer to the base type.

Keep in mind that there is a difference when it comes to memory allocation: Declaring an array variable allocates the memory for its elements while this does not happen for a pointer decalration.

After the declaration you only pass around the variable. Imagine that th declaration gives you a handle on a memory location - in C only the programmer has any further information about what structure starts at this memory location ( this is oversimplifying but should give you a start), which is curse (maintenance!) and boon (efficiency!) at the same time. The modern perspective on software development clearly emphasizes design, reuse and maintainabiity and would thus consider it primarily a curse ...

In particular you do not specify array bounds in formal parameters of function

Keeping the structure of your program for instructional purposes,this would be a rectified version:

#include <stdio.h>
#include <stdlib.h>


void  inserimentoarray( int* au, int* ad );
void  calcolo ( int* au, int* ad, int* at);

int arrayuno[5], arraydue[5], arraytre[5];


int main ()
{
    int cont=0;

    inserimentoarray ( arrayuno, arraydue );
    calcolo ( arrayuno, arraydue, arraytre );
    for (cont=0;cont<5;cont++) {
        printf("%d    +    %d     =     %d; ", arrayuno[cont], arraydue[cont], arraytre[cont]);
    }
    return 0;
}

void  inserimentoarray( int* au, int* ad )
{
    int cont=0;
    for (cont=0; cont<5; cont++) {
        scanf("%d",&(au[cont]));
        scanf("%d",&(ad[cont]));
    }
}

void  calcolo ( int* au, int* ad, int* at)
{
    int cont=0;
    for (cont=0; cont<5; cont++) {
        at[cont] = au[cont] + ad[cont];
    }
}

I have prepared a codepad (the scanf calls are replaced by hardcoded assignments).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.