r/cprogramming • u/Delicious-Change7795 • Sep 12 '26
What are pointers?
I am new to C programming and i came through pointer. So can anyone tell what are they and why we need them? And if u can then can you give me reliable source to learn it.
15
Upvotes
19
u/tobdomo Sep 12 '26
Pointers are addresses. Usually addresses of memory, but they can also point to peripherals. A pointer variable is a variable that contains an address.
Example: a byte lives in memory at address 1234. Assuming your core architecture supports a linear address space (i.e.: addresses are numbered, starting at 0 just count upwards). Thus, if p is a pointer to this byte, it contains the address 1234. In code:
This means: p is a pointer that gets assigned the address of x.
Why you need it? Let's say you have a function called
addone()that takes a variable of type int and increments it. You could (very verbosely) write:This actually reads value as referred to by
ptr, adds 1 to it and stores it at the address referred to byptr.Pointers are very useful for a number of cases. E.g., if you want to allocate memory at runtime to store information, you would use
malloc(). malloc's prototype is:As you can see, the function takes a size. It allocates a size bytes on the heap and returns the address to that memory for the caller to use. That address can be stored in a pointer so that you can use it for future reference. E.g., when you no longer need the memory allocated by
malloc(), you need to free it using the functionfree(). E.g.:In this example, p is a pointer that saves the address of the memory allocated by
malloc(). The memory is free'd for re-use just before returning from this function through the call tofree().6
u/Kurouma Sep 12 '26
Your data lives somewhere in memory. Doing operations involving data would be slow if you had to copy all the data around all the time. So sometimes it is useful to just know where something is so you can operate on it directly. The "where" (a pointer) is a single piece of information, unlike your data (which may be big).
So kind of like
instead of
3
u/neomashab Sep 12 '26
This is a gem
https://youtube.com/playlist?list=PL9D558D49CA734A02&si=Jc6I3O3GJdoy0-zP
5
u/torsten_dev Sep 12 '26 edited Sep 16 '26
Only really understood after learning a bit of assembly
vs
5
u/zhivago Sep 12 '26
To understand pointers, start with pointer arithmetic.
What does puts(p); output?
What does puts(p + 1); output?
And what is (p + 1) - p?
What does putchar(*p); output?
What does putchar(*(p + 1)); output?
2
u/Party_Trick_6903 Sep 12 '26 edited Sep 12 '26
When you do:
int a = 23, you create a variableathat holds a value23. This variable is stored somewhere in the memory. This variable gets assigned a memory address so we know where exactly it is in the memory. Let's say the address of this variableais0x7ffe5367e044.A pointer is a variable that holds a memory address of another variable as its value. Basically, instead of int value
23(like you see above), it holds a memory address (for example0x7ffe5367e044) as its value and this memory address is a memory address of another variable. And we can use this pointer to access the variable that is at the said memory address.An example:
2
u/c_cpp_CSharp Sep 12 '26
pointer is a name for a thing that holds the starting position of data that is represented with an address
1
u/Pesciodyphus Sep 12 '26
Do you alread know other programming languages ? It is important to know, whether you come from Assembler or from Object Oriented Programming or are an absolute beginner.
In Assembly it is stuff like MOV AX,[BX] instead of MOV AX,BX . In the first example BX is an adress and you read the word pointed to by BX into AX. In the second you copy BX to AX.
In Object oriented programming, think of objects a structure pointers and methods as function that get a structure pointer named this as hidden parameters. And while the syntax for method-calls in Java or C++, look like callling function pointers in C in the language, most methods aren't implemented as pointer inside the structure, as the compiler simply knows what to call. Only if Virtual Methods or multiple inheretence is used, a function pointer is needed.
1
u/Dry-War7589 Sep 12 '26
A pointer is a variable that holds the address of another variable or some data. Think of it like an address of a house: the address tells you where the house is, but you have to go to the house to get the information. Now, the syntax looks like this:
In this example we have declared a pointer that points to a string. Now
pointercontains the address of"Hello World\n". And it points to the first character of the string, which is H. To get the data from the pointer, you need to dereference it. You do that by putting * or [index] infront of pointer. Example:Here printf will output
To get the next character you can do
pointer + 1. This is called pointer arithmetic, and it advances the pointer by one element. Technically it would advance bysizeof(char);bytes, but it is basically advancing by one element. I1
u/TakenIsUsernameThis Sep 12 '26
Its a variable that stores an address in memory where another variable is stored
1
u/duane11583 Sep 12 '26
think of memory like grid paper each square is an 8 bit number or one letter in a string (word, ie letter d in dog)
but you need to store a bigger (16, 32, 64 bit) number or a string like dog, cat or text like blah
so you pick some location on that grid and write the value to that location (and possibly the next 2 or 3 or ?? squares) or maybe it is a collection of values (name, address, etc aka a data record or data structure) or maybe it is an array of those things
you now want to perform some operation on that value.. ie: add one, multiply, or square root, or get the length of the text.
another example is to compare two data records. question: what does the comparison function need? the value (that is too hard to pass to the function) but can we pass the starting location to the two values to compare? or the starting location where the number is stored?
what if we can write a generic function that given two starting locations it can compare two strings and tell us which one comes first or if they are equal? we can use that to sort or find something.
that starting location is a pointer.
in the c language a generic starting location is a void pointer.
then you can convert that generic value into a specific type ie “cast a void pointer in to an float pointer”
1
u/Dazzling_Music_2411 Sep 12 '26
Where did you come across pointers?
Did they not explain what they are there?
1
u/tcpukl Sep 12 '26
Memory addresses.
Read this with the more elaborate replies
1
u/StunningHeart7004 Sep 12 '26
Value - the "guy' Pointer - knows the "guy" Pointer to pointer - knows the guy who knows the "guy"
1
u/SmokeMuch7356 Sep 12 '26 edited Sep 14 '26
A pointer is any expression whose value is the location of an object or function in a running program's execution environment; i.e., an address, but with additional type information.
We use pointers when we can't (or don't want to) access a function or object by name; they provide indirect access. If you've played with arrays at all, you've already seen a form of indirection:
The expression
arr[i]can refer to different objects at different times based on the value ofi. A pointer works similarly.Pointer variables store values of pointer type. Different kinds of pointer variables are declared as follows (for some arbitrary type
T):A pointer to
int(int *) is a different type from a pointer tochar(char *) which is a different type from a pointer todouble(double *) which is a different type from a pointer to an array offloat(float (*ap)[N]), etc. Pointers to different types are incompatible and cannot be directly assigned to each other without a cast. The exception to this rule is that a pointer tovoid(void *) can be assigned to different pointer types and vice versa without a cast.Pointer values are obtained by:
Using the unary
&operator on an lvalue expression (something that designates an object):The expression
*pacts as an alias forx; you can read and write*pas if it wasx.malloc,calloc, andreallocfor dynamic memory, but also functions likefopen,fgets, etc.There are two cases where we have to use pointers:
Pointers also come in handy for:
IMO the best way to explain pointers is to show how they're actually used in working code. Here's a short, useless, but complete program that demonstrates how pointers are typically used - updating function arguments and tracking dynamic memory. It generates an array of random integer values, displays them, sorts the array, then displays the sorted array. Don't worry if it doesn't all make sense; this is just what code that involves pointers typically looks like.
And the output:
1
u/reality_comes Sep 12 '26
You're starting at the top and working your way down, it will make 10x more sense if you start at the bottom and work your way up
1
u/buzzon Sep 12 '26
Pointers are variables that store addresses of other things (typically other variables).
Several use cases for pointers:
1
u/Azra__1 Sep 13 '26
Data lives in memory and a pointer to that data indicates where is that data located in memory
1
u/Nimesh1205 Sep 13 '26
Pointer is the variable that store the address of another variable. To understand pointers in dept you must read :
1
u/Rythim Sep 13 '26 edited Sep 13 '26
A pointer is the value of the memory address of a value.
Say for example variable a stores and integer 1.
And pointer b stores the location in memory where variable a stores integer 1.
They are needed for multiple reasons.
Say you have a function that takes a struct as a parameter. Without pointers, when you pass a struct to the function, the function adds a copy of that struct to the stack, does whatever operations you code, then deletes it from the stack. The original struct is never changed. If you'd like to change it, you'd need your function to return the copy of the struct and assign that to the original variable. With small simple structs that is no big deal, but with larger structs, or with a large number of structs, that can be slow and use up more memory (because now you have several copies of data in memory).
If instead in your function you pass a pointer to a struct, that function gets a copy of the memory address of the struct. Using that as a reference it can find the original struct and operate on it directly without having to make a copy. This uses less memory and can be faster.
Pointers are important for performance critical code. But they can be dangerous because depending on how you structure your code you could do something unintended. Such as trying to work with a pointer that is referencing a memory address that has already had the data freed, or reserving several chunks of memory and forgetting to free it up to be used later when done with it.
Other languages like python don't use Pointers. Instead, they pass "pointers" (references) automatically when it makes sense to do so and keep track of when memory isn't needed anymore and delete it for you. But keeping track of all that adds more work for the computer, which is one reason why C is considered much faster than most other languages.
1
u/CertainBaby9837 Sep 13 '26
What are pointers the answer is nightmare 😂😂😂😂
1
u/DreadSpidey Sep 13 '26
Pointers are basically addresses, watch a youtube video and build a project you should be good
1
u/nngnna Sep 15 '26
Your program see a list of all the storage locations it uses, with each having an address. Pointers are a bit more "clever" than byte adresses because they're typed. So for instance a 64-bit of memory contain 8 addressable bytes, but only 4 pointable int16_t or 2 pointable int32_t or a single int64_t.
Memory that is declared or obtained togheter is often contigous, so one pointer let you access others by adding and substracting from it, and you don't have to multiply by the number of bytes in your type.
More generally pointers let you (and the creators of you favorite language) build all non-trivial data-structures by having data that referance other data.
1
u/EpochVanquisher Sep 12 '26
Get a good book like the KN King book, or use a good course like CS50.
Pointers are variables that, when valid,can contain NULL or references to values. You can access the other value using the pointer.
1
u/mathlontrades Sep 12 '26
From a value point of view: A memory address.
From a usage point of view: Something that points to a location which is the start of a block of data, and when dereferenced, gives you access to the actual value stored at that address.
Think of it like a sticky note with a house address written on it. The note itself is just a set of directions (the pointer value), but when you actually drive to that address and open the front door (dereferencing it), you get to interact with whatever or whoever is sitting inside the house.
0
u/ern0plus4 Sep 12 '26
Pointer is an address of your stuff:
Accessing stuff your through a pointer is a slightly slower than accessing stuff directly, but it doesn't matter practically.
Pointers introduce a lot of problems, e.g. if a pointer is pointing to to your array, but you destroy the array, the pointer does not get destroyed, it will keep pointing to the same memory address, which doesn't contains your array anymore, or, worse, it contains, nobody overwritten it, but it's already invalid... These issues art not the problems of the pointers, but programming.
-4
u/AdOnly69 Sep 12 '26
Learn python, you will be safe from pointers (because everything will be a pointer)
0
u/[deleted] Sep 12 '26
[deleted] — view deleted comment