编译警告:strcpy实参2无强制转换将整数转指针,求解决方案
strcpy Warning: "pointer from integer without a cast" Hey there, let's break down that annoying warning you're hitting and fix it up. First, let's understand exactly what the warning is telling you:
passing argument 2 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]
This means when you called strcpy, you passed an integer (or a single char, which is treated as an integer in C) as the second argument—but strcpy expects a string pointer (const char*) for that position. The compiler is flagging this because it's a critical type mismatch that will almost certainly cause bugs or crashes at runtime.
Common Causes in Your Code Context
Looking at your book deletion function and Book struct, here are the most likely mistakes you're making:
- You tried to pass a single character (like
temp[0]) instead of the full stringtemptostrcpy - You accidentally used an integer variable (like
xorb) as the second argument tostrcpy - A typo: you referenced a single-character variable instead of your string buffer
Step-by-Step Fixes
Ensure you're passing a valid string pointer to
strcpy
The correct usage for copying a title (like yourtempbuffer into a book'stitlefield) looks like this:strcpy(book_array[x].title, temp);Here,
tempis yourchar[]buffer—this is a valid pointer to a string, which matches exactly whatstrcpyexpects.If you only need to copy a single character, don't use
strcpy
If you meant to set just one character in the title, assign it directly instead of misusingstrcpy:book_array[x].title[0] = 'A'; // Or temp[0], if that's what you intendedBonus: Fix your
fgetsnewline issue
Your code usesfgets(temp, MAX_TITLE_LENGTH, stdin);—note thatfgetsincludes the newline character (\n) in the buffer. This will makestrcmpfail if your stored book titles don't have that trailing newline. Fix it with this quick line:// Remove the trailing newline from the temp buffer temp[strcspn(temp, "\n")] = '\0';
Quick Note on Your Deletion Loop
While we're at it, I spotted a potential logic bug in your loop:
for(x = 0; x < no_books; x++) { if (strcmp(book_array[x].title, temp) == 0) { for(x=1; x < no_books -1; x++) { // This overwrites the outer loop's x! // ... } } }
Reusing the x variable in the inner loop will break your outer loop's iteration. Swap it for a different variable name (like y) to avoid this.
内容的提问来源于stack exchange,提问作者John

