You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

编译警告:strcpy实参2无强制转换将整数转指针,求解决方案

Fixing the 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 string temp to strcpy
  • You accidentally used an integer variable (like x or b) as the second argument to strcpy
  • A typo: you referenced a single-character variable instead of your string buffer

Step-by-Step Fixes

  1. Ensure you're passing a valid string pointer to strcpy
    The correct usage for copying a title (like your temp buffer into a book's title field) looks like this:

    strcpy(book_array[x].title, temp);
    

    Here, temp is your char[] buffer—this is a valid pointer to a string, which matches exactly what strcpy expects.

  2. 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 misusing strcpy:

    book_array[x].title[0] = 'A'; // Or temp[0], if that's what you intended
    
  3. Bonus: Fix your fgets newline issue
    Your code uses fgets(temp, MAX_TITLE_LENGTH, stdin);—note that fgets includes the newline character (\n) in the buffer. This will make strcmp fail 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:01:21