#dereference increment

44 messages · Page 1 of 1 (latest)

junior notchBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

#

@teal raptor

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. You can use !solved to close a post and mark it as solved.

teal raptor
#
char a[2] = "0";
*a = '1';```

this sets a[0] to 1 ... how would i set the next index in this way
atomic steeple
#

btw " is string use ' for char

teal raptor
#

*a goes to the first index of the array

atomic steeple
#

yeah

teal raptor
#

can I set the next index by incrementing that somehow?

#

instead of the normal way

atomic steeple
#

yeah

#

you can increment it with sizeof(char)

atomic steeple
teal raptor
#

I see

#

*a + sizeof(char) = '4';

#

error: lvalue required as left operand of assignment

runic cape
#

dont delete questions

#

they may be interesting for other people

teal raptor
#

yeah I Read the automated message 🙂

#

not sure who marked this as solved

atomic steeple
# teal raptor *a + sizeof(char) = '4';

;compile

#include <cstdio>
int main(void) {
    int arr[10] = {0, 69};
    printf("%d %d\n", arr[1], *(arr + 1));
    *(arr + 1) = 50;
    printf("%d %d\n", arr[1], *(arr + 1));
}
modest geodeBOT
#
Program Output
69 69
50 50
atomic steeple
teal raptor
#

compiler does what thing?

#

*(a + sizeof(char)) = '4';

#

works

atomic steeple
atomic steeple
teal raptor
#

oh so you can just do + 1

#

ty

#

!solved

junior notchBOT
atomic steeple
modest geodeBOT
#
Program Output
0x7fff64ecde70 0x7fff64ecde74
atomic steeple
#

the compiler offset the address by sizeof(int)

#

but you don't have to

teal raptor
#

ok

#

got it

#

ty

atomic steeple
#

this works the same way but the compiler offsets the pointer only by 1

int arr[10] = {0, 69};
printf("%d\n", *((char*)arr + sizeof(*arr)));
teal raptor
atomic steeple
#

;compile

#include <cstdio>

int main() {
  int arr[10] = {0, 69};
  printf("%d\n", *((char*)arr + sizeof(*arr)));
  return 0;
}
modest geodeBOT
#
Program Output
69
atomic steeple
#

typecast dont cost any computational time