Placing objects in C ++

char *myfunc() {
    char *temp = "string";
    return temp;
}

In this code fragment, where is the selection of the object pointed to temp, and what is its scope?

Is this function a valid way to return a char * pointer?

+5
source share
3 answers

Is the code correct?

Yes, your code is (almost) excellent, because it "string"is a string literal and is in static storage.

Note. A pointer is simply a variable in which the memory address is stored. This string simply stores the address of the string literal "string" inside a variable named temp.

char *temp = "string";

++ , , . , .


?

++ 03 () , :

" n const char" (3.7),

3.7.1 - 1:

, . .


:

char*, const char *. undefined, , .

. "string", , , .

+13

( ), "" - , "" ( ) - .

- ++ const char* char* .

+6

, ; .

++ , , const char. , , -.

+3

All Articles