•

Check out our Community Blogs

Register and join over 40,000 other developers!

### Recent Blog Entries

• phi

I love this community !

• JackJames

hi i am jack i am seo expert jack james would love you to read new post

# C - How to copy values of 2 arrays...

c array

9 replies to this topic

### #1 equation

equation

CC Regular

• Member
• 27 posts
• Programming Language:C, Java, PHP
• Learning:C, Java

Posted 18 September 2012 - 07:16 AM

Hi everyone.

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

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

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

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

### #2 gregwarner

gregwarner

Obi Wan of Programming

• Expert Member
• 1586 posts
• Location:Arkansas
• Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 18 September 2012 - 07:42 AM

If twofunction() requires local copies of external arrays, then you must pass in these external arrays as parameters. Then, twofunction() must copy the contents of the passed in arrays into its own local copies, so that it can perform operations on its local copies without altering the originals.

The pseudocode would look something like this:
```twofunction(array1, array2)
declare array1copy
declare array2copy
copy(array1, array1copy)
copy(array2, array2copy)
// perform some operation on array1copy
// perform some operation on array2copy
```

The copy() function would look something like this:
```copy(source, destination)
Loop on 'i' from 0 to n-1, where n is the length of the source:
set destination[i] = source[i]
Next 'i'.
```

Since you're working in C, you must also pass length parameters along with these arrays, to tell the copy function how many items to copy ('n' in the above pseudocode), as well as ensure you have allocated sufficient memory in the destination arrays so that you do not suffer an overflow error.
• 0

Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid

### #3 equation

equation

CC Regular

• Member
• 27 posts
• Programming Language:C, Java, PHP
• Learning:C, Java

Posted 18 September 2012 - 08:10 AM

You mean like this? I tried but it does't work!

```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];
}
```

• 0

### #4 gregwarner

gregwarner

Obi Wan of Programming

• Expert Member
• 1586 posts
• Location:Arkansas
• Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 18 September 2012 - 08:25 AM

sizeof() is a bad way to get the size of an array. It return the size, in bytes, of the whole array. Notice I said bytes. That's different from the number of elements. For integer arrays, each integer is 4 bytes, meaning sizeof() returns a number which is 4 times too large!

Instead, declare a global constant integer that represents the maximum size of all arrays in your program, if your intent is to have one common size across the whole program. If you have a need for varying sizes of arrays, you'll need to add an additional parameter to twofunction() which accepts this array size.
• 0

Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid

### #5 equation

equation

CC Regular

• Member
• 27 posts
• Programming Language:C, Java, PHP
• Learning:C, Java

Posted 18 September 2012 - 08:32 AM

Ok, it's wrong with sizeof. I don't need any size. How can i copy the values that stored into the 2 arrays in the first function?
i don't understand this:

where n is the length of the source:

How can i do that?
• 0

### #6 gregwarner

gregwarner

Obi Wan of Programming

• Expert Member
• 1586 posts
• Location:Arkansas
• Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 18 September 2012 - 08:41 AM

If you only need one unvarying size of array throughout your whole program, then simply declare a global constant integer to store this value. Then, anywhere in the program where you need to declare a new array or loop over an array, use this constant instead of a hard-coded number.

To copy the local variables in your first function, simply call the 2nd function from within the body of your 1st function, passing the local variables of the 1st function as parameters to the 2nd function's call.
• 0

Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid

### #7 equation

equation

CC Regular

• Member
• 27 posts
• Programming Language:C, Java, PHP
• Learning:C, Java

Posted 18 September 2012 - 08:50 AM

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

I would like to call the onefunction() from within the twofunction() and the copy the values into the 2 new arrays.
• 0

### #8 gregwarner

gregwarner

Obi Wan of Programming

• Expert Member
• 1586 posts
• Location:Arkansas
• Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 18 September 2012 - 08:59 AM

Then this is totally different. You aren't creating the arrays in onefunction() and then copying them in twofunction(). Instead, you're just going to declare them in twofunction(), and pass them as parameters to onefunction(), which will modify their values, since arrays are passed by reference automatically in C. In other words, onefunction() will not have any local variables besides the two arrays which are passed in as parameters.

Edit: It would help very much if you gave your functions and variables more meaningful names, instead of onefunction(), twofunction(), array1, and array2. If the arrays are meant to represent something, name them after what they represent. Likewise, name the functions after what they do.
• 0

Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid

### #9 equation

equation

CC Regular

• Member
• 27 posts
• Programming Language:C, Java, PHP
• Learning:C, Java

Posted 18 September 2012 - 09:10 AM

I can't do that because onefunction calls another function and then do something. It is necessary to have local variables into onefunction() because calculates other values from another function.
• 0

### #10 gregwarner

gregwarner

Obi Wan of Programming

• Expert Member
• 1586 posts
• Location:Arkansas
• Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 18 September 2012 - 09:17 AM

Without seeing your whole block diagram and flowchart, I can't say for certain, but I'd be willing to bet your fears are unjustified, as you can always restructure things to work around the constraints. If you have the entire block diagram and flowchart of your application available to post on here, I'll take another look; otherwise, my previous suggestion still stands as the recommended solution.
• 0

Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid

### Also tagged with one or more of these keywords: c, array

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download