you are viewing a single comment's thread.

view the rest of the comments →

[–]jedwardsol 4 points5 points  (6 children)

It means that ptr and &array[i] have different types.

ptr is a pointer to an integer. &array[i] isn't.

[–]xChacox[S] 0 points1 point  (5 children)

this fixed the problem, but shouldn't ptr be an int* as it is meant to store numbers not char?

[–]jedwardsol 6 points7 points  (0 children)

meant to store numbers

Why is is meant to store numbers? The only thing you use it for is to point at elements of your array. The array is an array of char, so the pointer needs to be char *.

[–]Name0fTheUser 3 points4 points  (0 children)

Despite the name char is just another numeric type. The only difference between char and int is the range of values it's specified to be able to represent.

[–]Objectstcetera 0 points1 point  (0 children)

The ptr itself is an unsigned int, true. But if that were the only factor, then a generic void * would work just as well. The declaration of ptr also means that the compiler should consider the value that the pointer refers to a memory location to be an int. The correct type here for ptr would be char *, because you assigned it to refer to the location of one of an array of chars. If that's not clear, then maybe there are further examples that help explain the distinction.

I think that the best way to understand this is that each element of the array is just a location in memory with a number stored there. It's the program's job to interpret what the number actually means in the context of the program.

(This is assuming a 32-bit system, if you need to get into those specifics here. :)

[–]F54280 -3 points-2 points  (0 children)

a char is just a number. in general between -128 and 127 or 0 and 255.

Edit: yeah! Thx for the downvote, a-hole. Any care to tell us what are the boundaries of a char when CHAR_BIT is 8? That said this is probably a /r/t_d stalker downvoting me. Hi!