Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a variable in C/C++ work?

Tags:

How does a variable in C/C++ work?

I mean, a pointer stores an address from a variable and then you have to dereference it to access the object to which it refers, so I think that a variable is a pointer that is dereferenced automatically when used... does that make any sense?

like image 375
Hugo Avatar asked Aug 13 '10 12:08

Hugo


People also ask

How does a variable work?

A variable is a symbolic name for (or reference to) information. The variable's name represents what information the variable contains. They are called variables because the represented information can change but the operations on the variable remain the same.

What does variable mean in C?

Variable is basically nothing but the name of a memory location that we use for storing data. We can change the value of a variable in C or any other language, and we can also reuse it multiple times.

What are the 3 types of variables in C?

There are many types of variables in c: local variable. global variable. static variable.


1 Answers

A variable is an abstraction (a convenient name) for a memory position on the computer. In C/C++ if the variable is of type int it will be a convenient name for a memory address holding an integer value.

And a variable is not a pointer automatically dereferenced. A variable just holds the value it is supposed to hold. If it is a pointer, it will hold a memory address, if it is an integer it will hold an integer value, if it is a float, it will hold a floating point number... And so on and so forth...

like image 70
Pablo Santa Cruz Avatar answered Sep 17 '22 14:09

Pablo Santa Cruz