I am new to programming. I am learning C as my first programming language. I found something strange to understand.
I have learnt that in C we can represent a String as a sequence of characters like this (using a char array):
char status[10] = "Married";
I have learnt that the problem of this approach is that we have to tell the size of the status
array during compilation.
But now I have learned we can use a char
pointer to denote an string
like -
char status[10] = "Married";
char *strPtr;
strPtr = status;
I don't understand it properly. My questions are -
How can I get char at index 4 (that is i in Married) using the
strPtr
?In
status
there is a null character (\0
) at the end of thestring
represented by thechar
array -M
-a
-r
-r
-i
-e
-d
-\0
. So by using the null character (\0
) we can understand the end of the string. When we usestrPtr
, how can we understand the end of thestring
?