So I have a question about scoping and it's relation to pointer style C strings. Specifically, if you allocate a C string like:
char* test = "this is a C string";
since test is a char*, I'm guessing that this just sitting on the stack and that the string is also just a constant on the stack and that both of them will go out of scope when the function exits. I ran the following in XCode to test this as follows:
char* createCString();
int main (int argc, char * const argv[]) {
char* test;
test = createCString();
char* other_string = "this is some other string";
cout << test << endl;
return 0;
}
char* createCString(){
char* result = "this is a C style string.";
return result;
}
and somehow it does in fact output "this is a C style string." So I'm wondering what exactly the scope of this style of C string is?
Any help on this is appreciated b/c I'm kind of miffed. I tried reading the section on this in K&R but it didn't really help that much. Thanks for any help.
char* test = "this is a C string";
since test is a char*, I'm guessing that this just sitting on the stack and that the string is also just a constant on the stack and that both of them will go out of scope when the function exits. I ran the following in XCode to test this as follows:
char* createCString();
int main (int argc, char * const argv[]) {
char* test;
test = createCString();
char* other_string = "this is some other string";
cout << test << endl;
return 0;
}
char* createCString(){
char* result = "this is a C style string.";
return result;
}
and somehow it does in fact output "this is a C style string." So I'm wondering what exactly the scope of this style of C string is?
Any help on this is appreciated b/c I'm kind of miffed. I tried reading the section on this in K&R but it didn't really help that much. Thanks for any help.