2

I just started learning c++, andI have problems with uderstanding working on adresses and rewriting variables from one adress to another. I have a program to correct:

using namespace std;

void cp(char* str2, char* str1) {

    cout << &(str1+1);

}

int main()
{
    char *str1 = "ppC";

    char str2[10] = "Witaj";

    // cout << str2 << endl;    // Witaj

    cp(str2,str1);
}

Problem: I have to write a function to rewrite text from str1 to str2.I also have to use specified amount of memory to store the text in str2.

But I've got stucked at first step: I would like to begin with taking the adress of str1 (its adress of str1[0] am i right?) then i would like to go in loop fro after adding +1 to each adress to go through all elements of str1 and write it to new char* var and return it.

aslg
  • 1,966
  • 2
  • 15
  • 20
jawjaw
  • 147
  • 1
  • 11
  • @vishal Nope, it's either `&str1[1]` or `str1 + 1`. – Rostislav Oct 25 '15 at 12:28
  • @vishal Why would you want to get the address of the paremeter pointer? If anything he'd want to write `*(str1+1)` to see the second element. @jawjaw Whatever it is you are reading or following to learn C++, don't. Thinking in terms of addresses is not the way to go. Check the FAQs recommendations http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – aslg Oct 25 '15 at 12:30
  • @vishal it shows some strangecharacters, when i'll do *(&str1) it shows me entire string instead of first character. Why? – jawjaw Oct 25 '15 at 12:33
  • @jawjaw `*(&str1)` is the same as writing `str1`. When you pass a `char*` to `cout` it will print all characters until it finds a `\0`. – aslg Oct 25 '15 at 12:35

1 Answers1

1

As I have understood you are going to write a function that copies a string stored in one character array in another character array.

Usuaaly such string functions also returns pointer to the first character of the destination array.

So instead of

void cp(char* str2, char* str1);

It is better to declare the function like

char * cp( char *str2, const char *str1 );

It is a low level function and t should not check whether there is enough space in the destination array. It is a problem of the caller.

So the function can be defined like

char * cp( char *str2, const char *str1 )
{
    char *p = str2;

    while ( *str2++ = *str1++ );

    return p;
}

Take into account that the second parameter has type const char *. In this case you can use constant arrays as an argument for this parameter including string literals.

Using the variable declarations in your program you could call the function the following way

std::cout << cp( str2, str1 ) << std::endl;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • thanks works fine but the idea was to do not change anything but body of the function. It has to be void.. :/ – jawjaw Oct 25 '15 at 13:21
  • @jawjaw No problem. Simply return nothing from the function.:) – Vlad from Moscow Oct 25 '15 at 13:27
  • yea everything works fine, thanks , what if i woudl like to write from the end to the beginning?(str1 = "ppC" str2 after using cp function str = "Cpp"? Is it possible to get what value is at the adress of last character and write it into adress of first. Thanks – jawjaw Oct 25 '15 at 14:08
  • @jawjaw In this case you need to write another function that uses the decrement operator with pointers. Something as reverse copy. Or you could write a general function that uses iterators and pass to it reverse iterators. – Vlad from Moscow Oct 25 '15 at 14:13