C语言指针引用定义用*的原因及指针赋值合法性概念性问询
Hey there! Let's break down your questions about C pointers clearly—they can feel confusing at first, but once you wrap your head around how C handles types and memory, it makes sense.
1. Why do we need * to define pointer-type variables in C?
In C, every variable has a type that tells the compiler two critical things:
- How much memory to allocate for the variable
- How to interpret the data stored in that memory
A pointer isn't a regular variable—it doesn't hold a value like an integer or character. Instead, it holds the memory address of another variable. The * in the declaration (int *aPtr, for example) is C's way of saying:
"This variable isn't storing an int directly. It's storing the address of an int variable."
Without that *, the compiler would treat the variable as a regular int (or whatever base type you use), which is a completely different kind of data. Think of it like labeling a box: a regular int box holds numbers, while an int* box holds the location of a number box. The * is the label that tells everyone what the box is meant to contain.
2. Why is the first code valid, but the second isn't?
Let's start by looking at both snippets side by side:
Valid Code
int a = 5; int *aPtr = &a; printf("%i", *aPtr);
Invalid Code
int a = 5; int aPtr = &a; printf("%i", *aPtr);
Here's the breakdown of why one works and the other doesn't:
The Valid Snippet
int *aPtr = &a;: We're declaringaPtras an int pointer (thanks to the*). The&aoperator gives us the memory address ofa, which is exactly the type of data anint*variable is designed to hold. This is a type-compatible assignment—like putting a house address into an address book, not into a box meant for numbers.*aPtr: Here, the*is the dereference operator. It tells the compiler: "Go to the memory address stored inaPtr, and grab the int value that lives there." SinceaPtris a valid pointer pointing toa, this gives us the value5, which matchesprintf's%iformat specifier.
The Invalid Snippet
int aPtr = &a;: Here,aPtris declared as a regular int variable, not a pointer. But&ais anint*(a pointer to int). Assigning a pointer type to a regular int type is a type mismatch. C is strict about types—you can't just put an address (which is a memory location, often a large hex number) into a variable meant to hold small integers. Some compilers might warn you about this, but it's fundamentally wrong.*aPtr: The real showstopper here is trying to dereferenceaPtr. The*operator only works on pointer types—because only pointers hold valid memory addresses. SinceaPtris a regular int, the compiler has no idea what to do with*aPtr: it would try to treat the integer value stored inaPtras a memory address, which is almost certainly an invalid location (leading to a crash or garbage output). Worse, from a syntax standpoint, C doesn't allow dereferencing non-pointer variables at all—this is a core rule of the language's type system.
In short: The invalid snippet fails because you're mixing up regular integer types and pointer types, which are completely distinct in C.
内容的提问来源于stack exchange,提问作者user6769219

