The add_after() function should complement the list after detecting the string a_word[] with the string a_word_after[] . Both lines are entered by the user after filling out the list.
The compiler points to the marked line and writes:

error: incompatible types in assignment of 'char *' to 'char [80]'

I understand that it is impossible to assign a pointer to a character to an array of characters? But is a one-dimensional array not a pointer to the zero element of this array? Please explain how to fix it?

 #include <iostream> #include <stdlib.h> #include <string.h> struct node { char word[80]; node* ptrToNextNode; }; void fillList(node *&list); void prntList(node *list); void makeList(node *&list); void add_after(node *list,char a_word[],char a_word_after[]); using namespace std; int main() { node* list; char a_word[80],a_word_after[80]; fillList(list); prntList(list); cout<<"Input a_word: "; cin>>a_word; cout<<"Input a_word_after: "; cin>>a_word_after; add_after(list,a_word,a_word_after); prntList(list); return 0; } void fillList(node*& list) { makeList(list); cout<<"Input word end press 'Enter'(input '.' for exit): "; cin>>list->word; if(!strcmp(".",list->word)) { delete list; list=NULL; } node *current, *last; current=list; while(current!=NULL) { last=new node; cout<<"Input word end press 'Enter'(input '.' for exit): "; cin>>last->word; if(!strcmp(".",last->word)) { delete last; last=NULL; } current->ptrToNextNode=last; current=last; } } void makeList(node *&list) { list=new node; if(list==NULL) { cout<<"Not enough memory! Terminate program..."; exit(1); } cout<<"List ready\n"; } void prntList(node *list) { if(list==NULL) { cout<<"This list is empty\n"; } else { while(list!=NULL) { cout<<list->word<<endl; list=list->ptrToNextNode; } cout<<"//////////End of list//////////"; } } void add_after(node *list,char a_word[],char a_word_after[]) { if(list==NULL) { cout<<"This list is empty"; } else { while(list!=NULL) { if(!strcmp(list->word,a_word)) { node* filling_pointer; makeList(filling_pointer); filling_pointer->word=a_word_after; //<---------------Ошибка в этой строке filling_pointer->ptrToNextNode=list->ptrToNextNode; list->ptrToNextNode=filling_pointer; } list=list->ptrToNextNode; } cout<<"//////////Pasting over//////////"; } } 
  • To format the code, I had to type 4 spaces before each new line. Is this the only way to format the code? - hal9000
  • @ hal9000, To format the code, select it with the mouse and click on the button 101010 of the editor. - angry
  • one
    select it with the mouse and click on the button 101010 of the editor. This holy phrase should be written side by side in the help. Thanks you! - hal9000

2 answers 2

The fact is that the name of the array, "created" (declared) in the function, is "fictitious" (m. More correctly virtual ) pointer. It is fictitious in the sense that its value cannot be changed (in fact, permanent memory is not allocated for it, the compiler constructs calls to the elements of the array, starting from a certain "base" (usually one of the CPU registers)). The language is designed so that you can access array elements using the pointer access syntax.

If an array is passed as a function parameter, then there is a real memory cell (usually on the stack) that contains the address of the first element of the array. Similarly for dynamic arrays that you allocate explicitly (for example, by calling malloc ()). In this case, the address of the beginning of the array is stored in a pointer type variable.

A small illustration:

  int a[4], *b; a[0] = 1; *(a+3) = 4; b = a+2; *b = 3; b[-1] = 2; printf ("%d %d %d %d\n",a[0],a[1],a[2],a[3]); 

Prints: 1 2 3 4

  • Thanks you. - hal9000 pm

The problem is that you have node :: word already pointing to an array of char.
You do not need to assign pointers, but copy from one array to another, for example: strcpy(filling_pointer->word, a_word_after)

Or diamically re-allocate memory for all lines.

  • The problem is that you have node :: word already pointing to an array of char. It turns out that node :: word is something like a constant (unchanged) pointer? - hal9000
  • because in the function main (), both of them also already point to an array char [80] (i.e. one should use copying from one array to another), and pointers to these arrays are copied to the function (in this case, char [] is equivalent to char *). Pointers can already be reassigned, calling a_word = a_word_after, but the result will not go anywhere beyond the limits of the function and physically the lines will remain unchanged - Baho
  • Thanks you. - hal9000