While I was doing a online quiz (http://www.mycquiz.com/), I came to this question:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void myfunc(char** param){
++param;
}
int main(){
char* string = (char*)malloc(64);
strcpy(string, "hello_World");
myfunc(&string);
printf("%s\n", string);
// ignore memory leak for sake of quiz
return 0;
My first answer choice was that the string would get modified, whch didn't happen, and I am puzzled why not? Isn't he passing the address of the literal? Also a train of thought came to me that how would these situation vary?
void myfunc(char* param){
++param;
}
int main(){
char* string = (char*)malloc(64);
strcpy(string, "hello_World");
myfunc(string);
void myfunc(char* param){
++param;
}
int main(){
char string[12];
strcpy(string, "hello_World");
myfunc(string);
None of them seems to actually change the passed string. So if I do need to modify the string how do I pass it by reference?
Thanks.