Pointers what is your address dude

Learn pointers in C/C++ with clear examples. Understand variables, addresses, and pointer syntax to master this essential programming concept.

2 min read 343 words

Pointers, heart of C, C++. If you belong to an engineering branch and learned C as a subject, then this may be the last chapter of your curriculum. This is the stepping stone of a programming career. This is the chapter, or I can say love for the pointer, that will differentiate a passionate coder from a normal coder.

Even if you work with higher level languages or going for web, you can't deny the existence of pointers in your life. It's just that you are not touching them directly, but they are there every time. Now, I directly jump to basic understanding of the topic.

If I give a definition, then a pointer is a variable which saves the address of another variable. It is useful when you want to share data. A pointer is like a lightweight variable which saves memory and does normal tasks for us like anything.

A pointer consumes two bytes of memory to save an address. It doesn't matter which type of pointer you are using. Here we directly jump to the example

int value = 10; // declaring and assigning simple integer variable.

int *ptr = &value; // declaring and assigning simple pointer variable

print value; //printing value

print *ptr; // printing pointer value

print ptr; // printing pointer

Result of this comes somewhat like 10, 10, 8000. Here you can easily understand the first and second values give the value of the integer, while the last is something else. It is the address of the integer, the place in memory where the pointer is stored. Here value is called the pointee, a variable pointed to by the pointer.

Here are a few things you have to care about: you can't assign a value directly to the pointer, like *ptr = 10; if a value is not assigned, don't try to fetch the value of that pointer, meaning if *ptr is not assigned, then it will give an exception. Defining a pointer and defining a pointee is a completely different operation, and both are necessary.

This is a very basic understanding of pointers. I will try to go somewhat deeper with this in upcoming write-ups. If you want to know something more about pointers, you can always click here.

Frequently Asked Questions

What is a pointer and why is it important in C/C++?

A pointer is a variable that stores the memory address of another variable. It's a fundamental concept in C and C++ that differentiates skilled programmers from beginners, as it enables efficient data sharing and memory management. Pointers are also present in higher-level languages, though often handled indirectly behind the scenes.

How much memory does a pointer consume?

A pointer consumes two bytes of memory to store an address, regardless of the data type it points to. This makes pointers lightweight variables that can efficiently reference other data without consuming much memory themselves.

What's the difference between the asterisk (*) and ampersand (&) operators with pointers?

The ampersand (&) operator gets the address of a variable (used when creating a pointer), while the asterisk (*) operator dereferences a pointer to access the value it points to. For example, `int *ptr = &value;` stores the address of value, and `*ptr` retrieves that value.

Can I directly assign a value to a pointer variable?

No, you cannot directly assign a value to a pointer using syntax like `*ptr = 10;` without first initializing the pointer to point to an existing variable. Attempting to dereference an uninitialized pointer will cause an exception, so you must define both the pointer and the pointee (the variable it points to).

What happens when I print a pointer versus dereferencing it?

Printing a dereferenced pointer (`*ptr`) gives you the value stored in the variable it points to, while printing the pointer itself (`ptr`) gives you the memory address where that variable is located. This distinction is crucial for understanding how pointers work in memory.

Share this article