在C语言中,‘int *’是否为派生数据类型?关于两类矛盾代码案例的疑问
int* Count as a Derived Data Type? Resolving Your Contradictory Cases Great question—this confusion stems from how C's declaration syntax works, not whether pointer types like int* qualify as derived types. Let's break down your two cases to clear this up:
Case 1: Why int* a, b, c, d Doesn't Make All Variables Pointers
Here's the key detail you're missing: C's declaration syntax doesn't treat int* as a single, atomic type label. Instead, the * is a type modifier that binds to the variable name, not the base type int.
When you write:
int* a, b, c, d;
It's actually interpreted by the compiler as:
int *a, b, c, d;
Which means:
ais a pointer to anint(a derived type)b,c,dare regularintvariables (the base type)
This isn't evidence against int* being a derived type—it's just how C's declaration grammar is designed. If you want all variables to be pointers, you'd need to write:
int *a, *b, *c, *d;
Case 2: Why int* MyFunc() Confirms int* Is a Derived Type
Your second case is spot-on: when specifying a function's return type, int* does represent a complete derived type. A pointer type is explicitly defined as a derived type in C (derived from its base type, int in this case).
The function declaration int* MyFunc() tells the compiler that the function returns a value of type "pointer to int"—which is absolutely a derived data type. This doesn't contradict Case 1; it's just a different use of the type syntax, where we're referring to the complete derived type rather than declaring multiple variables with mixed modifiers.
Final Verdict
int* (or more precisely, "pointer to int") is a derived data type in C. The apparent contradiction between your two cases is purely a result of C's unique declaration rules, not a flaw in the definition of derived types.
内容的提问来源于stack exchange,提问作者user15933960

