0

I managed to "solve this issue", but I don't know why I get the problem I get.

enter image description here

Right after the last line I also got a segmentation fault, which I failed to include in the above screenshot. My question is, is why don't I need to multiply i by sizeof(DATA_TYPE), so that I can traverse through the correct number of bytes.

Redoing this program without the sizeof operation, I get the results I want: enter image description here Sans segfault, as well.

Very similar code also works for traversing through a char array, no need to use the sizeof function. My question is why? Does the compiler know that since I'm manipulating a DATA_TYPE array that I need to traverse by Y number of bytes?

1

Yes you are correct. The compiler knows what data type you are using and traverses by the correct amount of bytes every time. You just tell it how many places away from zero you want (by the + i). You can take a look at the example shown here: https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/BitOp/pointer.html

1
  • Thanks! This bothered me for far longer than it should have – Tom Jul 17 '15 at 21:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .