The task: to write a program that copies one line to another n-times, writing two functions. The first is in which the array itself is transmitted, the second in which the address of the first element is transmitted.
Decision:
#include "stdafx.h" #include <iostream> #include <locale> using namespace std; #define MAXLINE 80 int copies(char s[MAXLINE], char s1[MAXLINE], int n) { int i, j; for (i = 0; s[i] != '\0'; i++); for (j = 0; s1[j] != '\0'; j++); if (n <= 0 || !((i+j-1) < MAXLINE)) { return 0; } j--; while (n != 0) { int stop = i; for (i = 0; i < stop;i++) { j++; s1[j] = s[i]; } n--; } j++; s1[j] = '\0'; cout << "Строка: " << s1 << endl; return 1; } int copies2(char *s, char *s1, int n) { cout << "Начало выполнения с помощью адресации" << endl; int i, j; for (i = 0; s[i] != '\0'; i++); for (j = 0; s1[j] != '\0'; j++); if (n <= 0 || !((i + j - 1) < MAXLINE)) { return 0; } //int first = j-1; j--; while (n != 0) { int stop = i; for (i = 0; i < stop; i++) { j++; s1[j] = s[i]; } n--; } j++; s1[j] = '\0'; //cout << "Строка: " << s1 << endl; cout << "Конец выполнения с помощью адресации" << endl; return 1; } int main() { setlocale(LC_ALL, "Russian"); //передача с помощью массива char a[MAXLINE]; char b[MAXLINE]; cout << "Введите строку, которая будет скопирована" << endl; gets_s(a); cout << "Введите строку, в которую будет скопировано" << endl; gets_s(b); int some; cout << "Введите количество раз, сколько строка будет скопирована" << endl; cin >> some; if (!(copies(a,b,some))) cout << "Допущена ошибка. Строку можно копировать 1 раз и более"; cout << "=====" << endl; cout << a << endl; cout << b << endl; cout << "=====" << endl; //адресация copies2(a, b, some); cout << a << endl; cout << b << endl; return 0; } Question: why is the function that is not being passed to pointers, but simply an array of elements changes the values in main? Also, do I pass the values correctly using the pointer to the first element?