1. Why is value of arr[1]
less than arr[0]
, and what are these values?
printf("%d\n",arr[0])
and printf("%d\n",arr[1])
will print the decimal values of the characters in those positions, since your character encoding is ASCII the decimal value of e
(arr[1]
) is 101 and the decimal value h
(arr[0]
) is 104, as you can check in the linked table.
2. Suppose we are given 0 to 1073741823 is the valid memory range and we have to check if array passed to func1 is in valid range then how to check that.
In this case, since it's a string, you know it will have a sentinel null byte at the end of the array, so you can use:
// initially arr will be pointing to the first element of the array
while(*arr != '\0'){ // will evaluate to false when the string ends
//do stuff...
arr++;
}
There are other ways of doing it, for example, pass the size of the array as second argument:
void func1(char *arr, int size){...}
and the call:
func1(a, sizeof a / sizeof *a);
This is more useful when your array is not a null terminated array, otherwise you can always use sentinels to signal the end of your arrays, the downside is that in most cases you'll need to add them yourself.
Note that the behavior of printf("%p\n", a)
is undefined, you should have printf("%p\n", (void*)a)
.