Wyszukiwanie imio w pliku tekstowym – błąd naruszenia ochrony pamięci

0

Witam, miałem za zadanie napisać program który miał za zadanie odczytać imiona z pliku, znaleźć imię, które w porządku leksykalnograficznym jest pierwsze, imię, które powinno być ostatnie, a potem posortować te listę. Na końcu powinien wypisać do innego pliku imię pierwsze, ostatnie oraz posortowaną listę. Niestety natknąłem się na problem i nie umiem go rozwiązać. Chodzi o błąd "Naruszenie ochrony pamięci (zrzut pamięci)". Wiem, że przy takim błędzie najczęściej winne są wskaźniki ale nie wiem na czym mógł polegać błąd, bo wydaje mi się, żebym wyszedł poza tablicę. Z góry dziękuje za pomoc. Oto kod:

#include <stdio.h>
#include <string.h>


void sortuj(char Imie[], int index){

    char temp[20];
    int i, j;
    

    for(i=0; i<index; i++){
        for(j=0; j<index-i-1; j++){
        
            if(strcmp(Imie[j], Imie[j+1]) > 0){
                //Zamiana miejscami
                strcpy(temp, Imie[j]);
                strcpy(Imie[j], Imie[j+1]);
                strcpy(Imie[j+1], temp);    
            }
        }        
    }
}

void minmax( char Imie[], int index, char *first[], char *last[]){
    *first = Imie[0];
    *last = Imie[0];
    int i;
    
    for(i=0; i<index; i++){
        if(strcmp(Imie[i], *first) > 0) *first = Imie[i];
    }
    for(i=0; i<index; i++){
        if(strcmp(*first, Imie[i]) > 0) *last = Imie[i];
    }
}


int main (int argc, char *argv[]){

    FILE *fin, *fout;
    
    fin = fopen(argv[1], "r");
    
    char imie[20];
    char Imie[20];
    int i,j,index;
    char first[20], last[20];
    
    index = 0;
    
    while( ((fscanf(fin, "%s", &imie))==1) && (index<20) ){
        
        strcpy(Imie[index], imie);
        index++;
    }
    
    fclose(fin);
    
    
    // MIN MAX
    minmax(Imie, index, &first, &last);
    
    
    //SORTOWANIE
    sortuj(&Imie, index);
    
    
    //DO PLIKU
    fout = fopen(argv[2], "w");
    
    for(j=0; j<index; j++){
    
        fprintf(fout, "%s \n", Imie[j]);
    }
    
    fprintf(fout, "Min: %s, Max: %s \n", first, last);
    fclose(fout);
    
    return 0;
}
1
while( ((fscanf(fin, "%s", &imie))==1) && (i<20) ){

ile w tym miejscu wynosi i? co sie stanie jezeli i bedzie mialo wartosc -204404?

co jezeli imie bedzie mialo wiecej niz 15 znakow?

0
fasadin napisał(a):
while( ((fscanf(fin, "%s", &imie))==1) && (i<20) ){

ile w tym miejscu wynosi i? co sie stanie jezeli i bedzie mialo wartosc -204404?

co jezeli imie bedzie mialo wiecej niz 15 znakow?

Faktycznie, mój błąd. Powinno być 'index<20' , już to poprawiłem ale niestety nadal wyskakuje naruszenie ochrony pamięci

0

Może okażą się pomocne ostrzeżenia jakie wyskakują w terminalu przy kompilacji, niestety nie wszystko rozumiem i nie do końca wiem jak to naprawić


sortim.c: In function ‘sortuj’:
sortim.c:14:14: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast [-Wint-conversion]
    if(strcmp(Imie[j], Imie[j+1]) > 0){
              ^~~~
In file included from sortim.c:2:0:
/usr/include/string.h:136:12: note: expected ‘const char *’ but argument is of type ‘char’
 extern int strcmp (const char *__s1, const char *__s2)
            ^~~~~~
sortim.c:14:23: warning: passing argument 2 of ‘strcmp’ makes pointer from integer without a cast [-Wint-conversion]
    if(strcmp(Imie[j], Imie[j+1]) > 0){
                       ^~~~
In file included from sortim.c:2:0:
/usr/include/string.h:136:12: note: expected ‘const char *’ but argument is of type ‘char’
 extern int strcmp (const char *__s1, const char *__s2)
            ^~~~~~
sortim.c:16:18: warning: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]
     strcpy(temp, Imie[j]);
                  ^~~~
In file included from sortim.c:2:0:
/usr/include/string.h:121:14: note: expected ‘const char * restrict’ but argument is of type ‘char’
 extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
              ^~~~~~
sortim.c:17:12: warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]
     strcpy(Imie[j], Imie[j+1]);
            ^~~~
In file included from sortim.c:2:0:
/usr/include/string.h:121:14: note: expected ‘char * restrict’ but argument is of type ‘char’
 extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
              ^~~~~~
sortim.c:17:21: warning: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]
     strcpy(Imie[j], Imie[j+1]);
                     ^~~~
In file included from sortim.c:2:0:
/usr/include/string.h:121:14: note: expected ‘const char * restrict’ but argument is of type ‘char’
 extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
              ^~~~~~
sortim.c:18:12: warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]
     strcpy(Imie[j+1], temp);
            ^~~~
