WebNovels

Chapter 8 - Chapter 8 — The Memory Vault

The computer screen dimmed slightly.

Then a new message appeared.

NEXT MODULE: MEMORY STORAGE

VARIABLE MEMORY MAP DETECTED

POINTER ACCESS REQUIRED

Pran stared at the words.

"Memory map?"

The cursor blinked slowly.

_

Pran leaned closer to the screen.

"Okay… computers store data somewhere. I know that."

Every variable he had created so far — names, numbers, guesses — had to be stored somewhere inside the computer.

But where exactly?

The computer answered.

The screen suddenly changed.

A strange diagram appeared.

MEMORY VAULT

--------------------------------

Address Value

--------------------------------

1001 ?

1002 ?

1003 ?

1004 ?

--------------------------------

Pran's eyes widened.

"Wait… those are memory locations."

Every piece of data inside a computer is stored in memory addresses.

Think of it like houses on a street.

Example:

House Address Value Inside

1001 7

1002 25

1003 42

The address tells the computer where the data lives.

The value tells the computer what the data is.

Pran nodded slowly.

"So every variable has an address."

Exactly.

When you write this:

int age = 25;

The computer stores 25 somewhere in memory.

Something like this:

Address Value

1002 25

Pran opened the code editor again.

He typed a small program.

#include

int main() {

int age = 25;

printf("Age: %d\n", age);

return 0;

}

He ran it.

The output appeared.

Age: 25

Nothing surprising.

But what if we want to know where the value is stored?

In C, there is an operator for that.

&

The & symbol means:

"Give me the memory address of this variable."

Pran updated the program.

#include

int main() {

int age = 25;

printf("Age: %d\n", age);

printf("Address: %p\n", &age);

return 0;

}

He ran the program again.

The output appeared.

Age: 25

Address: 0x7ffd4a92

Pran blinked.

"That's the address?"

Yes.

It looks strange, but that number tells us where the value is stored in memory.

The computer screen suddenly flickered again.

The memory vault diagram updated.

MEMORY VAULT

--------------------------------

Address Value

--------------------------------

0x7ffd4a92 25

--------------------------------

Pran leaned back.

"Okay… that's actually cool."

But then another message appeared.

POINTER SYSTEM REQUIRED

Pran scratched his head.

"Pointer system?"

A pointer is simply a variable that stores a memory address.

Instead of storing numbers like:

25

10

50

It stores something like:

0x7ffd4a92

Which is the address of another variable.

Pran typed a new program.

#include

int main() {

int age = 25;

int *ptr;

ptr = &age;

printf("Age value: %d\n", age);

printf("Address of age: %p\n", &age);

printf("Pointer value: %p\n", ptr);

return 0;

}

Let's break it down.

This line creates the pointer.

int *ptr;

The * tells the computer:

This variable stores a memory address.

Then we assign it.

ptr = &age;

This means:

Store the address of age inside ptr.

Pran ran the program.

The output appeared.

Age value: 25

Address of age: 0x7ffd4a92

Pointer value: 0x7ffd4a92

Pran pointed at the screen.

"Hey! Those are the same!"

Exactly.

The pointer stores the same address as &age.

The computer screen flickered again.

The memory vault diagram updated.

MEMORY VAULT

--------------------------------

Address Value

--------------------------------

0x7ffd4a92 25

0x7ffd4a96 -> 0x7ffd4a92

--------------------------------

Pran smiled.

"So the pointer points to the value."

Exactly.

But here is the really cool part.

A pointer can also access the value it points to.

For that, we use the * operator again.

Pran typed another line.

printf("Value using pointer: %d\n", *ptr);

Full program:

#include

int main() {

int age = 25;

int *ptr;

ptr = &age;

printf("Age: %d\n", age);

printf("Pointer value: %p\n", ptr);

printf("Value using pointer: %d\n", *ptr);

return 0;

}

He ran it.

Output:

Age: 25

Pointer value: 0x7ffd4a92

Value using pointer: 25

Pran laughed.

"So the pointer can access the value too!"

The computer screen flashed again.

Another message appeared.

POINTER SYSTEM VERIFIED

MEMORY VAULT UNLOCKED

The robot on the screen moved again.

[^_^]

/| |\

/ \

Another message appeared.

SYSTEM PROGRESS: 80%

Pran's eyes widened.

"Eighty percent?"

The system was almost fully unlocked.

Then the computer displayed another message.

NEXT MODULE: DATA STORAGE

REQUIREMENT: ARRAY SYSTEM

Pran smiled.

"Arrays."

He had heard of them.

If a variable stores one value, an array stores many values.

The cursor blinked again.

_

Waiting for the next command.

Chapter 9 — The Data Vault

You will learn:

arrays

storing multiple values

accessing array elements

More Chapters