为何无法初始化指针变量且gets不可用?求C语言代码报错原因与修复方案
Let's walk through your two errors and fix them step by step, plus adjust the code to match your desired output format.
1. Error: "This entity cannot be initialized." on char* exclude = " .,! ";
Why this happens:
String literals like " .,! " are read-only in C. They're stored in a special section of memory that your program isn't allowed to modify. When you assign this literal to a non-const char* pointer, modern compilers (especially MSVC, which you're likely using given _CRT_SECURE_NO_WARNINGS) flag this as a safety issue—since the pointer could theoretically be used to write to the read-only literal, which is undefined behavior.
Fix:
Declare the pointer as const char* to explicitly state that it points to read-only data:
const char* exclude = " .,! ";
This matches the type expected by strtok() (its second parameter is const char* anyway), so it won't break your tokenization logic.
2. Error: "The identifier gets cant be found." on while (gets(str[i]))
Why this happens:
The gets() function was removed from the C standard in C11 because it's catastrophically unsafe. It doesn't check how much input it reads, so if a user types more than 80 characters (the size of your str[i] buffer), it will overflow the buffer and crash your program (or worse, create a security vulnerability). Modern compilers no longer include this function, hence the "identifier not found" error.
Fix:
Replace gets() with fgets(), which is safe because it lets you specify the maximum number of characters to read. The catch is fgets() includes the newline character (\n) from the user's Enter press in the buffer—so we need to remove it manually to avoid it being treated as part of a word.
Here's the revised loop:
while (fgets(str[i], 81, stdin) != NULL) { size_t len = strlen(str[i]); // Remove the trailing newline if it exists if (len > 0 && str[i][len - 1] == '\n') { str[i][len - 1] = '\0'; } maxstr++; i++; // Prevent buffer overflow: stop after 10 lines (your array size) if (i >= 10) { break; } }
fgets(str[i], 81, stdin)reads up to 80 characters (plus the null terminator) from standard input.- We check for the newline character and replace it with a null terminator so
strtok()doesn't treat it as a separator. - We add a check to stop after 10 lines to avoid exceeding the bounds of your
str[10][81]array.
Full Fixed Code
I also adjusted the output to match your example (e.g., Hello(5) I(1) instead of strlen(Hello) = 5):
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> int main() { char str[10][81]; char* ptoken, * ptr; int i = 0, maxstr = 0; const char* exclude = " .,! "; printf("Enter the text you want on multiple lines\n"); printf("after you enter everything press ctrl+z and Enter \n\n"); while (fgets(str[i], 81, stdin) != NULL) { size_t len = strlen(str[i]); if (len > 0 && str[i][len - 1] == '\n') { str[i][len - 1] = '\0'; } maxstr++; i++; if (i >= 10) { break; } } for (i = 0; i < maxstr; i++) { printf("\nWords typed on line number %d: ", i + 1); ptr = str[i]; ptoken = strtok(ptr, exclude); while (ptoken != NULL) { printf("%s(%d) ", ptoken, (int)strlen(ptoken)); ptoken = strtok(NULL, exclude); } printf("\n"); } return 0; }
内容的提问来源于stack exchange,提问作者l Wawa

