0
#include <bits/stdc++.h>
using namespace std;

// Function to copy one string in to other
// using recursion
void myCopy(char str1[], char str2[], int index = 0)
{
    // copying each character from s1 to s2
    s2[index] = s1[index]; 

    // if string reach to end then stop 
    if (s1[index] == '\0')  
        return;

    // increase character index by one
    myCopy(s1, s2, index + 1); 
}

// Driver function
int main()
{
    char s1[100] = "GEEKSFORGEEKS";
    char s2[100] = "";
    myCopy(s1, s2);
    cout << s2;
    return 0;
}

I did not understand how the value of s2 is getting printed ....since we passed address of s1 and s2 to function mycopy(). mycopy() has two local array str1 and str2 as argument,so i was thinking two local array with values of s1 and s2 will be created.(call by values)

Shouldn't the function prototype be mycopy(char *s1,char *s2) for printing s2.(call by reference)

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
nitishy2j
  • 57
  • 1
  • 7
  • 4
    Possible duplicate of [C: differences between char pointer and array](https://stackoverflow.com/questions/1335786/c-differences-between-char-pointer-and-array) – eyllanesc Mar 16 '18 at 21:38
  • 3
    In a function prototype `char x[]` actually means `char *x` – M.M Mar 16 '18 at 21:39
  • 1
    The value of an array is the location in memory in which the array is stored (we say the array "decays" to this value). Otherwise, you wouldn't need `myCopy`, you could just do `s2 = s1;`. – David Schwartz Mar 16 '18 at 21:39
  • When you pass an array, what you are actually doing is sending a pointer to the first element. – Srini Mar 16 '18 at 21:40
  • 1
    @DavidSchwartz: The value of the array is the value of all elements. Yes, nearly all uses of an arrays name lead to array-decay, resulting in a pointer to the first element, but that's not the arrays value. – Deduplicator Mar 16 '18 at 21:42
  • `#include ` -- Don't do this. Please `#include` the proper headers. – PaulMcKenzie Mar 16 '18 at 22:04
  • Technically, `char x[]` is an array of characters and `char *x` is a pointer *to a single character*. The former is one or more characters. – Thomas Matthews Mar 16 '18 at 22:26
  • Your first line of myCopy confuses me: "s2[index] = s1[index]; ", where is s2 and s1? The myCopy parameters are str1 and str2. Did you compile this? – 2785528 Mar 16 '18 at 22:31
  • @Deduplicator That can't be right because C++ doesn't even always know how many elements an array has. If you use an array in a value context, it decays to a pointer to its first element. It's the process of using an array as a value that causes the decay -- it has no other value. – David Schwartz Mar 17 '18 at 20:24
  • @DavidSchwartz: What's your problem with being unable to determine somethings value when you only have partial information about it? That's a logical and unsurprising result. – Deduplicator Mar 17 '18 at 20:33
  • @Deduplicator If you couldn't determine its value, you couldn't pass it by value. But since you can pass it by value, it must be possible to determine its value since that's what you're passing. The value of something is whatever it is you pass when you pass it by value. – David Schwartz Mar 17 '18 at 21:46
  • @DavidSchwartz I thought you already accepted that you simply cannot pass a native array by value in C++. – Deduplicator Mar 18 '18 at 01:53

2 Answers2

0

void myCopy(char &str1[], char &str2[], int index = 0)

-1

To be able to reinitialize your parameter (array s2 in this case) you need to pass it by reference.

Like this:

void myCopy(char &str1[], char &str2[], int index = 0)

This means that myCopy() will use arrays as they are, otherwise it will create local duplication of them.

ivanjermakov
  • 1,131
  • 13
  • 24
  • 1
    The array parameter decays to a pointer; the local copy of the parameter is a copy of the pointer to the original array, not a copy of the array contents. – Harun Mar 17 '18 at 08:22