Assigning data to int array using pointers
-
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
-
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
How is iRecord declared and initialized?
The difficult we do right away... ...the impossible takes slightly longer.
-
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
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 whyOr better still just use the elements of the original array, since, presumably, it is a simple aray of integers.
-
How is iRecord declared and initialized?
The difficult we do right away... ...the impossible takes slightly longer.
-
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
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); }
-
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); }
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.
-
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.
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] = {}; ....
-
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
This is probably what you need:
#include
#include
#include
#includeint 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;
}