Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Functions, Arrays and Two Pointers | Pointers
C Basics

Functions, Arrays and Two PointersFunctions, Arrays and Two Pointers

Functions with Pointers

Let's experiment with a basic function to modify the value of our data. For instance, imagine you need a function that converts kilo-Ohms to Ohms (1 kOhm = 1000 Ohm).

c

Main.c

Our attempt to change the value of the r variable was unsuccessful. This is because the function receives a copy of the r variable, not the actual value itself.

To make our program work as intended, we need to pass the address of the r variable to the function. As a result, the Ohm function should accept double* instead of just double.

c

Main.c

Note that we reference the r variable twice. After invoking the Ohm function, the value of r is altered. This is because the function received the original address of the variable r, not a mere copy, and then modified the value at that particular address.

Moreover, a function can return a pointer to an object that it has generated:

c

Main.c

Are Arrays Merely Pointers?

What do you predict will transpire if a number is appended to an address?

c

Main.c

When a number (pX + 1) is added to an address, it yields the address of the subsequent memory cell!

Let's script a loop to navigate the "sequence" of RAM:

c

Main.c

We've projected three steps ahead. It's apparent from the derived addresses that there's a clear hierarchy.

Given that the int type occupies 4 bytes, we progress by 4 bytes with each step. This behavior is strikingly reminiscent of an array!

It seems an array is essentially a fixed address (represented by the array's name) coupled with allocated memory. The indices of the elements represent their offset from the address of the initial element!

This notion can be validated with the following program:

c

Main.c

As observed, we don't traverse directly through the array. We solely utilize its address, specifically the address of its initial element.

Все було зрозуміло?

Секція 6. Розділ 5
course content

Зміст курсу

C Basics

Functions, Arrays and Two PointersFunctions, Arrays and Two Pointers

Functions with Pointers

Let's experiment with a basic function to modify the value of our data. For instance, imagine you need a function that converts kilo-Ohms to Ohms (1 kOhm = 1000 Ohm).

c

Main.c

Our attempt to change the value of the r variable was unsuccessful. This is because the function receives a copy of the r variable, not the actual value itself.

To make our program work as intended, we need to pass the address of the r variable to the function. As a result, the Ohm function should accept double* instead of just double.

c

Main.c

Note that we reference the r variable twice. After invoking the Ohm function, the value of r is altered. This is because the function received the original address of the variable r, not a mere copy, and then modified the value at that particular address.

Moreover, a function can return a pointer to an object that it has generated:

c

Main.c

Are Arrays Merely Pointers?

What do you predict will transpire if a number is appended to an address?

c

Main.c

When a number (pX + 1) is added to an address, it yields the address of the subsequent memory cell!

Let's script a loop to navigate the "sequence" of RAM:

c

Main.c

We've projected three steps ahead. It's apparent from the derived addresses that there's a clear hierarchy.

Given that the int type occupies 4 bytes, we progress by 4 bytes with each step. This behavior is strikingly reminiscent of an array!

It seems an array is essentially a fixed address (represented by the array's name) coupled with allocated memory. The indices of the elements represent their offset from the address of the initial element!

This notion can be validated with the following program:

c

Main.c

As observed, we don't traverse directly through the array. We solely utilize its address, specifically the address of its initial element.

Все було зрозуміло?

Секція 6. Розділ 5
some-alt