What is the purpose of pointers in c/c++

what is the purpose of pointers in c/c++.

Ive been learning C/C++ on and off for a few years as well as other languages.

javascript helped understand C better but I dislike implicit types in javascript.

Other urls found in this thread:

youtu.be/0yXySXtJ1rw
youtu.be/SURclGLblcU
youtu.be/otxb3CMXH5c
thecrimson.com/article/2002/6/4/college-friends-remember-gates-genius-poker/
youtu.be/1JV7OYGZSZ4?t=20
youtu.be/yc4S6XbbjIk?t=1355
twitter.com/SFWRedditVideos

>what is the purpose of pointers in c/c++.
To point to a location in memory.

A pointer is just a variable that points hence the name a location in memory - you should study computer science before programming especially if it's C++ else you'll be confused and fail in many regards.

Ex location in memory 1001 stores the value 1009 which points to memory location 1009

It is a tool that you can use for w/e you need.

A pointer is not a variable.

Do you know iterators in Java or C++?

You don't need those if you have pointers. Tip from me to you, gf.

...

they point to another value you faggot

read the word "pointer" it fucking does what it says you maggot

it fucking points you fucking faggot

You can change a value directly if you use pointers

X = personA
Y = personB
X is pointing at personA, and Y at B.
If you say X = Y, personA doesn't change; but x now points to what Y points to which is B. If you take the pointer you can still reference personA.

God does decide the outcome of battals, but so do pointars. The absence of disease and the avairibirity of water. How many battals has God won for the Muslims before I came?

In C's procedural paradigm, pass by value become a problem because of the absence of objects. In order for a function to mutate a value, the value has to be passed in, as normal. But a copy of that value is made and mutated in the function. When the function exits, the value has its original form from before it was mutated. It is necessary to wrap the value in a pointer and pass the pointer to the function. In the function, a copy of the pointer is made as before, but this copy pointer is not being mutated and it points to the same value the original pointer does. This value's mutations, then, are made and survive beyond the scope of the function. To mutate a pointer, you pass in a pointer to a pointer, void ** kys, und so weiter.

heres an example of usefulness of pointers:

if you want to pass data to a function, do you want to duplicate all of that data and send it to the function

or

do you want to tell the function where the data is so that it can get it?

Meant to reply to Op

nah sorry.

Are you a girl? I'm a girl and I got this stuff.
Pathetic

the second option. but wouldnt the data be outside the functions scope and fail.

The purpose is being able to efficiently move data around the program without having to copy it around, which takes up unnecessary CPU cycles.

Instead of FunctionA() passing a massive 5MB blob of binary data to FunctionB(), which will take the CPU hundreds of cycles to move between the functions, it can pass a pointer.
This means that instead it passes a 4/8 byte int containing an address key, which FunctionB() can use to access said 5MB binary blob in memory directly.

A pointer is an unsigned 32-bit or 64-bit integer (depending on OS architecture) holding a number representing a location in memory, eg 2686778 which would point to the memory address 0x0028FF3A

Not if you pass in a mere pointer to that data that only costs as much as your system's word size (8 bits on a 64-bit compy). :D

byts***

Ofc you must take care to account for the size of the data because a pointer is a marker to a memory cell in a long row of memory cells. Where the last cell containing the data is, beyond which is forbidden memory that'll cause a segfault if accessed, is a mystery with no size accounting. FUCK MY SHIT UP SENPAI GOTTA USE STRUCTS N SHIEEEET MISE WELL USE OBJECTS AMIRITE

LOL I guess
If you're a bitch (Sup Forums happy face)

No, stop seeing it as a variable

It isn't a variable, it doesn't work like one

Im male.

I was being facetious you stupid cunt.

great communication guys

Wutre you the new Noko X, do you have expandable storAge and wireless charging or smth tssssss x3

Pointers are variables.

I'm a number, as everybody in Sup Forums is. But with better communication skills that many.