In file included from sortim.c:2:0:
/usr/include/string.h:121:14: note: expected ‘char * restrict’ but argument is of type ‘char’
 extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
              ^~~~~~
sortim.c: In function ‘minmax’:
sortim.c:25:9: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
  *first = Imie[0];
         ^
sortim.c:26:8: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
  *last = Imie[0];
        ^
sortim.c:30:13: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast [-Wint-conversion]
   if(strcmp(Imie[i], *first) > 0) *first = Imie[i];
             ^~~~
In file included from sortim.c:2:0:
/usr/include/string.h:136:12: note: expected ‘const char *’ but argument is of type ‘char’
 extern int strcmp (const char *__s1, const char *__s2)
            ^~~~~~
sortim.c:30:42: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
   if(strcmp(Imie[i], *first) > 0) *first = Imie[i];
                                          ^
sortim.c:33:21: warning: passing argument 2 of ‘strcmp’ makes pointer from integer without a cast [-Wint-conversion]
   if(strcmp(*first, Imie[i]) > 0) *last = Imie[i];
                     ^~~~
In file included from sortim.c:2:0:
/usr/include/string.h:136:12: note: expected ‘const char *’ but argument is of type ‘char’
 extern int strcmp (const char *__s1, const char *__s2)
            ^~~~~~
sortim.c:33:41: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
   if(strcmp(*first, Imie[i]) > 0) *last = Imie[i];
                                         ^
sortim.c: In function ‘main’:
sortim.c:51:25: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘char (*)[20]’ [-Wformat=]
  while( ((fscanf(fin, "%s", &imie))==1) && (index<21) ){
                        ~^   ~~~~~
sortim.c:53:10: warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]
   strcpy(Imie[index], imie);
          ^~~~
In file included from sortim.c:2:0:
/usr/include/string.h:121:14: note: expected ‘char * restrict’ but argument is of type ‘char’
 extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
              ^~~~~~
sortim.c:61:22: warning: passing argument 3 of ‘minmax’ from incompatible pointer type [-Wincompatible-pointer-types]
  minmax(Imie, index, &first, &last);
                      ^
sortim.c:24:6: note: expected ‘char **’ but argument is of type ‘char (*)[20]’
 void minmax( char Imie[], int index, char *first[], char *last[]){
      ^~~~~~
sortim.c:61:30: warning: passing argument 4 of ‘minmax’ from incompatible pointer type [-Wincompatible-pointer-types]
  minmax(Imie, index, &first, &last);
                              ^
sortim.c:24:6: note: expected ‘char **’ but argument is of type ‘char (*)[20]’
 void minmax( char Imie[], int index, char *first[], char *last[]){
      ^~~~~~
sortim.c:65:9: warning: passing argument 1 of ‘sortuj’ from incompatible pointer type [-Wincompatible-pointer-types]
  sortuj(&Imie, index);
         ^
sortim.c:5:6: note: expected ‘char *’ but argument is of type ‘char (*)[20]’
 void sortuj(char Imie[], int index){
      ^~~~~~
sortim.c:73:19: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat=]
   fprintf(fout, "%s \n", Imie[j]);
                  ~^      ~~~~~~~
                  %d

0
Bartosz Krawiec napisał(a):

void sortuj(char Imie[], int index){

 char temp[20];
 int i, j;
               strcpy(temp, Imie[j]);

}

Dywagacje nad szczególami tego kodu są zbędne. WYJE od ostrzeżeń kompilatora, których kolega nie był łaskawy zauważyć.

1

Co ty chcesz sortować litery w imieniu? Na dodatek, próbujesz porównywać litery za pomocą strcmp.
Czym niby się różni się imie od Imie?

1
int loadNames(FILE *f, char names[][20], int maxNameCount)
{
    int i = 0;
    while (i < maxNameCount && 1 == fscanf(f, "%19s ", names[i])
         ++i;
    return i;
}


int loadNamesFileName(const char *fileName, char names[][20], int maxNameCount)
{
     int result = 0;
     FILE *f = fopen(fileName, "r");
     if (f) {
         result = loadNames(f, names, maxNameCount);
         fclose(f);
     }
     return result;
}

char names[100][20];
int namesCount = loadNamesFileName("imiona.txt", names, 100);

1 użytkowników online, w tym zalogowanych: 0, gości: 1