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

C语言箭头运算符(指针与结构体)报错问题及解决咨询

Fixing Your C Code: Resolving the "did you mean to use '->'?" Error & Other Issues

Hey there! Let's break down the issues in your code step by step—no worries, these are super common pitfalls when you're just starting out with C.

Key Problems in Your Code

1. Wrong Pointer Level (The Core Error)

You defined typedef struct s_box *pt_box;, which means pt_box is already a pointer to your struct. Then you declared pt_box *p—this turns p into a pointer-to-a-pointer (struct s_box **). When you try p->matnr, the compiler gets confused because p doesn't point directly to a struct, it points to another pointer. That's why you're getting that confusing error message even though you used ->.

2. Incorrect malloc Memory Size

Right now you're allocating sizeof(pt_box)—that's only the size of a pointer (usually 4 or 8 bytes), not enough space for your entire struct. You need to allocate space for the full struct s_box instead.

3. Broken Format Specifiers

  • For reading double values with scanf, you need %lf, not %fl (you had the letters reversed).
  • In your final printf, you used /n instead of \n for newlines—this won't print a line break like you expect.

4. Array Out-of-Bounds Access

Your struct's pos array is int pos[2]; (only 2 elements: index 0 and 1), but you're trying to input and store a z-coordinate in pos2[2] and p->pos[2]. This is called array out-of-bounds access, which leads to undefined behavior (your program might crash or act weird).

5. Invalid Array Assignment

C doesn't let you assign arrays directly like p->pos = pos2[0],pos2[1],pos2[2]. You have to copy elements one by one, or use a function like memcpy to copy the whole array.


Corrected Code

#include <stdio.h>
#include <stdlib.h>
#include <string.h> // Needed for memcpy

struct s_box {
    int matnr;
    double vol;
    int pos[3]; // Updated to hold 3 coordinates (x/y/z)
};

int main () {
    int matnr2;
    double vol2;
    int pos2[3]; // Matching 3-coordinate array
    typedef struct s_box *pt_box;
    
    // Declare p as a direct pointer to the struct (not pointer-to-pointer)
    pt_box p;
    // Allocate enough memory for the entire struct
    p = (pt_box)malloc(sizeof(struct s_box));
    // Always check if malloc succeeded (prevents crashes if memory is full)
    if (p == NULL) {
        printf("Failed to allocate memory!\n");
        return 1;
    }

    printf("Write a value for: \nMaterialnr= ");
    scanf("%d",&matnr2);
    printf("\nVolumen = ");
    scanf("%lf",&vol2); // Fixed format specifier for double
    printf("\nxPos =");
    scanf("%d",&pos2[0]);
    printf("\nyPos =");
    scanf("%d",&pos2[1]);
    printf("\nzPos =");
    scanf("%d",&pos2[2]);

    // Now p is a direct pointer to the struct, so -> works correctly
    p->matnr = matnr2;
    p->vol = vol2;
    // Copy the coordinate array (two options below, pick one)
    // Option 1: Assign each element manually
    // p->pos[0] = pos2[0];
    // p->pos[1] = pos2[1];
    // p->pos[2] = pos2[2];
    // Option 2: Use memcpy to copy the whole array at once
    memcpy(p->pos, pos2, sizeof(pos2));

    // Fixed newline character and cleaned up output format
    printf("MaterialNr = \t%d\nVolumen = \t%f m^3\nPosition = (%d,%d,%d)",
           p->matnr, p->vol, p->pos[0], p->pos[1], p->pos[2]);

    // Don't forget to free the memory we allocated!
    free(p);
    return 0;
}

Quick Notes on the Fixes

  • Pointer Fix: Changed pt_box *p to pt_box p so p points directly to your struct, making p-> work as intended.
  • Malloc Fix: Now allocates space for the full struct instead of just a pointer.
  • Format Fixes: Corrected %fl to %lf for reading doubles, and fixed /n to \n for newlines.
  • Array Fix: Updated the struct's pos array to 3 elements to fit x/y/z coordinates, eliminating out-of-bounds errors.
  • Memory Cleanup: Added a check for malloc failure and free(p) to avoid memory leaks—this is a great habit to build early!

内容的提问来源于stack exchange,提问作者Johnmar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 07:41:19