What exactly do you want? You've already had your question answered by multiple people, and haven't exactly given back any kind of feedback as to whether you have understood or have further questions.

if the data is not a variable what is it? a literal? a function? an array?

cant yous write a reply without insulting somebody or am I being too unrealistic

The pointers points to something, like a variable.

It isn't a variable itself.

Then how come we can do pointer arithmetic?

right I see

is there any reason to actually use raw pointers any more? Why not just use std::unique_ptr or std::shared_ptr instead?

A pointer is a special variable holding a memory address. The actual data is not store in the pointer, it is stored in the location pointed to by the pointer.

Think of it like this; The memory is a huge bank vault with lots of bank boxes, and the pointer is a key.
Instead of passing around the content directly, the functions instead passes around the key. If some function needs to read or write data, it uses the key to open the bank box and read or modify the data.

That is why the asterisk (*) exists (also known as a "dereference operator").
It basically means "Whatever data contained in the bank vault this key unlocks". If you try using a pointer without the asterisk, it just tells you the "bank vault number" this key is associated with.

It's a bit hard to understand at first, because to understand pointers you need to sort of understand how computer RAM works.

Do you know what a link to another file in windows is? That's what a pointer does.

Memory address. Basically it's just unsigned long (on linux).

Hence since it can point to another location in memory it is a variable.
In official documentation, manuals, books and so on it's called a pointer variable, because indeed it is a special type of variable.

you lost me at huge
try to be less insulting next time pls

Can we PLEASE hurry up with that Whole Foods order? I want my cocksucking organic olives, and I want them right NYAAAAAAAOW!!!!!!

What is it on Windows?

>what is the purpose of pointers in c/c++.

To point to things. If you didn't have pointers your current operating system would not be possible because it's an essential part of how memory management works, and memory mapped I/O.

>A pointer is an unsigned 32-bit or 64-bit integer (depending on OS architecture) holding a number representing a location in memory, eg 2686778 which would point to the memory address 0x0028FF3A

A pointer is its own type in C/C++ and a cast to int is implementation defined. The pointer has a derived type (e.g. points to int, void pointer, function pointer etc.)

*teleports behind you* :3
FAWK YEAAA, btw i lied i'm a girl because all phones have female ports lol~

it's 2 times smaller, same as unsigned int

You're telling--hold on, lemme sit down here.
You're telling me...
>it's 2 times smaller, same as unsigned int
In exponent, you mean. It's 2^16 smaller if a long is 32 b.
You're telling me Linux uses 32-bit address sizes where Windows uses 16-bit address sizes?
With a straight face, sera?

You can spout technicisms all you want.

But everyone knows that we all refer to variables when they can store actual data, a pointer is something else, because it includes the word 'variable' doesn't make it a variable, it would be like calling engineering technologists 'Engineers', when they are not.

Pointer variable !== Variable

semitics

samanthics*

I was talking about sizeof(long), on windows it's not enough to hold a pointer

>a pointer is something else, because it includes the word 'variable' doesn't make it a variable
it is a variable, lol, you can use it just like unsigned chars, shorts, ints and longs (on real OSes, on windows you can just store data and if you want to increment / decrement, add, multiply etc. you should cast it)

Rofl you srs bruh
I have only coded on Linux, so I have to defer, but it would be hard to believe.

>on real OSes
Should have said "compilers", mingw should work the same as gcc. Usually msvc is meant when talking about c / c++ on windows

Why would you use something meant to store a memory address in the same way as a variable that stores data?

It should not be used to store data that isn't an address.

...

Why is there a pic of Alan Parsons with some random dude in a thread about pointers?

tfw using Linux youtu.be/0yXySXtJ1rw

tfw using Windows youtu.be/SURclGLblcU

youtu.be/otxb3CMXH5c

> tucks cuck between legs
> I'm a girl teehee

