What's up with incrementing character Arrays?

Hello,

Would someone please explain how the following three bolded statements function?

typedef struct {
ulong dataBucket1[5];
ulong dataBucket2[10];
int Endianness;
} Buckets;

void BlahBlah(Buckets dataStructure)
{

dataBucket[0] = unsigned long value...;
dataBucket[1] = unsigned long value...;
dataBucket[2] = unsigned long value...;
dataBucket[3] = unsigned long value...;
dataBucket[4] = unsigned long value...;

Pointer to not a single character, but a string of characters/character array?
char* charPtr;

Without the count, points to first array element - with count, array element
three?
charPtr = (char *) dataStructure->dataBucket1 + count; //count = 3


Next array element set to a null termination character?. (0x80))
*charPtr++ = 0x80;

//....

}
Last edited on
closed account (zb0S216C)
1) Declares an uninitialised, non-constant pointer to a non-constant char

2) databucket1 of dataStructure is converted to a non-constant pointer to a non-constant char. charPtr is set to point to the address returned from the cast. After conversion, count is added to dataBucket1.

3) charPtr is dereferenced. The char pointed to by charPtr is assigned to character equivalent of 0x80. The char pointed to by charPtr is incremented by 1.

Wazzak
Last edited on
Wazzak,

Thank you for your time and effort, and shared expertise - they are much appreciated.

Regarding #2

"After conversion, count is added to dataBucket1"

It is added to an existent ulong value within what array index?
I was thinking charPtr = (char *) dataStructure->dataBucket1 pointed to the
first array element... dataBucket1[0]... and then the + <A Number> would
not indicate a different array index?

Regarding #3

how could 1415931988 (*charPtr) + 10 (real value, not 3 as in above) == a single char '@'? (the actual value returned by the operation; I.E. *charPtr)

THEN, After *charPtr++ *charPtr equals '\0' ?

Still confused...? (I will now reread your original post, to make certain that I understood what you wrote...)

Thank you or anyone else responding to this query - i am not usually flummoxed, but this one has my mind going in different directions...
Last edited on
closed account (zb0S216C)
userid wrote:
"It is added to an existent ulong value within what array index?"

No. It moves dataBucket1 up count addresses and returns the address of the resulting address. The addition is not permanent.

userid wrote:
"I was thinking charPtr = (char *) dataStructure->dataBucket1 pointed to the
first array element... dataBucket1[0]..."

It does. Though, casting to a char* would result in a significant loss of data from ~4,294,967,295 to 255 (126 if the receiving char is signed) when dereferenced.

userid wrote:
"how could 1415931988 (*charPtr) + 10 (real value, not 3 as in above) == a single char '@'?"

I'm not sure. It could possibly be undefined behaviour.

userid wrote:
"THEN, After *charPtr++ *charPtr equals '\0' ?"

When charPtr was incremented, the new address it pointed to could've contained zero. Though, I haven't seen the full implementation, so it's a pure guess.

Wazzak
Last edited on
Regarding @ symbol, dataBucket1[10] equals 0. (dataBucket1 character pointer + 10.)

I will have to think about this a little more, but I think that you have given me the knowledge that i needed.

Thank you! :-)
Topic archived. No new replies allowed.