I write this program for print content of a string in c language. but dont print string!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define GROW_BY 10
int main(){
char *ptr_s;
int c;
ptr_s= (char *) malloc(GROW_BY);
printf("please enter a string\n");
while((c=getchar() ) !='\n'){
*ptr_s++=c;
}
*ptr_s=0;
printf("the string is : \n %s \n", ptr_s);
}
result is :
the string is:
do not print content of string in ptr_s.
where is problem?
the problem is use of original value of ptr_s in increment
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define GROW_BY 10
int main(){
char *ptr_s, *copy_ptrs;
int c;
ptr_s= (char *) malloc(GROW_BY);
copy_ptrs=ptr_s;
printf("please enter a string\n");
while((c=getchar() ) !='\n'){
*copy_ptrs++=c;
}
*copy_ptrs=0;
printf("the string is : \n %s \n", ptr_s);
}