it seems such silly thing ask don't know why happening. it's 5am , i'm still doing but..
it should print -ca why when compile it, printing
- ca?
instead of -ca, there isn't '\n' anywhere in sight. can guys think of logical explain it?
int main(int argc, char* argv[]){ int check = 0; char *thing = (char*)malloc(2 * sizeof(char)); strcpy(char, "ca"); code.. do{ more code... if(condition== 1) { more code.... if(check == 0) { printf("-"); check++; } if (some conditon != null){ printf("%s\n",thing); }while(condition) return 0;
}
you didn't allocate enough space string. every string has null terminator, 2-character string needs 3 bytes in array. strcpy()
writing outside bounds of thing
array when copies null byte, results in undefined behavior.
use
char *thing = malloc(3);
you can use strdup()
, makes copy of string in dynamic memory, automatically allocating enough space based on length of original string.
char *thing = strdup("ca");
Comments
Post a Comment