Hi!
i trying to take the same value from 2 arrays and then count how many time of it. However, when i compile, it run ok. but it did not print out the value of coutting. I did not know why, i had tried several times to fixing. please help me.

#include <stdio.h>
#include <stdlib.h>

int main() {
    char *em[] = {"hello", "bunjour", "ccc"};
    char *e[] = {"hello", "b"};
    int i, j, count = 0;
    for (i = 0; i < 10; i++) {
        for (j = 0; j < 10; j++) {
            if (strcmp(em[i], e[j]) == 0) {
                count++;
            }
            if (em[i] == NULL) {
                break;
            }
        }
        if (e[i] == NULL) {
            break;
        }
    }
    printf("count %d\n", count);

    return 0;
}

Dani AI

Generated

Short diagnosis: the program invokes undefined behavior by indexing past the ends of the arrays. The two literal arrays are small (3 and 2 elements) but the loops run far beyond that, so strcmp gets invalid pointers and the run-time result is unpredictable. The code also never includes <string.h>, so on some compilers strcmp may be implicitly declared or mis-compiled. As noticed, you must stop the loops at the actual array length; ’s idea of using fixed bounds works but is brittle — a safer approach is to compute each array’s length at compile time.

A compact, correct pattern (C) is to compute lengths with sizeof and use those in the loop bounds; do not test for NULL unless you purposely add a NULL sentinel. Also use <string.h> for strcmp and prefer size_t for indexes:

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

int main(void) {
    const char *a[] = {"hello", "bonjour", "ccc"};
    const char *b[] = {"hello", "b"};
    size_t na = sizeof(a) / sizeof(a[0]);
    size_t nb = sizeof(b) / sizeof(b[0]);
    int count = 0;
    for (size_t i = 0; i < na; ++i)
        for (size_t j = 0; j < nb; ++j)
            if (strcmp(a[i], b[j]) == 0)
                ++count;
    printf("count %d\n", count);
    return 0;
}

Quick troubleshooting tips: compile with warnings and a modern standard (for example gcc -std=c99 -Wall -Wextra), run with AddressSanitizer (-fsanitize=address) or valgrind to catch out-of-bounds accesses, and avoid inserting C++ headers like <string> or using namespace std in plain C code (that was in ’s reply). Checklist: include <string.h>, compute lengths via sizeof (or use a NULL sentinel consistently), loop with < length (not a hardcoded high value), and test with sanitizers.

Recommended Answers

All 4 Replies

lines 8 and 9. Why are those loops counting from 0 to 10? Array em only contains 3 trings and array e contains 2 strings.

line 17: array em does not contain a NULL pointer, so line 17 will do nothing.

There are a couple ways to do what you are tring to do. One ways is to add a NULL string at the end of each array so that the program doesn't have to know ahead of time how many strings are in each array. The loops just keep on incrementing until a NULL string is found

The other way is to just hard-code the loop counters with the number of strings in each array, for example for(j = 0; j < 2; j++)

Hi!
i trying to take the same value from 2 arrays and then count how many time of it. However, when i compile, it run ok. but it did not print out the value of coutting. I did not know why, i had tried several times to fixing. please help me.

#include <stdio.h>
#include <stdlib.h>

int main() {
    char *em[] = {"hello", "bunjour", "ccc"};
    char *e[] = {"hello", "b"};
    int i, j, count = 0;
    for (i = 0; i < 10; i++) {
        for (j = 0; j < 10; j++) {
            if (strcmp(em[i], e[j]) == 0) {
                count++;
            }
            if (em[i] == NULL) {
                break;
            }
        }
        if (e[i] == NULL) {
            break;
        }
    }
    printf("count %d\n", count);

    return 0;
}

Hi,

The below code may help u.Since you have only 3 string in first em variableand 2 string in second e variable then how can you use for loop for 10 times.

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

using namespace std; 

int main() {
    char *em[] = {"hello", "bunjour", "ccc"};
    char *e[] = {"hello", "b"};
    int i, j, count = 0;
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 1; j++) {
            if (strcmp(em[i], e[j]) == 0) {
                count++;
            }
            if (em[i] == NULL) {
                break;
            }
        }
        if (e[i] == NULL) {
            break;
        }
    }
    printf("count %d\n", count);
 
    return 0;
}

@Perry -- the two loops are still incorrect.

@Perry -- the two loops are still incorrect.

Hi Ancient Dragon,

I think this is correct

for (i = 0; i <= 2; i++) {
        for (j = 0; j <= 1; j++) {
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.