Why isn't `src` changing when I change it from the function?

喜欢而已 提交于 2019-12-02 04:02:58

In your code, src="readers"; is wrong. You have to try like

strcpy(src, "readers");

The reason is, C uses pass by value while passing function parameters. So, src here is passed by value, so you cannot change src from inside the function. You can change *src from the function. So, to change the content pointed by src, you have to use strcpy().

Also, as Mr, @coolguy has mentioned, you're not using the return value of str(), so the return statement is making no impact here.

Inside main you declared array

char src[40]="hello";

When you pass the array to the function

str(src);

it is converted to a temporary object that is a pointer to the first element of the array. The parameter of the function is a local variable of the function that is destroyed after exiting the function.

You can imagine the function call and its definition the following way

str(src);

char* str( void )
{
    char * s = &src[0]
    s = "readers";

    return s;
}

That is at first this local variable (I renamed it like s that to distinguish it from the original array) assigned the address of the first element of the array and then it reassigned the address of the first element of the string literal. And address of this first element of the string literal is returned from the function. The local variable (that is the function parameter) is destroyed after exiting the function.

These operations do not influence on the original array itself. All the time you dealt with local variable s within the function.

If you want that the string literal would be copied in the original array you have to use standard C function strcpy declared in header <string.h>. For example

#include <string.h>

//...

char* str( char *s )
{
    strcpy( s, "readers" );

    return s;
}

In this case the original array will be changed because in function strcpy there is used pointer to its first element that is s So the string literal is copied in the area pointed to by the pointer that is in the array.

Take into account that arrays do not have the assignment operator. You may initialize an array the following way

char src[40] = "hello";

but you may not write after the array definition the following assignment statement

src = "hello";

You have to copy arrays element by element as for example for character arrays that contain strings by means of standard function strcpy

Lets have a look at the code, shall we?

str(src);

Function is called with src as parameter. The address of the first element of the array src is passed to the function str.

char* str( char *src)

In this function, src is a pointer that points to the address of the first element of src(the array) in main.

src="readers";

You change the location where src points to and makes it point to a string literal "readers". This doesn't effect the array src in main.

return src;

Returns the pointer src. In the code snippet you have shown, it does not have any effect.

What you actually want is

strcpy(src,"readers");

instead of

src="readers";

The former copies the string "readers" to src and its subsequent bytes and this changes the contents of the array src in main.
The latter makes the pointer src point to a new string literal "readers".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!