#weird output

27 messages · Page 1 of 1 (latest)

granite wasp
#
void findRecords(char * str) {
    char *pointer = strstr(str, "</");
    if(pointer != NULL){
        pointer += 2;
        int i = 0;
        while(i < strlen(pointer)){
            if(pointer[i] == '>'){
                pointer[i] = '\0';
                break;
            }
            i = i + 1;
        }
        i = 0;
        while(i < 1000) {
            if (mainArray[i] != NULL) {
                if (strcmp(mainArray[i], pointer) == 0) {
                    countArray[i] = countArray[i] + 1;
                    break;
                }
                i = i + 1;
            } else {
                mainArray[i] = pointer;
                //printf("Index: %i, Array: %s, Pointer: %s\n", indexArray, mainArray[i], pointer);
                countArray[i] = countArray[i] + 1;
                break;
            }
        }
    }
}

I wrote this method to collect all lines from a file which start with "</", I cut out the chars between "</" and ">" and finally saved them in "mainArray"
I can say for sure, that alle the "pointers" I write to "mainArray" are correct, but when I print out "mainArray" in a loop, there is just nonsense. Can someone explain me why this is happening?

char *mainArray[1000];
int countArray[1000];
honest egretBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

tight basin
#

are you intentionally only setting the first element in mainArray?

granite wasp
tight basin
granite wasp
#

In mainarray I list all the different strings and in countArray I am counting how often this special string shows up

tight basin
#

yeah so you should remove the break and increment i

#

and probably also zero initialize mainarray so you can know when to stop looping it for an example if you want to print the different strings

granite wasp
#

yeah I did this in the main Method

granite wasp
tight basin
#

in here ```c
while(i < 1000) {
if (mainArray[i] != NULL) {
if (strcmp(mainArray[i], pointer) == 0) {
countArray[i] = countArray[i] + 1;
break;
}
i = i + 1;
} else {
mainArray[i] = pointer;
//printf("Index: %i, Array: %s, Pointer: %s\n", indexArray, mainArray[i], pointer);
countArray[i] = countArray[i] + 1;
break;
}
}

#

wait

granite wasp
#

If the string is already in mainArray, then I go to the if statement, count it and break out of the while loop
If the string is not in the Array, I save it in the array and break out of the loop

tight basin
#

and you call that function multiple times?

granite wasp
#
    while (!gzeof(fp)) {

        findRecords(line);

        gzgets(fp, line, LL);

    }
#

yes I do

#

I think I am messing with the pointers, so strcmp is always true

#
void findRecords(char * str) {
    strcpy(tmp3, str);
    char *pointer = strstr(tmp3, "</");
    if(pointer != NULL){
        pointer += 2;
        int i = 0;
        while(i < strlen(pointer)){
            if(pointer[i] == '>'){
                pointer[i] = '\0';
                break;
            }
            i = i + 1;
        }
        strcpy(tmp, pointer);
        i = 0;
        while(i < 1000) {

            if (mainArray[i] != NULL) {
                if (strcmp(mainArray[i], pointer) == 0) {
                    countArray[i] = countArray[i] + 1;
                    break;
                }
                i = i + 1;
            } else {
                mainArray[i] = tmp;
                countArray[i] = countArray[i] + 1;
                i = i + 1;
            }
        }
    }
}

This is the version I am working with now

#

and I saw, that strcmp is always true
I think it is because I did something wrong with the pointer in here

#

In the very first loop the else case is entered because mainArray[0] == Null is true
In all the other loops strcmp is true

tight basin
granite wasp
#

what do you mean by starting from the start?

tight basin
#

actually idk

granite wasp
#

But thank you for your help!

#

How would you solve this problem?

honest egretBOT
#

@granite wasp Has your question been resolved? If so, run !solved :)

honest egretBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.