Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Why isn't call by reference needed for strcpy()

Tags:

c++

strcpy

I have a homework assignment with a number of questions. One is asking why the strcpy() function doesn't need the call by reference operator for CStrings. I've looked through the book numerous times and I can't, for the life of me, find the answer. Can anyone help explain this to me?

It is an array of sorts so I would think you would need the call by reference.

like image 305
Ribbs Avatar asked Jan 24 '26 02:01

Ribbs


1 Answers

strcpy() takes a pointer to char.

Thus you don't 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.

Passing a pointer is fast. On a 32 bits architecture, a pointer takes 32 bits, whatever the length of the pointed string.

like image 76
ereOn Avatar answered Jan 25 '26 17:01

ereOn