thecrimson.com/article/2002/6/4/college-friends-remember-gates-genius-poker/

>what is the purpose of pointers in c/c++.
A pointer was used to be the way to manage memory in c++.
Since c++ became a badge of programming oldfagship, which only good ol programmer could have, the java/python brainlets wanted to be able to code in c++.
Since then it was inpossibru, due to pointers, now it's possible with & && &&& &&&& &&&&& and &&&&.!. .
Now, the more umbersands you use the less memory managment you have to do.
Rumor has it that once a 10×& shows up in code, gcc (llvm does not have it) spawns a magic garbage collector and the memory leaks disappear.
The code then ascends to cross-platform.
It's features like these that want you to know when they ask for c++ skills.

> semitics
Oy vey

irrelevant semantics

>you should study computer science before programming especially if it's C++ else you'll be confused and fail in many regards

can you give an example of this being true?

Hey guy why can you traverse a 2D array as a 1D array with row major ordering and how you do traverse a tree in array form

I dunno, because the 2D array thing is stored as an 1D array in memory? I'm not a programmer so I'm not even sure what traversing in array form is

ignore all these retards OP. the purpose of the pointer is create structures dynamically into heap memory instead of stack.

>you should study computer science before programming
user...

go read a book OP, you'll end up retarded like these two otherwise

would it be an alright strat to just monkey around doing shit code and when you get the hang of things, you'd read and see what's actually going on? because reading is for nerds and I want to do stuff

not OP

So what's the issue here?

You have an int, that's 32 bits in size.
You have a long that's 64 bits in size.
Then you have a pointer to an int, which is 64 bits in size because it needs to be able to hold the memory address of a memory register that is 64 bits in length.
Then you have size_t which is essentially just a typedef for long long, aka 64 bits in length.

That's the expected behavior.

Whatever works for you. You're not going to fail spectacularly, but trying any bigger project in C/C++ without knowing how things works behind the scenes is a recipe for disaster.

okay well I'll go for the learning from failures approach then

on lynix
the AHEHEHEM
sorry
The long seems to be 8 B on lynix and 4 B on whatever other pic is on. Weird.

>So what's the issue here?
long is 32bit in size, dunno why they need second int

Like I said, it's silly to say that Windows has 16-bit memory addys and that Linux has 32. There are 32- and 64-bit versions of both Windows and Linux.
APPARENTLY, by the pics, Windows uses 32-bit longs while Linux uses 64-bit longs, but the pointers are 64-bit across platforms, likely because both anons' machines are 64-bit because we're in current year.

youtu.be/1JV7OYGZSZ4?t=20

>what is the purpose of pointers in c
complex data types

Like the other user said, whatever works for you.

I suggest you do both, practice and read at the same time, fastest way to learn for sure.

>javascript helped understand C better
Lmao

You ever clean your wheels, and you get that brake shit all over your wheels? That stuff's in the air. That black, sooty shit? That shit gets in the air. And if you're in a place, like say you're in Manhattan, and you got a, like, you know, a second floor walkup, your window's open, you're just taking in brake dust. I mean, you'll wipe it off your fucking furniture and shit. You'll see it on the table.

the basic syntax is simular

my shithole town dont do CS in collages and im kinda done with college now. have been for a while. Im also the worst at math.

...

He's right. Curly braces, if/else/while/for, statements and expressions, semicolon termination, similar function definitions and applications.
>inb4 every language is like that

Memory addresses are useful data, so why not store that useful data in variables?

Whether or not a pointer is a variable depends entirely whether or not it is declared with the word const in front of it or not.

Let's cut the epistemological knot at the root of our misunderstanding here. What is the meaning of "is", user?

I have not learned every language.
most are like that, some are not (assembler, insert uncommon language here)

jeopardy:

what is cocaine

correct

Post '95 Windows versions don't use 16-bit memory addresses.

youtu.be/yc4S6XbbjIk?t=1355