What is the output of the below code?
#include<stdio.h> void main() { int var; char *name = "TalentEve"; printf("%d", str_man(name)); } int str_man(char *name) { char *name_1 = name; while(*++name); return (name-name_1); }
-----------------------------------------------------------------
The above program assigned "TalentEve" to character pointer - name.
This is like assigning a string to character array, where name gives the starting address and to print each element in the array it can be accessed using name[0], name[1] and so on
name has been passed to the function str_man()
In the function str_man(), the name has been assigned to another pointer *name_1
*++name inside the while loop increments until it reaches the end of the string "TalentEve".
For example, *name gives numerical equivalent of T, *++name gives numerical equivalent of a and so on.
While loop exits only when end of the string is reached. This meant the address now in name is the end of the string. So, if we subtract name from name_1, we get the number of characters in a string and is returned.
This program is another way of calculating the length of the string.
0 Comments