C - Declaring a string by using a pointer

Introduction

Consider the following code.

Demo

#include <stdio.h> 

int main() /* ww w . j a v  a2s .  co m*/
{ 
   char *sample = "this is a test?\n"; 

   while(putchar(*sample++)) 
       ; 
   return(0); 
}

Result

The string that's displayed is created by initializing a pointer.

Anytime you use a pointer to directly declare a string, the pointer variable can't be manipulated or else the string is lost.

Related Topic