C Programming Tutorial
Computer Tutorial

Pointers and Strings



Pointers and Strings

Suppose we wish to store “Hello”. We may either store it in a string or we may ask the C compiler to store it at some location in memory and assign the address of the string in a char pointer. This is shown below:
char str[ ] = "Hello" ;
char *p = "Hello" ;
There is a subtle difference in usage of these two forms. For example, we cannot assign a string to another, whereas, we can assign a char pointer to another char pointer. This is shown in the following program.

Program to demonstrate pointer and string

\* C Program to demonstrate pointer and string *\
# include < stdio.h >
int   main( )
{

char   str1[ ] = "Hello"  ;
char   str2[10]  ;
char   *s = "Good Morning"  ;
char   *q  ;
str2 = str1 ;  \* It is not work *\
q = s ;   \* It is correct *\
return ( 0 ) ;

}