#include <stdio.h>
#include <string.h>
// This will accomplish nothing
void set_char_pointer(char* val){
val = (char*)"Moidified string";
}
// This will sprintf over the char*
void sprintf_char_pointer(char* val){
// can only be as long as previous value
sprintf(val, "Modified");
}
// In this case, val doesn't even have to be initialized
void char_pointer_pointer(char** val) {
*val = "Modified";
}
#ifdef __cplusplus
// This trick will work in c++, but not in c
void char_pointer_ref(char*& val) {
val = "Modified";
}
#endif
int main()
{
char * orig = "Original";
set_char_pointer(orig);
printf("set_char_pointer(char* val) => \"%s\"\n", orig);
// char* orig1 = "Original"; // produces an error in this case
char orig1[9] = "Original";
sprintf_char_pointer(orig1);
printf("sprintf_char_pointer(char* val) => \"%s\"\n", orig1);
char * orig2 = NULL; // Doesn't have to be initialized
char_pointer_pointer(&orig2);
printf("char_pointer_pointer(char** val) => \"%s\"\n", orig2);
#ifdef __cplusplus
char * orig3 = NULL; // Doesn't have to be initialized
char_pointer_ref(orig3);
printf("char_pointer_ref(char*& val) => \"%s\"\n", orig3);
#endif
return 0;
}