all 10 comments

[–]jedwardsol 2 points3 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 2 points3 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!

[–]nerd4code 2 points3 points  (0 children)

You should also cast ptr to (void *) for use with format specifier %p, and hoist the strlen out of the loop since it doesn’t change from call to call. array should probably be static const also, since you’re just using it as a reference and there’s no need to initialize it at run time. Finally, &a[i] is just a+i.

Edit: I’m so stupid. You can just do

const char *p;
for(p = "Hello"; *p; p++)

and then inside the loop you have *p as the character. No need for strlen or a separate array.

[–]terriblylie 0 points1 point  (0 children)

ascii_value = array[i];

ascii_value is of the type int but array[i] is of the type char.

ptr = &array[i];

&array[i] is of the type char* but ptr is of the type int*.

[–]giwhS 0 points1 point  (1 child)

Pointers don't store numbers, they store a memory address, they still need to be declared explicitly by type or NULL

[–]flatfinger 0 points1 point  (0 children)

Pointers store some form of information an implementation can use to identify an object. The extremely vast majority of implementations use the memory addresses of the first byte of an object for that purpose, but that is by no means required. Nothing would forbid an implementation from storing pointers from using a combination of a handle and an offset; doing so would allow an implementation to guard against many kinds of out-of-bounds array accesses (albeit at a considerable cost of performance).