I need to transfer the contents of a 20-item char array of one object to another, and I'd really like to avoid using for loops (I have to do this several thousand times). is there a way to set a pointer to the first array, and transfer its contents to the second?
Thanks!
How can I use pointers to transfer the contents of an array to another of the same size and type in C++?
With a little work you can reduce the number of steps.
You can cast a pointer to long int to point to your array and move 8 bytes at a time, *only if* you assure that each of your arrays is 8-byte aligned.
The best way to do this is something like the following:
long fakeArray[3];
char *charArray = (char *)fakeArray;
then to copy do something like
long *longPtr = fakeArray, *longPtr2 = fakeArray2;
*longPtr++ = *longPtr2++;
*longPtr++ = *longPtr2++;
*longPtr++ = *longPtr2++;
Maybe even make this a macro:
#define COPY(AAA, BBB) {long *longPtr = (long *) %26amp;(AAA), *longPtr2 = (long *) %26amp;(BBB);\
*longPtr++ = *longPtr2++;\
*longPtr++ = *longPtr2++;\
*longPtr++ = *longPtr2++;\
}
Notice if you do this each of your arrays will need 24 bytes instead of 20, so if you really have a lot of them it will make your memory image larger.
It is often said that premature optimization is a real enemy to good code. Computers are really fast, my guess is that copying short arrays is not going to slow you down very much. Why don't you write the program normally first and then run a profiler to see if this is a problem or not?
Reply:I don't know. I know when programming for Palm, there is an api call named MemMove which will do this, but I don't know for windows.
Maybe you should write that segment in assembly, that would make it quick, but not so easy :)
Hope that helps!
Reply:sorry, u must use a loop to increment the pointer's position
but i guess u can use something called memcpy "it's C not C++ , but i guess u will find it in C++"
or u can use any function that maniuplate strings in C++ like strcat or strcpy and this stuff
sorry if i didn't give u a big help but im really not good in C++, u should move to C# coz it's much better
plants
Thursday, July 30, 2009
Plzz help me out! How to pass a 2-D array from main() to other function in C, using pass by value.....?
Duplicate the original in the main() funtion then pass a pointer to the new 2D array.
Plzz help me out! How to pass a 2-D array from main() to other function in C, using pass by value.....?
i don't think you can do it....arrays are passed using refrence only....but im not sure...
Plzz help me out! How to pass a 2-D array from main() to other function in C, using pass by value.....?
i don't think you can do it....arrays are passed using refrence only....but im not sure...
Sorting using Array in C++?
How to Sort three numbers using Array in C++?
Let say input 3 number which looks like this:
Input Number 1: 5
Input Number 2: 4
Input Number 3: 7
and the output will became: 457
anyone?
Sorting using Array in C++?
So what you want to do is sort from smallest to largest? That's fairly routine. However, in case this is a homework question, I will refer you to "Algorithms in C++" by Sedgewick, and give you a web page that might help - http://www.cs.sunysb.edu/~algorith/files...
If you're dealing with a dataset that's not too large and efficiency isn't an issue, I'd recommend going with a binary sort for simplicity.
Let say input 3 number which looks like this:
Input Number 1: 5
Input Number 2: 4
Input Number 3: 7
and the output will became: 457
anyone?
Sorting using Array in C++?
So what you want to do is sort from smallest to largest? That's fairly routine. However, in case this is a homework question, I will refer you to "Algorithms in C++" by Sedgewick, and give you a web page that might help - http://www.cs.sunysb.edu/~algorith/files...
If you're dealing with a dataset that's not too large and efficiency isn't an issue, I'd recommend going with a binary sort for simplicity.
Read input file into an array in C?
can anyone help me to read an input file of text into a char array in C. whenever i am doing fscanf(...)!=EOF ...it hangs ... anyone with working CODE... ??
Read input file into an array in C?
are you input text to the file ?????
if so you can use
fputc(ch,ft)
if you want to read input file into a c array
ch=fgetc(ft)
where ch is the char array and ft is the file pointer
or
Read input file into an array in C?
are you input text to the file ?????
if so you can use
fputc(ch,ft)
if you want to read input file into a c array
ch=fgetc(ft)
where ch is the char array and ft is the file pointer
or
How in c++ i can check if array(n*n) have numbers from 1 to n?
I would sort the array from smallest to largest, then see if a number is skipped. If you want to save time, delete repeats.
int highest_probed_num = 0;
for ( int i = 0; i %26lt; array.length-1; i++){
if ( (array[i] != array[i+1]) || (array[i] != array[i+1]+1)){
return false;
}
if ( (array[i] == n)){ break;} //this is incase there are numbers higher than N AND you only care about numbers between 1 and N.
}
return true;
PS. SW, I think "array(n*n)" means that his array is n^2 elements in it instead of a double array.
How in c++ i can check if array(n*n) have numbers from 1 to n?
when you say have numbers from 1 to n, do you mean between 1 and n?
for(int row = 0; row %26lt; n; ++n) {
for(int col = 0; col %26lt;n; ++col) {
if (array(row,col) %26lt; 1 ) || (array(row,col) %26gt; n )) {
++failure;
}
!UPDATE
Kat: Could be n*n. Can't figure if he wants to number from 1 to n in each row/col, or 1 to n^2, or what.
Couldn't figure if array(n*n) meant array[n*n] or array[n][n], I took it to be the later.
If you are right...
int i = 1, diff = 0, failures=0;
while(i%26lt;n*n) {
diff = array[i] - array[i-1];
failures+=(diff%26lt;0 || diff %26gt;1) ?:1:0;
++i;
}
id cards
int highest_probed_num = 0;
for ( int i = 0; i %26lt; array.length-1; i++){
if ( (array[i] != array[i+1]) || (array[i] != array[i+1]+1)){
return false;
}
if ( (array[i] == n)){ break;} //this is incase there are numbers higher than N AND you only care about numbers between 1 and N.
}
return true;
PS. SW, I think "array(n*n)" means that his array is n^2 elements in it instead of a double array.
How in c++ i can check if array(n*n) have numbers from 1 to n?
when you say have numbers from 1 to n, do you mean between 1 and n?
for(int row = 0; row %26lt; n; ++n) {
for(int col = 0; col %26lt;n; ++col) {
if (array(row,col) %26lt; 1 ) || (array(row,col) %26gt; n )) {
++failure;
}
!UPDATE
Kat: Could be n*n. Can't figure if he wants to number from 1 to n in each row/col, or 1 to n^2, or what.
Couldn't figure if array(n*n) meant array[n*n] or array[n][n], I took it to be the later.
If you are right...
int i = 1, diff = 0, failures=0;
while(i%26lt;n*n) {
diff = array[i] - array[i-1];
failures+=(diff%26lt;0 || diff %26gt;1) ?:1:0;
++i;
}
id cards
Creating a variable array in c++?
The problem I have is that I want to create a 2D array that has a variable size. I see that I can not do the following:
int m, n;
double testarray[m][n];
as c++ will not let me declare variables for the size. How would I get around this? Perhaps you could write a little code for me to illistrate this?
Thank you,
Brian
Creating a variable array in c++?
There is several otion easiest one is using double pointer though many people are not comfortable with pointer.
Using double pointer,
double ** testarray;
testarray= new double* [m];
for(i=0;i%26lt;m;i++)
testarray[i]= new double [n];
now you can use this test array as before.
Reply:The Boost project (http://www.boost.org/) provides free peer-reviewed portable C++ source libraries. Many of these are candidates for inclusion in a future revision of the C++ language standard. In fact, the Boost project was started by members of the C++ standards committee's library subcommittee, to develop libraries that everyone agreed were needed in C++ but weren't ready at the time the C++ standard was released.
Boost libraries are reviewed, thoroughly tested, and documented. They are in wide use and are portable to nearly all platforms that run C++.
One of the Boost libraries is the Multidimensional Array Library
(Boost.MultiArray). From the docs:
"The Boost Multidimensional Array Library provides a class template for multidimensional arrays, as well as semantically equivalent adaptors for arrays of contiguous data. ... Boost MultiArray is a more efficient and convenient way to express N-dimensional arrays than existing alternatives (especially the std::vector%26lt;std::vector%26lt;...%26gt;%26gt; formulation of N-dimensional arrays). The arrays provided by the library may be accessed using the familiar syntax of native C++ arrays. Additional features, such as resizing, reshaping, and creating views are available..."
Reply:You're only permitted to use variable value for the left-most array dimension ... this is because indexes into the array for the other dimensions need to be constant to calculate where in the array to begin the offset.
But you can simply allocate space for a one-dimensional array of the size necessary for the two-dimensional array (i.e. double testarray[m * n]) and then access elements as :
testarray[(X * n) + Y)] ... where X and Y are the row/column or column/row of the element you are seeking.
This can be expanded to work for any number of dimensions.
int m, n;
double testarray[m][n];
as c++ will not let me declare variables for the size. How would I get around this? Perhaps you could write a little code for me to illistrate this?
Thank you,
Brian
Creating a variable array in c++?
There is several otion easiest one is using double pointer though many people are not comfortable with pointer.
Using double pointer,
double ** testarray;
testarray= new double* [m];
for(i=0;i%26lt;m;i++)
testarray[i]= new double [n];
now you can use this test array as before.
Reply:The Boost project (http://www.boost.org/) provides free peer-reviewed portable C++ source libraries. Many of these are candidates for inclusion in a future revision of the C++ language standard. In fact, the Boost project was started by members of the C++ standards committee's library subcommittee, to develop libraries that everyone agreed were needed in C++ but weren't ready at the time the C++ standard was released.
Boost libraries are reviewed, thoroughly tested, and documented. They are in wide use and are portable to nearly all platforms that run C++.
One of the Boost libraries is the Multidimensional Array Library
(Boost.MultiArray). From the docs:
"The Boost Multidimensional Array Library provides a class template for multidimensional arrays, as well as semantically equivalent adaptors for arrays of contiguous data. ... Boost MultiArray is a more efficient and convenient way to express N-dimensional arrays than existing alternatives (especially the std::vector%26lt;std::vector%26lt;...%26gt;%26gt; formulation of N-dimensional arrays). The arrays provided by the library may be accessed using the familiar syntax of native C++ arrays. Additional features, such as resizing, reshaping, and creating views are available..."
Reply:You're only permitted to use variable value for the left-most array dimension ... this is because indexes into the array for the other dimensions need to be constant to calculate where in the array to begin the offset.
But you can simply allocate space for a one-dimensional array of the size necessary for the two-dimensional array (i.e. double testarray[m * n]) and then access elements as :
testarray[(X * n) + Y)] ... where X and Y are the row/column or column/row of the element you are seeking.
This can be expanded to work for any number of dimensions.
How to delete all the elemnts in an array in c++?
need it now.. plzzz reply..........
How to delete all the elemnts in an array in c++?
Repeat to yourself as many times as it takes, that an array is a pointer..an array is a pointer..
Arkangyl presents a method to create/delete a dynamically created array - that may or may not be what you want to do. Your question isn't clear enough.
Say your array is defined and declared in your code:
Foo foo_array[10]
This array is static, and you cannot change its size at runtime.
If these aren't answering your question, you may not be asking in your question what you think you are.
Reply:Unless you've designed some new data type, an array IS a pointer. If you allocated an array of objects using 'new', you can deallocate them as follows:
Foo *foo_array;
foo_array = new Foo[10]; // the allocation
delete[] foo_array; // the deallocation
If you can give more details regarding the situation, I could give a better answer.
How to delete all the elemnts in an array in c++?
Repeat to yourself as many times as it takes, that an array is a pointer..an array is a pointer..
Arkangyl presents a method to create/delete a dynamically created array - that may or may not be what you want to do. Your question isn't clear enough.
Say your array is defined and declared in your code:
Foo foo_array[10]
This array is static, and you cannot change its size at runtime.
If these aren't answering your question, you may not be asking in your question what you think you are.
Reply:Unless you've designed some new data type, an array IS a pointer. If you allocated an array of objects using 'new', you can deallocate them as follows:
Foo *foo_array;
foo_array = new Foo[10]; // the allocation
delete[] foo_array; // the deallocation
If you can give more details regarding the situation, I could give a better answer.
Subscribe to:
Posts (Atom)