There is an ex structure, it has data. It is necessary to copy the field to the structure ex2. The field is a string. Is it possible not to copy the string using strcpy(); , and rewrite the pointer?

 #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct q{ char pole[200]; }test1; typedef struct q2{ char pole[200]; }test2; int main(void) { test1 ex[1]; test2 ex2[1]; strcpy((*ex).pole, "qwer"); // заносим данные для примера ex2.pole = &ex.pole; // перезапись указателя (?) printf("%s",(*ex2).pole); return 0; } 
  • "... and rewrite the pointer." What pointer? You have not declared a single pointer in the code. - AnT
  • isn't the array name a pointer to its 0 element? - Hardc0re
  • Not. The name of the array in a number of specified contexts is implicitly cast to the type of pointer with the formation of a temporary value of the type of pointer. But the array is by no means a pointer. There is no pointer and there is nothing to rewrite. - AnT

3 answers 3

From what you are trying to do, you can assume that you are a victim of a popular misconception that the array in C is supposedly a pointer and that pointer can be somehow "overwritten".

The array in C is not a pointer. Therefore, there is no possibility to overwrite a certain "pointer" in this case.

    Now, if the structures were declared as

     typedef struct q{ char *pole; }test1; 

    Then yes, although - at the cost of certain difficulties (with memory allocation, release, etc.)

      In both structures, you have declared character arrays. Arrays do not have an assignment operation. You cannot assign a pointer to an array name. You can only copy the elements of one array to another.