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?

  • one
    if the function arguments are pointers, then you cannot pass non-pointers. The names of the arrays have a pointer - AR Hovsepyan
  • @ARHovsepyan, I understand that, but why does an array without pointers change values ​​in main? - Watermelon Alexander
  • because you change the value of the elements in your function. For example, here: s1 [j] = s [i]; And in main you call this function by passing to it as arguments the pointers to the first element of the arrays, i.e. a and b - AR Hovsepyan

2 answers 2

char s[MAXLINE] reads this:

s is a pointer that contains the address of the first element of such an array, and that *(s + MAXLINE - 1) == '\0' You can also write a function like this:

  int copies(char s[], char s1[], int n, const unsigned size = 80) { тут вместо MAXLINE будет фигурировать `size` и функция будет иметь вид для общего пользования а не только для массивов с фиксированным размером, а также избавит вас от лишней головной боли с макросом ... } 

and this in turn is identical to the following expression:

 int copies(char* s, char* s1, int n, const unsigned size = 80) { ... } 

I hope everything is clear now ...

    In C and C ++, when using an array as a function argument, the type of the argument will be implicitly changed to a pointer. those. the signature of the first function actually coincides with the second:

     int copies(char * s, char * s1, int n) 

    Accordingly, the results are the same.