Re initializing an array in C++ ?
-
I have a task of passing variable values in an array to a function. Of course compiler cannot reinitialize same array with different values. Putting the code in a block works. Awful hack. So what is "the proper " way to accomplish the task in C++ ? Multidimensional array is impractical in my case. Cheers Vaclav
{ int \*ipTestValue; int a\[32\] = {1,2,3}; ipTestValue =&a\[0\]; vna.utility.TestArray(ipTestValue); } { int \*ipTestValue; int a\[32\] = {1,2,3,4,5,6,7}; ipTestValue =&a\[0\]; vna.utility.TestArray(ipTestValue); }
What exactly are you wanting
TestArray()
to do?"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
What exactly are you wanting
TestArray()
to do?"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
It does nothing useful, just test prints the values passed to it. The question is about how to initialize / reinitialize THE ARRAY of the values passed.
Have you tried
memset()
ormemcpy()
? It's not ideal because (currently)TestArray()
does not know how much room it has to work with."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
I have a task of passing variable values in an array to a function. Of course compiler cannot reinitialize same array with different values. Putting the code in a block works. Awful hack. So what is "the proper " way to accomplish the task in C++ ? Multidimensional array is impractical in my case. Cheers Vaclav
{ int \*ipTestValue; int a\[32\] = {1,2,3}; ipTestValue =&a\[0\]; vna.utility.TestArray(ipTestValue); } { int \*ipTestValue; int a\[32\] = {1,2,3,4,5,6,7}; ipTestValue =&a\[0\]; vna.utility.TestArray(ipTestValue); }
-
I have a task of passing variable values in an array to a function. Of course compiler cannot reinitialize same array with different values. Putting the code in a block works. Awful hack. So what is "the proper " way to accomplish the task in C++ ? Multidimensional array is impractical in my case. Cheers Vaclav
{ int \*ipTestValue; int a\[32\] = {1,2,3}; ipTestValue =&a\[0\]; vna.utility.TestArray(ipTestValue); } { int \*ipTestValue; int a\[32\] = {1,2,3,4,5,6,7}; ipTestValue =&a\[0\]; vna.utility.TestArray(ipTestValue); }
Here is a partial test solution using memset. Compiler likes it. There is a small problem "This function copies the value of c (converted to an unsigned char) into each of the first size bytes of the object beginning at block. It returns the value of block. " I am getting only the first value passed and it fills all 32 bits Index 0 pDataArray 37373737 Index 1 pDataArray 37373737 Index 2 pDataArray 37373737 Index 3 pDataArray 37373737 Obviously I am using the pDataArray and memset (pointers ?) wrong, as usual. Could use some more help. Thanks Cheers Vaclav
int \*pDataArray; int a\[\] = { 55, 1, 2, 3 }; int iTestIndex = 0; memset(pDataArray, \*a, sizeof(a)); do { cout << "index " << dec << iTestIndex; // << endl; cout << " array 0x" << hex << +a\[iTestIndex\] << endl; } while (iTestIndex++ != sizeof(a)/4); // temp /4 iTestIndex = 0; do { cout << "index " << dec << iTestIndex;// << endl; cout << " pDataArray " << hex << +pDataArray\[iTestIndex\] << endl; //} while (pDataArray\[iTestIndex++\]); } while (iTestIndex++ != sizeof(a)/4); exit(1); // test stop int b\[32\] = { 4, 5, 6 }; memset(pDataArray, b\[32\], sizeof(b)); iTestIndex = 0; do { cout << "index " << dec << iTestIndex << endl; cout << " pDataArray " << hex << +pDataArray\[iTestIndex\] << endl; //} while (pDataArray\[iTestIndex++\]); } while (iTestIndex++ < 16);
-
Here is a partial test solution using memset. Compiler likes it. There is a small problem "This function copies the value of c (converted to an unsigned char) into each of the first size bytes of the object beginning at block. It returns the value of block. " I am getting only the first value passed and it fills all 32 bits Index 0 pDataArray 37373737 Index 1 pDataArray 37373737 Index 2 pDataArray 37373737 Index 3 pDataArray 37373737 Obviously I am using the pDataArray and memset (pointers ?) wrong, as usual. Could use some more help. Thanks Cheers Vaclav
int \*pDataArray; int a\[\] = { 55, 1, 2, 3 }; int iTestIndex = 0; memset(pDataArray, \*a, sizeof(a)); do { cout << "index " << dec << iTestIndex; // << endl; cout << " array 0x" << hex << +a\[iTestIndex\] << endl; } while (iTestIndex++ != sizeof(a)/4); // temp /4 iTestIndex = 0; do { cout << "index " << dec << iTestIndex;// << endl; cout << " pDataArray " << hex << +pDataArray\[iTestIndex\] << endl; //} while (pDataArray\[iTestIndex++\]); } while (iTestIndex++ != sizeof(a)/4); exit(1); // test stop int b\[32\] = { 4, 5, 6 }; memset(pDataArray, b\[32\], sizeof(b)); iTestIndex = 0; do { cout << "index " << dec << iTestIndex << endl; cout << " pDataArray " << hex << +pDataArray\[iTestIndex\] << endl; //} while (pDataArray\[iTestIndex++\]); } while (iTestIndex++ < 16);
Most of that code is not going to do what you think.
memset(pDataArray, *a, sizeof(a));
You are trying to fill the area pointed to by
pDataArray
, but you never initialise it to point to anything, so it will likely crash your application. Also you are trying to fillpDataArray
with the value stored in the first element ofa
. Is that what you actually mean?memset(pDataArray, b[32], sizeof(b));
You are trying to fill
pDataArray
with the value stored in a random element:b[32]
refers to a cell 1 beyond the end of that array. In both cases why are you using thememset
(presumably you actually meanmemcpy
), since you do not usepDataArray
for anything useful? -
Here is a partial test solution using memset. Compiler likes it. There is a small problem "This function copies the value of c (converted to an unsigned char) into each of the first size bytes of the object beginning at block. It returns the value of block. " I am getting only the first value passed and it fills all 32 bits Index 0 pDataArray 37373737 Index 1 pDataArray 37373737 Index 2 pDataArray 37373737 Index 3 pDataArray 37373737 Obviously I am using the pDataArray and memset (pointers ?) wrong, as usual. Could use some more help. Thanks Cheers Vaclav
int \*pDataArray; int a\[\] = { 55, 1, 2, 3 }; int iTestIndex = 0; memset(pDataArray, \*a, sizeof(a)); do { cout << "index " << dec << iTestIndex; // << endl; cout << " array 0x" << hex << +a\[iTestIndex\] << endl; } while (iTestIndex++ != sizeof(a)/4); // temp /4 iTestIndex = 0; do { cout << "index " << dec << iTestIndex;// << endl; cout << " pDataArray " << hex << +pDataArray\[iTestIndex\] << endl; //} while (pDataArray\[iTestIndex++\]); } while (iTestIndex++ != sizeof(a)/4); exit(1); // test stop int b\[32\] = { 4, 5, 6 }; memset(pDataArray, b\[32\], sizeof(b)); iTestIndex = 0; do { cout << "index " << dec << iTestIndex << endl; cout << " pDataArray " << hex << +pDataArray\[iTestIndex\] << endl; //} while (pDataArray\[iTestIndex++\]); } while (iTestIndex++ < 16);
Vaclav_ wrote:
int *pDataArray; int a[] = { 55, 1, 2, 3 }; int iTestIndex = 0; memset(pDataArray, *a, sizeof(a));
This code looks wrong. You didn't allocate any memory for the
Quote:
pDataArray
-
I have a task of passing variable values in an array to a function. Of course compiler cannot reinitialize same array with different values. Putting the code in a block works. Awful hack. So what is "the proper " way to accomplish the task in C++ ? Multidimensional array is impractical in my case. Cheers Vaclav
{ int \*ipTestValue; int a\[32\] = {1,2,3}; ipTestValue =&a\[0\]; vna.utility.TestArray(ipTestValue); } { int \*ipTestValue; int a\[32\] = {1,2,3,4,5,6,7}; ipTestValue =&a\[0\]; vna.utility.TestArray(ipTestValue); }
Here is the last modification of the code. It fulfills the task to be able to reinitialize the array, actually passing a pointer with new data to called function. The "test function" process / role was immaterial, as long as correct pointer with correct data was received. . All replies were generaly helpful, which I appreciate. Thanks. Cheers
int \*pDataArray\[32\]; int a\[\] = {55, 1, 2, 3}; int iTestIndex = 0; memcpy(pDataArray, a, sizeof(a)); do { cout << "index " << dec << iTestIndex; // << endl; cout << " array 0x" << hex << +a\[iTestIndex\] << endl; }while (iTestIndex++ != sizeof(a)/4); // temp /4 iTestIndex = 0; do { cout << "index " << dec << iTestIndex; // << endl; cout << " pDataArray a " << hex << +pDataArray\[iTestIndex\] << endl; //} while (pDataArray\[iTestIndex++\]); }while (iTestIndex++ != sizeof(a)/4); int b\[32\] = {4, 5, 6}; memcpy(pDataArray, b, sizeof(b)); iTestIndex = 0; do { cout << "index " << dec << iTestIndex << endl; cout << " pDataArray b " << hex << +pDataArray\[iTestIndex\] << endl; //} while (pDataArray\[iTestIndex++\]); }while (iTestIndex++ != sizeof(b)/4);
-
Here is the last modification of the code. It fulfills the task to be able to reinitialize the array, actually passing a pointer with new data to called function. The "test function" process / role was immaterial, as long as correct pointer with correct data was received. . All replies were generaly helpful, which I appreciate. Thanks. Cheers
int \*pDataArray\[32\]; int a\[\] = {55, 1, 2, 3}; int iTestIndex = 0; memcpy(pDataArray, a, sizeof(a)); do { cout << "index " << dec << iTestIndex; // << endl; cout << " array 0x" << hex << +a\[iTestIndex\] << endl; }while (iTestIndex++ != sizeof(a)/4); // temp /4 iTestIndex = 0; do { cout << "index " << dec << iTestIndex; // << endl; cout << " pDataArray a " << hex << +pDataArray\[iTestIndex\] << endl; //} while (pDataArray\[iTestIndex++\]); }while (iTestIndex++ != sizeof(a)/4); int b\[32\] = {4, 5, 6}; memcpy(pDataArray, b, sizeof(b)); iTestIndex = 0; do { cout << "index " << dec << iTestIndex << endl; cout << " pDataArray b " << hex << +pDataArray\[iTestIndex\] << endl; //} while (pDataArray\[iTestIndex++\]); }while (iTestIndex++ != sizeof(b)/4);
No, I am afraid it is still wrong. You have the following line of code:
int *pDataArray[32];
That creates
pDataArray
as an array of integer pointers, not an array of integers. And what is the purpose of this variable, since you never do anything useful with it? Maybe if you actually explained what problem you are trying to solve we could make some useful suggestions. Also in the expressionssizeof(a)/4
, it is better to usesizeof(a)/sizeof(a[0])
. Then if a is changed to some other type, the calculation will still be correct. -
It does nothing useful, just test prints the values passed to it. The question is about how to initialize / reinitialize THE ARRAY of the values passed.
This is really a serious issue for c++ beginners as I am finding it very tough during my initial stage of the developer. printer offline fix helped me to get the detail of this. Now i finally able to do initialization.