Home > database >  string in function arguments in c explaination robert lafore
string in function arguments in c explaination robert lafore

Time:01-13

int main()
{
void fun(char *);
char str[] = "some text";
fun(str);
return 0;
}

void fun(char* ps){
while(*ps){
cout << *ps  ;}
cout << endl;

}

So this is one of the examples in a c text book by Robert Lafore which illustrates the example of string passed in a function. While explaining the example it is mentioned that the array address str is passed to the function. This address is constant, but since it is passed by value here. and a copy of it is created in fun().

I am not able to understand this statement as it is c style string which is a constant pointer which behaves similar to the array. The name itself should be the address and and we pass address to the function. My questions are: How it is being passed by value if the name itself is address? Why it is the copy being created when we are passing the pointer argument to the fun()?

I got really confused here. I might be wrong even in my basics, so an explanation would be great here.

CodePudding user response:

As you probably know, every parameter (variable) of function is stored on the stack (special memory dedicated to local variables) somewhere and acts as new variable.

So your function:

void fun(char* ps)
{
 ...
}

has variable 'ps' of type 'pointer to char' which exists within the scope of 'fun' function and nowhere else.

So when you use it from other function (such as your main), then you'll have two variables containg pointer to the "some text" string. Namely 'str' inside main and 'ps' inside 'fun' function. The 'ps' is that copy mentioned in the book.

Also you have to distinguish between term 'address' and 'variable'. Address is WHAT is stored and variable is WHERE it is stored.

So for example, this code:

char my_character = 'x';

char *a = &my_character;
char *b = &my_character;
char *c = &my_character;

are three DIFFERENT variables (the one having type char*) but they ALL contain same value - address of my_character variable.

CodePudding user response:

Let us take a look at what is happening in each of the statements in your program.

Statement 1

Here we consider the statement void fun(char *);.

This statement is a declaration for a function named fun that has one parameter of type char* and return type of void. This means that this function takes a pointer to char(char*) as argument by value and returns nothing(void).

Statement 2

Here we consider the statement char str[] = "some text";

This statement defines an array of char named str. That is, the type of str is char [10].

Statement 3

Here we consider the statement fun(str);

This statement calls the function named fun by passing str as an argument. There are 2 important things to note here:

  1. Since the argument is passed by value, str decays to a pointer to char due to type decay. This means, char [10] decays to char *. Remember from statement 1 that the function fun expects a char* as argument and this is exactly what we are supplying/providing it. In particular, we are providing it a pointer to the first character of the array which is nothing but char*.

  2. By value means the argument is copied. But note that in our case the argument is nothing but a char*(from point 1 above due to decay).This is why it is written that:

but since it is passed by value here. and a copy of it is created in fun().

  •  Tags:  
  • Related