C语言运行时获取用户输入字符串长度的问题求助
Hey there! Let's break down the issues in your code and get it working properly—you're close, but there are a few critical fixes needed first.
First: Fix the Uninitialized Input Pointer
Your char *input; is just a pointer that doesn't point to any valid memory yet. When you try to use scanf with it, you'll get a crash (undefined behavior) because there's no space allocated to store the user's input.
Two simple fixes here:
- Use a fixed-size character array (great for beginners):
char input[256];(pick a size that makes sense for your use case) - Or dynamically allocate memory (more flexible, but requires cleanup):
char *input = malloc(256 * sizeof(char));(don't forget tofree(input);later!)
Second: Fix the scanf Call
Your scanf("%s\n", input); has an extra \n in the format string—this will cause scanf to wait for additional input after the string, which isn't what you want. Use scanf("%s", input); instead. Note that %s stops reading at the first whitespace character; if you need to read strings with spaces, use fgets(input, sizeof(input), stdin); instead (just remember to trim the trailing newline that fgets adds).
Third: Calculate the String Length
To get the length of the input string at runtime, use the strlen() function from the <string.h> standard library. Just add #include <string.h> at the top of your code, then set:
int n = strlen(input);
strlen() returns the number of characters in the string excluding the null terminator (\0), which is exactly what you need for your loop counts.
Thread Handling Notes
Since you're creating and joining threads, you'll need:
- To include the
<pthread.h>header - An array to store thread IDs (since you can't track n threads without it):
pthread_t threads[n]; - To compile with the
-lpthreadflag (e.g.,gcc your_code.c -o your_program -lpthread)
Full Modified Code Example
#include <stdio.h> #include <string.h> #include <pthread.h> // Use a fixed-size array for simplicity char input[256]; void *thread_function(void *arg) { // Example thread logic—replace with your code int index = *(int *)arg; printf("Thread %d handling character: %c\n", index, input[index]); return NULL; } int main(){ printf("Enter the string : "); // Use scanf for whitespace-separated strings, or fgets for strings with spaces scanf("%s", input); // Get the string length int n = strlen(input); // Array to hold thread IDs pthread_t threads[n]; int thread_args[n]; // To pass indices to threads safely // Create threads for (int i=0; i<n; i++){ thread_args[i] = i; if (pthread_create(&threads[i], NULL, thread_function, &thread_args[i]) != 0) { perror("Failed to create thread"); return 1; } } // Join threads for (int i=0; i<n; i++){ if (pthread_join(threads[i], NULL) != 0) { perror("Failed to join thread"); return 1; } } return 0; }
A few extra notes on the thread code:
- We pass thread indices via a separate array (
thread_args) because passing&idirectly would cause all threads to see the same changing value ofi. - Added error checking for
pthread_createandpthread_jointo catch issues early.
内容的提问来源于stack exchange,提问作者Latey Laley

