I want to get Code 2 result, but if I use Code 1 there is some strange result.
I think number 116 is t
's ASCII code. But I can't understand what happened in Code 1.
Can you explain what's different between Code 1 and Code 2?
Code 1
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int main(){
char t;
char *p = &t;
char *s;
int count=0;
char test[40] = "Test String partial copy in C\n";
s = strchr(test,'S');
while(!isblank(*s)){
*(p+count) = *s;
s++;
printf("%d\n",count);
count++;
}
*(p+count) = '\0';
printf("%s\n",p);
}
Result: 0 116 117 118 119 120 Sy
Code 2
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int main(){
char *p = (char*)malloc(sizeof(char));
char *s;
int count=0;
char test[40] = "Test String partial copy in C\n";
s = strchr(test,'S');
while(!isblank(*s)){
*(p+count) = *s;
s++;
printf("%d\n",count);
count++;
}
*(p+count) = '\0';
printf("%s\n",p);
}
Result: 0 1 2 3 4 5 String