Титла: Bubble sort на елементи на структура C program Публикувано от: iHshar в Mar 25, 2015, 10:46 Здравейте, исползвам метода на мехуечето за да сортирам имена по азбучен ред. Когато стринговете не са в структура се получава, но когато опитам да ги напраяв във структура със други данни ми дава куп грешки.
Ето сорса: #include <stdio.h> #include <string.h> struct students { char name[80]; int age[80]; } item[100]; int main() { char t[100]; int maxstud, j, e; int choice; printf("How many students you have?: "); scanf("%d", &maxstud); for(j=0; j<maxstud; j++){ printf("Enter Name: "); scanf("%s", item[j].name); printf("Enter Age "); scanf("%d", &item[j].age); } printf("Sorting: \n"); printf("1. Sort by name (alhabetical)\n"); printf("2. Sort by age\n"); printf("Enter your choicie: "); scanf("%d", &choice); if(choice==1){ for(j=1; j<maxstud-1; ++j) for(e=maxstud-1; e>=j; --e){ if(strcmp(item[e-1].name, item[e].name) >0){ strcpy(t, item[e-1]); strcpy(item[e-1], item[e]); strcpy(item[e], t); } } for(j=0; j<maxstud; j++) printf("Name: %s \t Age: ", item[j].name, item[j].age); } return 0; } Ето и грешките от компилатора: est.c: In function ‘main’: test.c:23:5: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int (*)[80]’ [-Wformat=] scanf("%d", &item[j].age); ^ test.c:36:11: error: incompatible type for argument 2 of ‘strcpy’ strcpy(t, item[e-1]); ^ In file included from test.c:2:0: /usr/include/string.h:129:14: note: expected ‘const char * __restrict__’ but argument is of type ‘struct students’ extern char *strcpy (char *__restrict __dest, const char *__restrict __src) ^ test.c:37:11: error: incompatible type for argument 1 of ‘strcpy’ strcpy(item[e-1], item[e]); ^ In file included from test.c:2:0: /usr/include/string.h:129:14: note: expected ‘char * __restrict__’ but argument is of type ‘struct students’ extern char *strcpy (char *__restrict __dest, const char *__restrict __src) ^ test.c:37:11: error: incompatible type for argument 2 of ‘strcpy’ strcpy(item[e-1], item[e]); ^ In file included from test.c:2:0: /usr/include/string.h:129:14: note: expected ‘const char * __restrict__’ but argument is of type ‘struct students’ extern char *strcpy (char *__restrict __dest, const char *__restrict __src) ^ test.c:38:11: error: incompatible type for argument 1 of ‘strcpy’ strcpy(item[e], t); ^ In file included from test.c:2:0: /usr/include/string.h:129:14: note: expected ‘char * __restrict__’ but argument is of type ‘struct students’ extern char *strcpy (char *__restrict __dest, const char *__restrict __src) Титла: Re: Bubble sort на елементи на структура C program Публикувано от: lastcyrol в Mar 25, 2015, 13:59 Започваш от първата грешка, оправяш я и продължаваш нататък.
В случая, имаш зададено за age да е масив от int, с брой елементи колкото са буквите на name. Може би първоначално си смятал да е string, после си сменил типа на int, но не си махнал [80]. Титла: Re: Bubble sort на елементи на структура C program Публикувано от: michael1960 в Mar 25, 2015, 18:41 Аз съм прост и дърт човек и явно не съм запознат с последните тендениции, и искам да попитам следното:
1.След като си декларирал в структурата масив int age[80], в кой от всичките елементи на този маси записваш стойност с това -> scanf("%d", &item[j].age); по мое време това ставаше така -> scanf("%d", &item.age[0]), примерно. 2.Според беглите ми познания по английски, това поле би трябвало да съдържа възраста на съответния студент, та бихте ли ми казали на колко години трябва да е човек, че стойността да е 80 цифри. Такава декларация би трябвало да е просто int age; 3.След като тук си се сетил да проверяваш стойностите на полето name -> strcmp(item[e-1].name, item[e].name), по каква логика, при положение че t e масив от 100 символа, допускаш че strcpy(t, item[e-1]); ще копира стойността на name в t. Би трябвало да е така -> strcpy(t, item[e-1].name); другите strcpy по - надолу трябва да е по същият начин. Михаил Петров град Смолян Титла: Re: Bubble sort на елементи на структура C program Публикувано от: remotexx в Mar 25, 2015, 19:37 Предполагам идеята е да се разместват целите записи (структури) е не само имената в тях
т.е. там дето се копира само името би трябвало да се копират и всички остнали полета на структурата едно по едно - в сл. имаме само едно допълнително поле 'age' Титла: Re: Bubble sort на елементи на структура C program Публикувано от: michael1960 в Mar 25, 2015, 19:59 Предполагам идеята е да се разместват целите записи (структури) е не само имената в тях Сигурно това е идеята, но човека питаше защо се сърди компилатора, а разместването не може да стане с strcpy, това е функция, която копира един стринг в друг. Трябва да стане така декларираме int age; тогава разместването ще изглежда така: age = item[e-1].age; strcpy(t, item[e-1].name); . . . . . Титла: Re: Bubble sort на елементи на структура C program Публикувано от: iHshar в Mar 26, 2015, 09:53 Благодаря за мненията. Ок, оправих първата грешка, беше от недовиждане.
Идеята ми е да разменя всички данни в структурата, а не само age. По мое мнение трябва да работи и не разбирам грешката: test1.c:36:11: error: incompatible type for argument 2 of ‘strcpy’ strcpy(t, item[e-1]); ^ In file included from test1.c:2:0: /usr/include/string.h:129:14: note: expected ‘const char * __restrict__’ but argument is of type ‘struct students’ extern char *strcpy (char *__restrict __dest, const char *__restrict __src) ^ test1.c:37:11: error: incompatible type for argument 1 of ‘strcpy’ strcpy(item[e-1], item[e]); ^ In file included from test1.c:2:0: /usr/include/string.h:129:14: note: expected ‘char * __restrict__’ but argument is of type ‘struct students’ extern char *strcpy (char *__restrict __dest, const char *__restrict __src) ^ test1.c:37:11: error: incompatible type for argument 2 of ‘strcpy’ strcpy(item[e-1], item[e]); ^ In file included from test1.c:2:0: /usr/include/string.h:129:14: note: expected ‘const char * __restrict__’ but argument is of type ‘struct students’ extern char *strcpy (char *__restrict __dest, const char *__restrict __src) ^ test1.c:38:11: error: incompatible type for argument 1 of ‘strcpy’ strcpy(item[e], t); ^ In file included from test1.c:2:0: /usr/include/string.h:129:14: note: expected ‘char * __restrict__’ but argument is of type ‘struct students’ extern char *strcpy (char *__restrict __dest, const char *__restrict __src) Титла: Re: Bubble sort на елементи на структура C program Публикувано от: michael1960 в Mar 26, 2015, 10:13 Абе човече божи,
est1.c:36:11: error: incompatible type for argument 2 of ‘strcpy’ strcpy(t, item[e-1]); нали ти писах в поста, че функцията strcpy копира един стринг в друг, а ти се опитваш да запишеш в стринга t, цялата структура в твоя случай трябва да е така: strcpy(t, item[e-1].name); и аз да съм на мястото на компилатора и аз ще се разсърдя |