Question: Here is my program : 

```
#include <cstring>
const int SIZE =10; 

int main() 
{ 
    char aName [SIZE]; // creates an array on the stack
    std::strcpy(aName, "Mary");
    return 0;
}

```

This program is obviously useless, I am just trying to understand the behavior of the strcpy function.

Here is it's signature : 
`char * strcpy ( char * destination, const char * source )`

so when I do :
`std::strcpy(aName, "Mary");`

I am passing by value the variable aName. I know that the aName (in the main) contains the address of the array. 
So is this assertion correct : strcpy creates a local variable called destination that has as value the address of the array aName that I have created on the stack in the main function?

I am asking this because it is very confusing to me. Whenever I have encountered addresses it usually was to point to a memory allocated on the **heap**...

Thanks!

Answer: 
> 
> So is this assertion correct : strcpy creates a local variable called destination that has as value the address of the array aName that I have created on the stack in the main function?
> 
> 
> 

Yes.

> 
> Whenever I have encountered addresses it usually was to point to a memory allocated on the heap...
> 
> 
> 

Yep, usually. But not always.

Pointers to non-dynamically-allocated things are fairly rare in C++, though in C it's more common as that's the only way to have "out arguments" (C does not have references).

`strcpy` is a function from C's standard library.
> 
> So is this assertion correct : strcpy creates a local variable called destination that has as value the address of the array aName that I have created on the stack in the main function?
> 
> 
> 

Yes. That is correct. Though I probably wouldn't call it a local variable. It is a parameter. Local variable usually means something like this:

```
int localVariable;

```

The word'parameter" is often associated with things like this:

```
int myFunction(int parameter) {
    // use parameter some where...
}

```

The point is roughly the same though: it creates a variable that will go out of scope once the function exits.

> 
> I am asking this because it is very confusing to me. Whenever I have encountered addresses it usually was to point to a memory allocated on the heap...
> 
> 
> 

Yes, this is the most common use case for them. But it isn't their only use. Pointers are addresses, and every variable has an address in memory regardless of whether it is allocated on the "heap" or "stack."

The use here probably because pointers to a `char` are commonly used to store strings, particularly on older compilers. That combined with the fact that [arrays "decay" into pointers,](https://stackoverflow.com/q/1461432/10957435) it is probably easier to work with pointers. It is also certainly more backwards compatible to do it this way.

The function could have just as easily used an array, like this:

```
char * strcpy ( char destination[], const char source[ )

```

But I'm going to assume it is easier to work with pointers here instead (**Note:** I don't think you can return an array in C++, so I'm still using `char *`. However, even if you could, I would imagine it is still easier to work with pointers anyway, so I don't think it makes a lot of difference here.).

---

Another common use of pointers is using them as a way to sort of "pass by reference":

```
void foo(int * myX) {
    *myX = 4;
}

int main() {
    int x = 0;

    foo(&x);

    std::cout << x; // prints "4"

    return 0;
}

```

However, in modern C++, actually passing by reference is preferred to this:

```
void foo(int & myX) {
    myX = 4;
}

int main() {
    int x = 0;

    foo(x);

    std::cout << x; // prints "4"

    return 0;
}

```

But I bring it up as another example to help drive the point home: memory allocated on the heap isn't the only use of pointers, merely the most common one (though actually dynamically allocated memory has been mostly replaced in modern C++ by things like [`std::vector`](https://en.cppreference.com/w/cpp/container/vector), but that is beside the point here).