Array and String

We can consider String as an character array and it can be declared and initialized as follows:

char str[5] = "hello";

C supports an alternative to create a String using Pointer as follows:

char *str = "hello";
or
char *str;
str = "hello";

In above declaration str is a character pointer which stores the memory address of first letter of string hello.

We can also point character pointer to character array if we want to take array content at run time from user.

char str[50], *p;
p = &str[0];    or     p=str;

The below program will find the length of string using pointer.

Program Code:
#include<stdio.h>
#include<conio.h>

int slen(char *);
void main()
{
 char str[50],*p;
 int n;
 p=str;
 printf("Enter String :"); 
 gets(str);
 n=slen(p);
 printf("\nLength of String %s = %d\n",str,n);
}

int slen(char *s)
{
 int l=0;
 while(*s != '\0')
 {
 s++;
 l++;
 }
 return l;
}
Output:
Enter String :hello

Length of String hello = 5

Thanks for Visit..!