V
V
Vladislav2015-01-03 12:24:19
C++ / C#
Vladislav, 2015-01-03 12:24:19

What do while(*from) and *to = " mean? Up to what point will the loop run?

The program was written using arrays, strings, and pointers.
The general format of the strcpy() function is:
strcpy(to, from);
The strcpy() function copies the contents of the from string to the to string.
Source:

#include <iostream>
#include <cstdio>
using namespace std;

void mystrcpy(char *to, char *from);

int main()
{
char str1[20], str2[20];
cout << «enter string: «;
gets(str1);

mystrcpy(str2, str1);

cout << «string copiet: » << str2;

cin.get();
return 0;
}

void mystrcpy(char *to, char *from)
{
while(*from)
{
*to = *from;
to++;
from++;
}
*to = »;
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Peter, 2015-01-03
@OnlyBooid

*from - getting the value at a specific address pointed to by the pointer.
0 = false for the while(...)
from++ condition - move to the next value. The important thing to remember here is that (address = address + sizeof(T)), where T is the value type, not a reference to it.

A
Alexander Ananiev, 2015-01-03
@SaNNy32

While *from!=0 character-by-character copying of a string

A
abcd0x00, 2015-01-03
@abcd0x00

The classic notation for this function is

void mystrcpy(char *to, char *from)
{
    while((*to++ = *from++))
        ;
}

The code
#include <iostream>
#include <cstdio>

using namespace std;

void mystrcpy(char *to, char *from);

int main()
{
    char str1[20], str2[20];
    cout << "enter string: ";
    gets(str1);

    mystrcpy(str2, str1);

    cout << "string copiet: " << str2;
    cin.get();
    return 0;
}

void mystrcpy(char *to, char *from)
{
    while((*to++ = *from++))
        ;
}

And the textbook in which you took it is better to throw it in the trash. Because everyone abandoned the use of gets() a long time ago, since it does not have a length limiter.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question