Hi everyone.

My question is how to copy values of 2 arrays from one function into 2 new arrays into another function?

Copy data from arraay1[i] into arraay3[8] and from arraay2[i] into arraay4[8] ?

int onefunction(int araay[]){
.......
.......
arraay1[i];
arraay2[i];
}

int twofunction(int araay[]){
arraay3[8] = {0};
arraay4[8] = {0};
}

Recommended Answers

All 17 Replies

Use a loop.

You've never used a loop before?

Of Course, but i'm learning functions now, since yesterday.
I can't figure out how to use the loop here.

if you want to copy the arrays that are declared in oneFunction() into the arrays declared in twoFunction() then onefunction() will have to call twofunction() passing it the two arrays. twofunction() will need two parameters, not one.

The exact same way you do there.

define funct(A1, size-of-array)
{
    define a2
    loop from 0 to size-of-array
        copy values
}

The exact same way you do there.

Wait. You're saying that the syntax and semantics of loops don't change from function definition to function definition? That's just crazy talk. ;p

commented: You know me -- crazeee +14
commented: yes! i also know you ;) +0

Like this?

int onefunction(int araay[]){

    arraay1[i];
    arraay2[i];
}

int twofunction(int araay1[], int araay2[]){

    arraay3[8] = {0};
    arraay4[8] = {0};
    int i;

    for(i = 0; i < sizeof(araay1); i++)
    array3[i] = araay1[i];
}

Kind of. sizeof doesn't quite work that way. You want to pass the size into the function.

What does onefunction() do?

onefunction() calculate the values of 2 numbers, a number with 8 digits and a number with 5 digits, and store the values into 2 arrays.

You want to pass the size into the function.

What do you mean? How can i do that?

What do you mean? How can i do that?

Think. Reread your section on functions.

Just because you post a question here does not mean you can turn your brain off. You still have to figure many things out for yourself.

If i knew that, I would not ask the question.
I can't figure out how to do it.

Thanks all for your "solutions".

I can't figure out how to do it.

You know how to get the size of an array, and you know how to pass arguments into a function. Put those two together and it's easy to see why one might think you've turned your brain off:

#include <stdio.h>

void foo(int size)
{
    printf("%d\n", size);
}

int main(void)
{
    int a[10];

    foo(sizeof a);

    return 0;
}

Thanks all for your "solutions".

With that attitude, you won't get any more. Best of luck with your homework.

It is not a homework. I try to understand how arrays and functions work together. I start programming 3 weeks ago.
What do you expect you all programming experts if a new programmer try to learn how to program?

Try to remember your first steps. This is not an answer: "Use loops".
What is that mean? I tried with loops, i tried with sizeof, i try to solve my problem a couple of hours now.
What about if your programming teacher can't explain as you expect. I searched help at daniweb.

Try to remember your first steps.

Sorry dude, but you'll get no sympathy from me on that front. The resources you have at your disposal would have given me a woody when I was first learning.

This is not an answer: "Use loops".

Did you learn anything at all from the code I provided? You pass the size of the array from the function that it was declared in because array parameters aren't actually arrays, they're pointers, and the size information is lost.

firstly, I will advice you to read what are loops and what is sizeof! then read more about C and its syntax and more about pointers. then read about functions and then about arguments. keep doing like this, and then try to implement various things in C. that will be beneficial to you and all. ;)

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.