- Consider "Hello" String is stored in Character Array 'a'.
- "HELLO" is Stored in Character Array [ in Contiguous Memory Location.]
- It will take Following Form after Initialization.
What Happens ?char a[] = "HELLO";
- Here Array Size is not Specified .
- So C Assigns Individual Character to Each Array Location -
It will Looks Like This ...a[0] = 'H' a[1] = 'E' a[2] = 'L' a[3] = 'L' a[4] = 'O' a[5] = '\0'
How a[3] Works Here ?
- Compiler Finds a[3].
- Compiler Checks Whether a is Array or Pointer ?
- If 'a' is Array Variable then It starts at the location "a", goes three elements past it, and returns the character there
- So Access is Sequential.
How Char *a Works ?
- Consider "Hello" String is stored in Anonymous Character Array.
- It will take Following Form after Initialization.
char *a = "HELLO";
- 'a' is Pointer Variable.
- 'a' Stores Base Address of the Anonymous Array [Unknown Array]
- Whenever a[3] is to be accessed then In one shot Following Memory Location is Accessed -
Address = [Base Address of Anonymous Array] + [i]
- So In Short if a is a pointer, it starts at the location "a", gets the pointer value there, adds 3 to the pointer value, and gets the character pointed to by that value
Related Article :
0 Comments:
Post a Comment
Your Feedback :This is Growing Site and Your Feedback is important for Us to improve the Site performance & Quality of Content.Feel Free to contact and Please Provide Name & Contact Email