In general, everything seemed to be doing fine, but at the end, when issuing the answer, the error runs-time check failure # 2 - the stack around the variable 'nazvtov' was corrupted when I get out of line and how to fix it? ..

#include <iostream> #include <stdio.h> #include <stdlib.h> #include <malloc.h> #define limtov 1 // ограничиваю количество товаров #define limst 1 // ограничиваю количество стран, импортирующих товар struct svedeniya { char nazv; char exportstr; int volume; char *import; }; void vvod(svedeniya *tovar) { int i = 0; int j = 0; for (i = 0; i < limtov; i++){ tovar[i].import = (char*)malloc(sizeof(char)); printf("\nВведите название товара №%d: ", i); scanf("%s", &tovar[i].nazv); printf("\nВведите страну, экспортирующую товар№%d: ", i); scanf("%s", &tovar[i].exportstr); printf("\nВведите общий объем товара в штуках№%d: ", i); scanf("%d", &tovar[i].volume); for (j = 0; j < limst; j++) { printf("\nВведите страну №%d, в которые импортируется товар№%d: ", j, i); scanf("%s", &tovar[i].import[j]); } } } void poisk(svedeniya *tovar) { char nazvtov; int i = 0; printf("Введите товар для поиска: "); scanf("%s", &nazvtov); for (i = 0; i < 1; i++) { if (strcmp(&nazvtov, &tovar[i].nazv) == 0){ printf("%s ", &tovar[i].nazv); printf("%s ", &tovar[i].exportstr); for (int j = 0; j < 1; j++) { printf("%s ", &tovar[i].import[j]); } } } } int main() { setlocale(LC_ALL, "Russian"); svedeniya *tovar; tovar = (svedeniya*)malloc(sizeof(svedeniya)); vvod(tovar); poisk(tovar); } 

Closed due to the fact that off-topic participants 0xdb , LFC , Enikeyschik , Anatol , aleksandr barakin 23 Feb at 19:23 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - 0xdb, LFC, Enikeyschik, Anatol
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • In general, I allocated memory for each line and now I have the error "Violation of access when reading" - Sergey Kolomiets

1 answer 1

You nazvtov - this is one and only symbol. Only one empty string can be pushed into one character. You are obviously trying to push a non-empty string into it. Of course turns out to go beyond.

You have in all the code for storing the string for some reason declared a field of type char . With the exception of some strange import field, which suddenly has type char * , but for which the one and only char is still allocated. How are you going to store lines in such fields the size of one character ???

  • Thank you very much. I understood in which direction to move. I tried to make each line a dynamic array, I cannot solve the problem - Sergey Kolomiets