#include <stdio.h>
char global[] = "hello";
char *process_string(const char *str) {
str = global;
//str[0] = '1'; // Não pode mexer no conteúdo duma string constante
return str;
}
char *process_string_2(char * const str) {
//str = global; // Não pode atribuir a um ponteiro constante
str[0] = '2';
return str;
}
char *process_string_3(char *str) {
str = global;
str[0] = '3';
return str;
}
int main(void) {
char string[] = "world";
printf("%s %s\n", process_string
(string
), string
); printf("%s %s\n", process_string_2
(string
), string
); printf("%s %s\n", process_string_3
(string
), string
);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CgpjaGFyIGdsb2JhbFtdID0gImhlbGxvIjsKCmNoYXIgKnByb2Nlc3Nfc3RyaW5nKGNvbnN0IGNoYXIgKnN0cikgewoJc3RyID0gZ2xvYmFsOwoJLy9zdHJbMF0gPSAnMSc7ICAgIC8vIE7Do28gcG9kZSBtZXhlciBubyBjb250ZcO6ZG8gZHVtYSBzdHJpbmcgY29uc3RhbnRlCglyZXR1cm4gc3RyOwp9CgpjaGFyICpwcm9jZXNzX3N0cmluZ18yKGNoYXIgKiBjb25zdCBzdHIpIHsKCS8vc3RyID0gZ2xvYmFsOyAgICAvLyBOw6NvIHBvZGUgYXRyaWJ1aXIgYSB1bSBwb250ZWlybyBjb25zdGFudGUKCXN0clswXSA9ICcyJzsKCXJldHVybiBzdHI7Cn0KCmNoYXIgKnByb2Nlc3Nfc3RyaW5nXzMoY2hhciAqc3RyKSB7CglzdHIgPSBnbG9iYWw7ICAgIAoJc3RyWzBdID0gJzMnOwoJcmV0dXJuIHN0cjsKfQoKaW50IG1haW4odm9pZCkgewoJY2hhciBzdHJpbmdbXSA9ICJ3b3JsZCI7CgkKCXByaW50ZigiJXMgJXNcbiIsIHByb2Nlc3Nfc3RyaW5nKHN0cmluZyksIHN0cmluZyk7CglwcmludGYoIiVzICVzXG4iLCBwcm9jZXNzX3N0cmluZ18yKHN0cmluZyksIHN0cmluZyk7CglwcmludGYoIiVzICVzXG4iLCBwcm9jZXNzX3N0cmluZ18zKHN0cmluZyksIHN0cmluZyk7CgkKCXJldHVybiAwOwp9Cg==