C语言箭头运算符(指针与结构体)报错问题及解决咨询
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
doublevalues withscanf, you need%lf, not%fl(you had the letters reversed). - In your final
printf, you used/ninstead of\nfor 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 *ptopt_box psoppoints directly to your struct, makingp->work as intended. - Malloc Fix: Now allocates space for the full struct instead of just a pointer.
- Format Fixes: Corrected
%flto%lffor reading doubles, and fixed/nto\nfor newlines. - Array Fix: Updated the struct's
posarray to 3 elements to fit x/y/z coordinates, eliminating out-of-bounds errors. - Memory Cleanup: Added a check for
mallocfailure andfree(p)to avoid memory leaks—this is a great habit to build early!
内容的提问来源于stack exchange,提问作者Johnmar

