今日已更新 273 条资讯 | 累计 21762 条内容
关于我们

What Is a Pointer in C? A Beginner's Guide

codeFacility 2026年07月20日 02:43 1 次阅读 来源:Dev.to

What Is a Pointer in C? A Beginner's Guide If you're learning C and pointers are the moment things suddenly feel harder, you're not alone. Pointers trip up more beginners than almost any other concept in the language. But the core idea is simpler than it looks once you strip away the confusing syntax: a pointer is just a variable that stores an address instead of a value. A Variable Normally Stores a Value When you write int age = 25; , C sets aside a small chunk of memory, gives it a label called age , and stores the number 25 inside it. Every variable in your program lives somewhere in memory, and every location in memory has an address, similar to a house having a street address. Most of the time you don't think about that address at all. You just use the variable name and C handles the memory bookkeeping behind the scenes. What a Pointer Actually Stores A pointer is a variable, but instead of holding a regular value like a number or character, it holds the memory address of another variable. Here's what that looks like: int age = 25 ; int * agePointer = & age ; The & symbol means "give me the address of," and * when declaring a variable means "this variable is a pointer." So agePointer doesn't contain 25. It contains the address where 25 is stored. If you want to see the value at that address, you dereference the pointer using * again: printf("%d", *agePointer); would print 25, not the address. Why Not Just Use the Variable Directly? This is the question that trips up most beginners, and it's a fair one. If you already have age , why bother with a pointer to it? The real value of pointers shows up in a few common situations: Passing large data to functions. When you pass a variable to a function in C, it normally gets copied. For a single integer that's cheap, but for a large array or struct, copying is wasteful. Passing a pointer instead means the function works with the original data directly, without duplicating it. Modifying a variable inside a function. Nor

本文内容来源于互联网,版权归原作者所有
查看原文