Checking array for string

This is a discussion on Checking array for string within the C Programming forums, part of the General Programming Boards category; I'll try to post the relevant parts of my bigger code. Bytheway, will this enormous, off-topic, code from shohebmuddhoo be ...

  1. #46
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    I'll try to post the relevant parts of my bigger code.

    Bytheway, will this enormous, off-topic, code from shohebmuddhoo be removed? I'm sure it's against some sort of rule...
    Nothing to see here, move along...

  2. #47
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    Hijacking is frowned upon, though not really against the rules of many forums.
    Though I am for splitting it into a new thread (a typical action for hijacking).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #48
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    Should I make a new thread for my code as well, or can I place it in this one. It is still on-topic, since it is quite usefull to understand how to combine this checking function with different kinds of arrays.

    Plus it is an application of the function in question.
    Nothing to see here, move along...

  4. #49
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    You can put it here. That's the best, me thinks.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #50
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    I've tried to shorten it by leaving out the main() part, which doesn't matter yet and by leaving out the code of the linecounting function.
    It's still a bit long though, but I don't know what you need to understand the error.

    The errors given after compiling are about the function Analyse3():
    - 199 assignment of read-only location

    That's the first error, there are a whole lot more, but I think it's best to start with this one, since it has to do with this topic.

    Code:
    /*  Description: Uses a list of mergersystems and devides them into 
                     seperate textfiles for each type of system.
                     The second line holds the required data for putting the
                     system into the correct file.
                     Format lists:
                          
                          line1: Initial binary system
                          line2: Binary system just before final merger
                          line3: Merger product
                          
                          Collum01: ID#
                          Collum02: Time [Myr]
                          Collum03: Distance [R_O]
                          Collum04: Eccentricity
                          Collum05: Type of star1
                          Collum06: Mass of star1 [M_O]
                          Collum07: Irrelivant
                          Collum08: Type of star2
                          Collum09: Mass of star2 [M_O]
                          Collum10: Irrelivant
    */
    
    
    //**********************************************
    // Types of Stars:
    //**********************************************
    
    
    const char *Startypes[] = 
    {    "Planet",
         "Brown Dwarf",
         "Main Sequence",
         "Wolf Rayet",
         "Herzsprung Gap",
         "Sub Giant",
         "Horizontal Branch",
         "Super Giant",
         "Carbon Star",
         "Helium Star",
         "Helium Giant",
         "Helium Dwarf",
         "Carbon Dwarf",
         "Oxygen Dwarf",
         "Thorn Zytkov",
         "Xray Pulsar",
         "Radio Pulsar",
         "Neutron Star",
         "Black Hole"
    };
    
    
    //**********************************************
    // Function for counting lines in a file:
    //**********************************************
    
    int Linecount(char* filename_array[],int n){
         //this one does work already
         }
    
    //**********************************************
    // Function for checking array for certain string:
    //**********************************************
    
    int stringcheck(const char* string1_to_check_for,const char* string2_to_check_for,const char array_to_check[][8],int length_of_array){
    
    int i=0;
    int result=0;
    
    for(i = 0; i < length_of_array; i++)
    {
        //printf("checking... %d\n",i);  
        if (strcmp(string1_to_check_for, array_to_check[i]) == 0 ||
            strcmp(string2_to_check_for, array_to_check[i]) == 0)
        {
            //printf("found it at %d\n",i); 
            return 1;
        }
    }   
    
    return 0;
    } 
    
    
    //**********************************************
    // Function for sorting the systems:
    // It also counts the number of systems added.
    //**********************************************
    
    
    void Analyse3(FILE* filetoread, const char* type_array[]){
         
                int startype1 = 0;
                int startype2 = 0;
                int id    = 0;
                int max_comb = 500;
                int comb_array_pointer = 0;
                char current_comb1[8];
                char current_comb2[8];
                
                char Binarysystem_filename[1000];
                
         
                char* line1     = (char*) malloc (1000);
                char* line2     = (char*) malloc (1000);       
                char* line3     = (char*) malloc (1000);
                //char* comb_array= (char*) malloc (1000);
                const char comb_array[max_comb][8];
                
                 
                
                printf("type01 = %s\n",type_array[0]);
                printf("type02 = %s\n",type_array[1]);
                printf("type03 = %s\n",type_array[2]);
                printf("type04 = %s\n",type_array[3]);
                printf("type05 = %s\n",type_array[4]);
                printf("type06 = %s\n",type_array[5]);
                printf("type07 = %s\n",type_array[6]);
                printf("type08 = %s\n",type_array[7]);
                printf("type09 = %s\n",type_array[8]);
                printf("type10 = %s\n",type_array[9]);
                printf("type11 = %s\n",type_array[10]);
                printf("type12 = %s\n",type_array[11]);
                printf("type13 = %s\n",type_array[12]);
                printf("type14 = %s\n",type_array[13]);
                printf("type15 = %s\n",type_array[14]);
                printf("type16 = %s\n",type_array[15]);
                printf("type17 = %s\n",type_array[16]);
                printf("type18 = %s\n",type_array[17]);
                printf("type19 = %s\n\n",type_array[18]);
    
                               
    
         //read the three lines and remember them:
            fgets(line1, 1000, filetoread);
            fgets(line2, 1000, filetoread);
            fgets(line3, 1000, filetoread);
         //divide the second line into three integers, startype1, startype2 and id:
            sscanf(line2,"%d %*s %*s %*s %d %*s %*s %d %*s %*s",&id,&startype1,&startype2);
    
            
    
    //Now look at the combination of the types and make a new file to write them into
    //unless I already made that file.
             
             
    sprintf(current_comb1, "%d%d",startype1,startype2);
    sprintf(current_comb2, "%d%d",startype2,startype1);
    int i=0;
    int result=0;
    
    result = stringcheck(current_comb1,current_comb2,comb_array,comb_array_pointer);
    
             if(result==0){
    
             sprintf(Binarysystem_filename, "%s - %s.txt",type_array[startype1-1],type_array[startype2-1]);
             
             FILE* file = fopen(Binarysystem_filename,"w");
    
             comb_array[comb_array_pointer] = current_comb1;
             comb_array_pointer++;
    
             fprintf(file,"%s",line1); 
             fprintf(file,"%s",line2);
             fprintf(file,"%s",line3);
                   
             fclose(file);
             
             
    
             }else{ //!!!!!!!!!!!!!!!!!Must not overwrite text already in file! 
                    //I don't think this'll work just yet.
                   
                   sprintf(Binarysystem_filename, "%s - %s.txt",type_array[startype1-1],type_array[startype2-1]);
                   
                   FILE* file = fopen(Binarysystem_filename,"w");
                   
                   fprintf(file,"%s",line1); 
                   fprintf(file,"%s",line2);
                   fprintf(file,"%s",line3);
                   
                   fclose(file);
                   }          
             
             }  
             
             sprintf(Binarysystem_filename, "%s - %s.txt",type_array[startype1-1],type_array[startype2-1]);
    
                free (line1);
                free (line2);
                free (line3);
                
                
    }
    Nothing to see here, move along...

  6. #51
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    Code:
    		comb_array[comb_array_pointer] = current_comb1;
    Problem here. You cannot assign arrays in C. Either use a loop or memcpy.

    Code:
    	const char comb_array[max_comb][8];
    Since you are assigning to it, it must be char, not const char.

    Other than that, you have one } too much:
    Code:
             }else{ //!!!!!!!!!!!!!!!!!Must not overwrite text already in file! 
                    //I don't think this'll work just yet.
                   
                   sprintf(Binarysystem_filename, "%s - %s.txt",type_array[startype1-1],type_array[startype2-1]);
                   
                   FILE* file = fopen(Binarysystem_filename,"w");
                   
                   fprintf(file,"%s",line1); 
                   fprintf(file,"%s",line2);
                   fprintf(file,"%s",line3);
                   
                   fclose(file);
                   }          
             
             }
    You MUST work on your indentation! This is a problem you get because you DON'T have proper indentation!

    Other than that, the examples compiles.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #52
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    Do you know of a nice little tutorial on indentations? I do want to tidy up my program, but I don't know the standards. I could google it ofcourse, but if you know of a good one, I would like to see it.

    And that const char was one of those problems I keep running into. If I change const char to char I get the error of incompatible pointer types. I'm just trading one error for another.
    Nothing to see here, move along...

  8. #53
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    Quote Originally Posted by Ayreon View Post
    Do you know of a nice little tutorial on indentations? I do want to tidy up my program, but I don't know the standards. I could google it ofcourse, but if you know of a good one, I would like to see it.
    Check the http://apps.sourceforge.net/mediawik...le=Indentation, or have a look at Wikipedia. Consistency is the key.

    And that const char was one of those problems I keep running into. If I change const char to char I get the error of incompatible pointer types. I'm just trading one error for another.
    I get no errors with the code I corrected (and it is as it should be), so post the relevant code where you get your other problems.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #54
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    Then let's first fix all the points from your post.
    First I changed "const char comb_array[max_comb][8];" to "char comb_array[max_comb][8];"
    Secondly I removed the }.

    But now for the difficult part. How can I implement memcpy, I read the defenition of the function, but I'm not sure how to use that in my case...
    Little help there?
    Nothing to see here, move along...

  10. #55
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    I've tried this: It doesn't give any compile errors, but that doesn't always mean it's ok:
    Code:
    memcpy(comb_array[comb_array_pointer], current_comb1, 8);
    However I do still get:
    179 [Warning] passing arg 3 of `stringcheck' from incompatible pointer type

    I didn't change anything in the code I posted exept for those things you pointed out.

    This is what I have:
    Code:
    char comb_array[max_comb][8];
    And this is the function's argument:
    Code:
    int stringcheck(const char* string1_to_check_for,const char* string2_to_check_for,const char array_to_check[][8],int length_of_array){
    Last edited by Ayreon; 03-07-2009 at 09:42 AM.
    Nothing to see here, move along...

  11. #56
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    Code:
    int stringcheck(const char* string1_to_check_for,const char* string2_to_check_for,const char array_to_check[][8],int length_of_array);
    
    int main()
    {
    	char comb_array[10][8];
    	stringcheck(NULL, NULL, comb_array, 0);
    }
    I get no errors / warnings.

    EDIT: This should work
    Code:
    		strcpy(comb_array[comb_array_pointer], current_comb1);
    I just realized they are strings. So you must use strcpy to copy a string.
    Last edited by Elysia; 03-07-2009 at 09:56 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #57
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    I don't get it, I'm doing exactly the same, but I get that warning.
    It compiles fine however, when I change the argument in the function to a char instead of a const char.

    And memcpy does seem to work. I've tested it by putting a user given string into the array and then reading out the array. It works:

    Code:
        int lines_in_file0 = 0;
        char filenames[10][512];
        char* datafile[1000];
        
    
        printf("Give the name of the file you wish to analyze.\n");
        printf("-> ");
        
        scanf("%s", &datafile);
        printf("\n");
        
        memcpy(filenames[0], datafile, 512);
        printf("filename placed in array: %s", filenames[0]);

    But this is in the main, and maybe a different situation. I've changed it in the "Analyse3" function to what you said.
    Nothing to see here, move along...

  13. #58
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    This is undefined. You are lucky it works, actually.
    You are defining an array of 1000 char* pointers, and they are not initialized.
    Luckily, you pass the address of the array, which points to its beginning and thus scanf will read into the data allocated for the pointers, and not where the pointers point.
    Using pointers without allocating them.
    Also, using scanf to read strings is dangerous. Scanf woes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #59
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    How else can I read the user's input?

    Can I just use char* datafile then? Or maybe char datafile[512]?
    Nothing to see here, move along...

  15. #60
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    If you would closely read those links I gave you...? Did you do that?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 4 of 6 FirstFirst 123456 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  2. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  4. Checking maximum values for dynamic array...
    By AssistMe in forum C Programming
    Replies: 1
    Last Post: 03-20-2005, 11:39 PM
  5. Replies: 5
    Last Post: 05-30-2003, 12:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21