c - How to store result of type char in pointer? -
i want store result of type char in pointer i'm passing argument of function. this:
#include<stdio.h> void try(char *); int main() { char *result; try(result); printf("%s",result); return 0; } void try(char *result) { result="try this"; }
but i'm getting result : (null)
could tell me what's wrong here?
also way (no dynamic memory):
#include<stdio.h> void try(char *); int main() { char result[100] = {0}; try(result); printf("%s",result); return 0; } void try(char *result) { strcpy(result,"try this"); }
note: when got null, doesn't mean - had undefined behaviour there - because result
not initialized. guess invoked ub before trying print result
, namely when passed try
. because copy made in method of pointer, try read value of original pointer - reading uninitialized variables undefined in c. hence initialize variables in c.
Comments
Post a Comment