Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Re initializing an array in C++ ?

Re initializing an array in C++ ?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++data-structurestoolslearning
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vaclav_
    wrote on last edited by
    #1

    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);
    }
    
    D L V 4 Replies Last reply
    0
    • V Vaclav_

      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);
      }
      
      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      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

      V 1 Reply Last reply
      0
      • D David Crow

        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

        V Offline
        V Offline
        Vaclav_
        wrote on last edited by
        #3

        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.

        D U 2 Replies Last reply
        0
        • V Vaclav_

          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.

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          Have you tried memset() or memcpy()? 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

          1 Reply Last reply
          0
          • V Vaclav_

            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);
            }
            
            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Why the extra pointer?

            int a[32] = {1,2,3};
            vna.utility.TestArray(a);
            for (i = 1; i <= 7; ++i)
            {
            a[i] = i;
            }
            vna.utility.TestArray(a);

            1 Reply Last reply
            0
            • V Vaclav_

              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);
              }
              
              V Offline
              V Offline
              Vaclav_
              wrote on last edited by
              #6

              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);
              
              L V 2 Replies Last reply
              0
              • V Vaclav_

                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);
                
                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                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 fill pDataArray with the value stored in the first element of a. 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 the memset (presumably you actually mean memcpy), since you do not use pDataArray for anything useful?

                1 Reply Last reply
                0
                • V Vaclav_

                  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);
                  
                  V Offline
                  V Offline
                  Victor Nijegorodov
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • V Vaclav_

                    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);
                    }
                    
                    V Offline
                    V Offline
                    Vaclav_
                    wrote on last edited by
                    #9

                    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);
                    
                    L 1 Reply Last reply
                    0
                    • V Vaclav_

                      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);
                      
                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      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 expressions sizeof(a)/4, it is better to use sizeof(a)/sizeof(a[0]). Then if a is changed to some other type, the calculation will still be correct.

                      1 Reply Last reply
                      0
                      • V Vaclav_

                        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.

                        U Offline
                        U Offline
                        User 14075349
                        wrote on last edited by
                        #11

                        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.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups