C ++ Why not call by reference needed for strcpy ()

I have homework with a number of questions. One asks why the strcpy () function does not need to be called by the reference operator for CStrings. I have looked through this book many times, and I could not, for life, find the answer. Can anyone explain this to me?

This is an array, so I think you will need a call by reference.

+3
source share
7 answers

strcpy() accepts a pointer to char.

Thus, you do not pass the "string" as a parameter, but only the address of its first character.

So basically you have something like this:

void func(const char* str);

const char* str = "abc";
func(str); // Here str isn't passed by reference but since str is a pointer to the first character ('a' here), you don't need a reference.

Pointer transfer is fast. In a 32-bit architecture, a pointer accepts 32 bits, regardless of the length of the pointed line.

+3

class CString, , , :

?

CString sExample;
char buffer[LARGE_ENOUGH];

strcpy(buffer, sExample);

, class CString const char * strcpy.

, , .

+2

, .

"" ( " " ) , . , .

:

void f(int x)
{
    x = 42;
}

void g()
{
    int y = 54;
    f(y);
    // here "y" still has value 54
}

f() x, x, , y g(). , f() x, , y of g(). .

++ , :

void f(int& x)
{
    x = 42;
}

void g()
{
    int y = 54;
    f(y);
    // here "y" now has value 42
}

( "&" ) ++ , x, f(), y, g(). : x y . .

"C". C ( ++) char, 0 ( ). ; . - , . ; - ( 32 64 , ), , char . , strcpy(), ( , ). : ; .

, strcpy() , . strcpy() , . strcpy(). , .

, : , , , . (strcpy() , ). , RAM ( ) " ", , (strcpy()) . , "" :

  • ++: "reference" "&", .

  • : "" , , . , , , .

++ "reference" ( ) - " " ( ) .

+2

c- (char *), , . , , .

+1

strcpy char *, . , strcpy . - .

, char * , ++. , .

0

C , .

. Peter Van Der Linden Expert C programming, .

0

automatic type conversion is the answer that I think they are looking for. The look that appears may give you some help.

0
source

Source: https://habr.com/ru/post/1744934/


All Articles