C - Declaring a string by using a pointer

Introduction

Consider the following code.

Demo

#include <stdio.h> 

int main() /*from   w ww .j  a va2s  .c  o  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