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...
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...
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...
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...
Problem here. You cannot assign arrays in C. Either use a loop or memcpy.Code:comb_array[comb_array_pointer] = current_comb1;
Since you are assigning to it, it must be char, not const char.Code:const char comb_array[max_comb][8];
Other than that, you have one } too much:
You MUST work on your indentation! This is a problem you get because you DON'T have proper indentation!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); } }
Other than that, the examples compiles.
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...
Check the http://apps.sourceforge.net/mediawik...le=Indentation, or have a look at Wikipedia. Consistency is the key.
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.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.
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...
I've tried this: It doesn't give any compile errors, but that doesn't always mean it's ok:
However I do still get:Code:memcpy(comb_array[comb_array_pointer], current_comb1, 8);
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:
And this is the function's argument:Code:char comb_array[max_comb][8];
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 10:42 AM.
Nothing to see here, move along...
I get no errors / warnings.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); }
EDIT: This should work
I just realized they are strings. So you must use strcpy to copy a string.Code:strcpy(comb_array[comb_array_pointer], current_comb1);
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...
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.
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...