![]() |
| | #1 |
| Registered User Join Date: Sep 2005
Posts: 20
| Array coping into another array?or function returning array So is there easy way to copy array of int to another one not using the loop for Or how can i get thet array from class in such manner that i can edit it without editing one in array. example: Code: class Example
{
public:
Example();//let say it already sets values of array[0]=1 and array[1]=2
~Example();
don't know what GetArray();//function passing array
int GetArray[1]();//which returns array[1]
private:
int array[2];
}
Code: int Whatdifferenc(int number)
{
class Example exm;
int array2[2],result;
//passing array from exm to array2
array2[1]=number;
result=exm.GetArray[1]()-array2[1];
return result;
}
|
| baniakjr is offline | |
| | #2 |
| (?<!re)tired Join Date: May 2006 Location: Portugal
Posts: 5,656
| I'm not sure if I understood you correctly. But it seems what you need to know is how to pass arrays as arguments to functions and how to return arrays from functions and you are aso having doubts on how arrays are created and initialized. Lets declare an array of ints and initialize it: Code: int myarray[5] = {11,12,13,14,15};
// or
int myarray[] = {11,12,13,14,15}; // dimensions are taken from the initialization
Code: int anotherarray[] = myarray; // error. cannot assign one array to another. int anotherarray[](myarray); // error. Cannot initialize an array as a copy of another. Code: const int ARRAY_SIZE = 5;
int myarray[ARRAY_SIZE] = {11,12,13,14,15};
int anotherarray[ARRAY_SIZE];
for (int i = 0; i != ARRAY_SIZE; ++i) {
anotherarray[i] = myarray[i];
}
Let's create a function that adds some value to each element of an array. Code: void add_to_elements(int* arr, const int size, int value) {
// arr is a pointer to int (read below)
// size is the size of the array passed
// value is the value to add to each element
for (int i = 0; i != size; ++i) {
arr[i] += value;
}
}
Code: int someval = 12;
const int ARRAY_SIZE = 5;
int myarray[ARRAY_SIZE] = {11,12,13,14,15};
add_to_elements(myarray, ARRAY_SIZE, 5); // Ok.
add_to_elements(someval, ARRAY_SIZE, 5); // Error!
The reason why I'm making a mention to it is because a nasty error can occur from using functions that operate on arrays with non arrays as arguments. Change the first line of the previous code to this: Code: int val = 12; int* someval = &val; Code: for (int i = 0; i != size; ++i) {
arr[i] += value;
}
Code: void add_to_elements(int arr[], const int size, int value) So, now you already have a pretty good idea on how to pass arrays to functions. But how about returning arrays? You can't. You can however return a pointer to the first element of the array or the element itself. Code: int get_array_element(int* arr, int elem) {
return arr[elem];
}
int* get_pointer to_first_array_element(int* arr, const int ARRAY_SIZE) {
// do whatever you want to do to the array
return arr; // return the array name which is converted to a pointer to the first element.
}
And I guess this pretty much covers what you want to do.
__________________ Originally Posted by brewbuck: Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster. Last edited by Mario F.; 11-08-2006 at 09:27 AM. |
| Mario F. is offline | |
| | #3 | ||
| Registered User Join Date: Apr 2006
Posts: 1,312
| Quote:
The hard way is to allocate memmory for you a new array and copy your array into it. Then pass the new array to the function. (or convercely, you could allocate the new array inside the function) But you have to be sure to free the memmory when you are done with it. Yes, this would generally use a for loop. Quote:
Besides that your example would work just fine.
__________________ It is too clear and so it is hard to see. A dunce once searched for fire with a lighted lantern. Had he known what fire was, He could have cooked his rice much sooner. | ||
| King Mir is offline | |
| | #4 |
| Registered User Join Date: Sep 2005
Posts: 20
| Thanks for answers.They're quit helpfull.Now i know that i always pass a pointer not an array to function and that i have to make a copy to distinguish them. And my second question is.Are there any other ways to copy one array to another other then Code: for (int i = 0; i != ARRAY_SIZE; ++i) {
anotherarray[i] = myarray[i];
Ok i know that my previous post was a little bit messed up(because my english and because i was tired).So here's something to streight it up.What i now wont realy to do is an accesor inside a class that will pass the pointer to the copy of array that is a private data of class(or any other way to pass that array). Code: class Example
{
public:
Example();
~Example();
int* GetArray();//function passing pointer to copy of array
private:
int array[2];
}
|
| baniakjr is offline | |
| | #5 |
| (?<!re)tired Join Date: May 2006 Location: Portugal
Posts: 5,656
| > And my second question is.Are there any other ways to copy one array to another other then [code showing a for loop] Yes. You can use memcpy(). Code: const int SIZE = 5;
int myarray[size] = {12,13,14,15,16};
int otherarray[size];
memcpy(otherarray, myarray, SIZE);
Use for. And if you don't like it, use while
__________________ Originally Posted by brewbuck: Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster. |
| Mario F. is offline | |
| | #6 |
| Registered User Join Date: Sep 2005
Posts: 20
| thanks a lot.Your help was most usefull.Now everything works fine.finaly i used mem allocation and for loop. |
| baniakjr is offline | |
| | #7 | |
| Registered User Join Date: Mar 2006
Posts: 726
| Quote:
__________________ Code: #include <stdio.h>
void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
/3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}
| |
| jafet is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Returning Array | baffa | C Programming | 26 | 02-01-2008 10:08 AM |
| Returning an Array | mmarab | C Programming | 10 | 07-19-2007 07:05 AM |
| Unknown Memory Leak in Init() Function | CodeHacker | Windows Programming | 3 | 07-09-2004 09:54 AM |
| Dynmic 2d array, Problem with returning it from function | Iamien | C++ Programming | 6 | 10-21-2003 09:57 PM |
| Hi, could someone help me with arrays? | goodn | C Programming | 20 | 10-18-2001 09:48 AM |