Hi L F, my nick is Nefisto and i'm not an american ppl, so if u can't understand my english, let me know and i'll try to explain in another way ;P
When u create an variable, or a vector in ur case, even if it is an const var, in rlly it is not more than a position in memory, so let's say the computer decide to give to you a memory space number 100 when u've created ur "alpha" const vector, so the letter 'A' will exist in position 100 in ur memory and because char cost 1 byte the letter B will be in position 101, like u can see in code below.
Code:
#include <stdio.h>
int main()
{
const char alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char* ptr = NULL;
int i;
for(i = 0; i < 26; i++)
printf("Memory POS: %p\tValue in this POS: %c\n", alpha + i, *(alpha + i));
return 0;
}
So if u have a char pointer called ptr, u can make it point to letter 'D' in some ways, lets try some of them:
Here we get the mem position of the const char, and made an implicity conversion for (char*) to remove const properties, after we use * to get the value that is storaged in this mem position
Code:
#include <stdio.h>
int main()
{
const char alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char* ptr = NULL;
int i;
for(i = 0; i < 26; i++)
{
printf("Memory POS: %p\tValue in this POS: %c\n", alpha + i, *(alpha+i));
if(*(alpha+i) == 'D')
ptr = (char*)(alpha+i);
}
printf("Memory POS for D: %p\tValue in this POS: %c\n", ptr, *ptr);
return 0;
}
here we get the first pos of const alpha vector, and go three position ahead
Code:
int main()
{
const char alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char* ptr = NULL;
ptr = (char*)alpha;
ptr += 3;
printf("Memory POS for D: %p\tValue in this POS: %c\n", ptr, *ptr);
return 0;
}
this another one will be usefull if u want to change the value of an const vector
Code:
int main()
{
const char alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char* ptr = NULL;
int i;
printf("Before:\n");
for(i = 0; i < 26; i++)
{
printf("Memory POS: %p\tValue in this POS: %c\n", alpha + i, *(alpha+i));
if(*(alpha+i) == 'D')
*(char*)(alpha+i) = 'U';
}
printf("After:\n");
for(i = 0; i < 26; i++)
printf("Memory POS: %p\tValue in this POS: %c\n", alpha + i, *(alpha+i));
return 0;
}
i hope i've helped good luck in ur studies ;P