I created ptr as pointer to an array of 5 chars.
char (*ptr)[5];
assigned it the address of a char array.
char arr[5] = {'a','b','c','d','e'};
ptr = &arr;
using pointer ptr can I access the char values in this array?
printf("\nvalue:%c", *(ptr+0));
It does not print the value.
In my understanding ptr will contain the base address of array but it actually point to the memory required for the complete array (i.e 5 chars). Thus when ptr is incremented it moves ahead by sizeof(char)*5 bytes. So is it not possible to access values of the array using this pointer to array?
When you want to access an element, you have to first dereference your pointer, and then index the element you want (which is also dereferncing). i.e. you need to do:
printf("\nvalue:%c", (*ptr)[0]);, which is the same as*((*ptr)+0)Note that working with pointer to arrays are not very common in C. instead, one just use a pointer to the first element in an array, and either deal with the length as a separate element, or place a senitel value at the end of the array, so one can learn when the array ends, e.g.