C语言中二维数组与字符数组对比://do something语句未执行
Hey there, let's dig into why your //do something block isn't executing when comparing those arrays. Since you've already ruled out newline leftovers, let's walk through other common issues that might be causing this:
1. Mismatched Array Dimensions or Effective Lengths
Even if you think the arrays "should" match, double-check their actual content lengths:
- If your 2D array stores lines from the file, make sure each line's length exactly matches the length of your target character array. Don't just rely on the declared array size—for example, a
fileArr[5][10]might only hold 8 valid characters per row, while your target array is 10 characters long. - Verify you're comparing the right row of the 2D array to the target array (if you're looping through rows, make sure the loop isn't skipping the matching entry).
2. Hidden Invisible Characters (Beyond Newlines)
You eliminated newlines, but other unseen characters could still break the match:
- Windows-saved files might leave a carriage return (
\r, ASCII 13) even after removing newlines. - Accidental spaces, tabs (
\t), or even UTF-8 BOM headers (if your file uses UTF-8 with BOM) could be lurking.
To debug this, print every character's ASCII value from both arrays:
// Print file array details for (int i = 0; i < totalRows; i++) { for (int j = 0; fileArr[i][j] != '\0'; j++) { printf("fileArr[%d][%d]: '%c' (ASCII: %d)\n", i, j, fileArr[i][j], fileArr[i][j]); } } // Print target array details for (int k = 0; target[k] != '\0'; k++) { printf("target[%d]: '%c' (ASCII: %d)\n", k, target[k], target[k]); }
This will reveal any hidden characters that look identical but have different ASCII values.
3. Flawed Comparison Logic
It's easy to slip up here—common mistakes include:
- Comparing array addresses instead of content: Using
if (fileArr[row] == target)compares memory locations, not the actual characters inside. This will never be true unless the arrays point to the same memory. - Incomplete loop conditions: Your comparison loop might exit early, or fail to check if one array ends before the other.
Use a proper element-wise comparison or strcmp (for null-terminated strings):
// Example: Compare a single row of the 2D array to the target int isMatch = 1; int targetLen = strlen(target); int fileRowLen = strlen(fileArr[rowIndex]); // First check if lengths match if (targetLen != fileRowLen) { isMatch = 0; } else { // Then compare each character for (int k = 0; k < targetLen; k++) { if (fileArr[rowIndex][k] != target[k]) { isMatch = 0; break; } } } if (isMatch) { // Your //do something code should run now if a match is found }
4. Missing Null Terminator (\0)
If you didn't properly add a null terminator to the file-read array, string functions (like strcmp or your custom loop) will read past the valid content into garbage data, causing mismatches.
When reading from the file, ensure you explicitly add \0 after removing any line endings:
char buffer[256]; while (fgets(buffer, sizeof(buffer), fp) != NULL) { // Remove newline (and carriage return if present) buffer[strcspn(buffer, "\n\r")] = '\0'; // Copy to your 2D array strcpy(fileArr[rowCounter++], buffer); }
Start with printing the ASCII values—it's the fastest way to confirm if the content is truly identical. Then double-check your comparison logic to make sure you're actually checking the characters, not just array pointers.
内容的提问来源于stack exchange,提问作者user8940323

