why do we have to add NULL to print a reserve string using pointer in C? -
this program in c print string in reverse using pointer. meanning of *pre='\0'?
#include <stdio.h> int main() { char string[50],reserve[50]; char *ps = string; char *pre = reserve; int i=-1; printf(" input string : "); scanf("%s",string); while(*ps) { ps++; i++; } while(i>=0) { ps--; *pre = *ps; pre++; --i; } *pre='\0'; printf(" reverse of string : %s\n\n",reserve); return 0; }
setting *pre='\0'; idiomatic way of adding nul-terminator end of char[] array. required c string type functions work: e.g. printf call %s specifier.
Comments
Post a Comment