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. Assigning data to int array using pointers

Assigning data to int array using pointers

Scheduled Pinned Locked Moved C / C++ / MFC
helpdatabasedata-structuresdebuggingcode-review
8 Posts 4 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 am trying to rebuild small data base to improve on my terrible knowledge of pointers. The array is pretty simple integers representing ASCII code , I do the conversion by casting the int or char to LCD print function for display. Works fine with one problem - one of the int in the array is a REAL number ( 0 thru 9) , not just ASCII representation of a number. For illustration - the ints are 32 65 73 7 73 The "problem " the index #3 does not read just 7 , but 773. And the array sizeof is also 3 , and it should be 4. Majority of the code is just for debug. I can change the real numbers to ASCII representation but would like to know why is this behaving this way. Appreciate your help. Vaclav

    Richard Andrew x64R L V A 4 Replies Last reply
    0
    • V Vaclav_

      I am trying to rebuild small data base to improve on my terrible knowledge of pointers. The array is pretty simple integers representing ASCII code , I do the conversion by casting the int or char to LCD print function for display. Works fine with one problem - one of the int in the array is a REAL number ( 0 thru 9) , not just ASCII representation of a number. For illustration - the ints are 32 65 73 7 73 The "problem " the index #3 does not read just 7 , but 773. And the array sizeof is also 3 , and it should be 4. Majority of the code is just for debug. I can change the real numbers to ASCII representation but would like to know why is this behaving this way. Appreciate your help. Vaclav

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      How is iRecord declared and initialized?

      The difficult we do right away... ...the impossible takes slightly longer.

      V 1 Reply Last reply
      0
      • V Vaclav_

        I am trying to rebuild small data base to improve on my terrible knowledge of pointers. The array is pretty simple integers representing ASCII code , I do the conversion by casting the int or char to LCD print function for display. Works fine with one problem - one of the int in the array is a REAL number ( 0 thru 9) , not just ASCII representation of a number. For illustration - the ints are 32 65 73 7 73 The "problem " the index #3 does not read just 7 , but 773. And the array sizeof is also 3 , and it should be 4. Majority of the code is just for debug. I can change the real numbers to ASCII representation but would like to know why is this behaving this way. Appreciate your help. Vaclav

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Why are you using double dereferencing? It would be much easier to code (and certainly to understand) by using arrays of integers rather than pointers. Something like:

        int pCarrier[sizeof(iRecordIndex)]; // although you did not tell us what iRecordIndex actually is
        for (i = 0; i < sizeof(iRecordIndex); i++)
        {
        pCarrier[i] = iRecord[i]; // move value to pCarrier. Not sure why

        Or better still just use the elements of the original array, since, presumably, it is a simple aray of integers.

        1 Reply Last reply
        0
        • Richard Andrew x64R Richard Andrew x64

          How is iRecord declared and initialized?

          The difficult we do right away... ...the impossible takes slightly longer.

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

          int iRecord[32] = {};

          1 Reply Last reply
          0
          • V Vaclav_

            I am trying to rebuild small data base to improve on my terrible knowledge of pointers. The array is pretty simple integers representing ASCII code , I do the conversion by casting the int or char to LCD print function for display. Works fine with one problem - one of the int in the array is a REAL number ( 0 thru 9) , not just ASCII representation of a number. For illustration - the ints are 32 65 73 7 73 The "problem " the index #3 does not read just 7 , but 773. And the array sizeof is also 3 , and it should be 4. Majority of the code is just for debug. I can change the real numbers to ASCII representation but would like to know why is this behaving this way. Appreciate your help. Vaclav

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

            I have modified the 3 position in the int array to ASCII representation and it worked. I do not understand why it does not work with int 0 thru 9 in 3rd position. I have included the database read and that is totally bogus when it fails. I still do not understand what the sizeof(iRecordIndex) is telling me. Modifying the iRecordIndex ( adding to it ) does not have any effects. The record has been initialized - this is first time I am using it and it contains 5 ints and rest of it is all zeroes. The iRecordIndex is 4 during this test. PS I had the end test in for loop wrong, but the 3rd index value was still printing wrong on LCD.

            //iRecord[iRecordIndex] //iRecordIndex += 3; int iField[32] = {}; // will need to reinitialize int *pCarrier[sizeof(iRecordIndex)]; // ponter to field for (i = 0; i != sizeof(iRecordIndex)+ 1 ; i++) { pCarrier[i] = &iRecord[i]; // assign data to pointer lcd_i2c.setCursor(0, 0); lcd_i2c.print("Index " ); lcd_i2c.print(i); //lcd_i2c.print(pCarrier ); lcd_i2c.setCursor(0, 1); lcd_i2c.print((char) iRecord[i] ); lcd_i2c.setCursor(0, 2); lcd_i2c.print(iRecord[i] ); lcd_i2c.print(" "); lcd_i2c.print(*pCarrier[i] ); delay(2*DELAY); } lcd_i2c.clear(); lcd_i2c.print("Database " ); lcd_i2c.setCursor(0, 1); for (i = 0; i != sizeof(iRecordIndex) + 1 ; i++) { lcd_i2c.print((int)*pCarrier[i] ); delay(DELAY); }

            Richard Andrew x64R 1 Reply Last reply
            0
            • V Vaclav_

              I have modified the 3 position in the int array to ASCII representation and it worked. I do not understand why it does not work with int 0 thru 9 in 3rd position. I have included the database read and that is totally bogus when it fails. I still do not understand what the sizeof(iRecordIndex) is telling me. Modifying the iRecordIndex ( adding to it ) does not have any effects. The record has been initialized - this is first time I am using it and it contains 5 ints and rest of it is all zeroes. The iRecordIndex is 4 during this test. PS I had the end test in for loop wrong, but the 3rd index value was still printing wrong on LCD.

              //iRecord[iRecordIndex] //iRecordIndex += 3; int iField[32] = {}; // will need to reinitialize int *pCarrier[sizeof(iRecordIndex)]; // ponter to field for (i = 0; i != sizeof(iRecordIndex)+ 1 ; i++) { pCarrier[i] = &iRecord[i]; // assign data to pointer lcd_i2c.setCursor(0, 0); lcd_i2c.print("Index " ); lcd_i2c.print(i); //lcd_i2c.print(pCarrier ); lcd_i2c.setCursor(0, 1); lcd_i2c.print((char) iRecord[i] ); lcd_i2c.setCursor(0, 2); lcd_i2c.print(iRecord[i] ); lcd_i2c.print(" "); lcd_i2c.print(*pCarrier[i] ); delay(2*DELAY); } lcd_i2c.clear(); lcd_i2c.print("Database " ); lcd_i2c.setCursor(0, 1); for (i = 0; i != sizeof(iRecordIndex) + 1 ; i++) { lcd_i2c.print((int)*pCarrier[i] ); delay(DELAY); }

              Richard Andrew x64R Offline
              Richard Andrew x64R Offline
              Richard Andrew x64
              wrote on last edited by
              #6

              Vaclav_Sal wrote:

              I still do not understand what the sizeof(iRecordIndex) is telling me.

              You have not told us how iRecordIndex is declared and initialized.

              The difficult we do right away... ...the impossible takes slightly longer.

              V 1 Reply Last reply
              0
              • Richard Andrew x64R Richard Andrew x64

                Vaclav_Sal wrote:

                I still do not understand what the sizeof(iRecordIndex) is telling me.

                You have not told us how iRecordIndex is declared and initialized.

                The difficult we do right away... ...the impossible takes slightly longer.

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

                Sorry. Both iRecord and iRecordIndex are declared as int and as class variables and initialized in the start of this loop. The iRecord is reset for next iDatabaseIndex, but I am not that far in checking the code. (The record needs to be split into several fields, again of variable length). I wanted to "do pointers" since the database fields length vary and like to save some memory in ARM processor. for (iDatabaseIndex = 0; iDatabaseIndex < MAX_DATABASE; iDatabaseIndex++) { iRecordIndex = 0; iRecord[32] = {}; ....

                1 Reply Last reply
                0
                • V Vaclav_

                  I am trying to rebuild small data base to improve on my terrible knowledge of pointers. The array is pretty simple integers representing ASCII code , I do the conversion by casting the int or char to LCD print function for display. Works fine with one problem - one of the int in the array is a REAL number ( 0 thru 9) , not just ASCII representation of a number. For illustration - the ints are 32 65 73 7 73 The "problem " the index #3 does not read just 7 , but 773. And the array sizeof is also 3 , and it should be 4. Majority of the code is just for debug. I can change the real numbers to ASCII representation but would like to know why is this behaving this way. Appreciate your help. Vaclav

                  A Offline
                  A Offline
                  Arthur V Ratz
                  wrote on last edited by
                  #8

                  This is probably what you need:

                  #include
                  #include
                  #include
                  #include

                  int main(void)
                  {
                  char str[256] = "32 65 73 7 73";

                  int \*pn = NULL, n = 0;
                  char\* \_tsn = strtok(str, " ");
                  while (\_tsn != NULL)
                  {
                  	int\* tmp = new int\[n + 1\];
                  	if (pn != NULL) memcpy((void\*)tmp, pn, sizeof(int) \* n);
                  
                  	tmp\[n++\] = atoi(\_tsn);
                  	if (tmp != NULL) pn = tmp;
                  
                  	\_tsn = strtok(NULL, " ");
                  }
                  
                  for (int i = 0; i < n; i++)
                  	printf("%d ", pn\[i\]);
                  
                  printf("\\n");
                  
                  \_getch();
                  
                  return 0;
                  

                  }

                  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