Functions, Arrays and Two Pointers | Pointers
C Basics

Course Content

C Basics

## C Basics

1. Introduction
2. Data
3. Operators
4. Control Statements
5. Functions
6. Pointers

# Functions, 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).

Main

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`.

Main

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:

Main

### Are Arrays Merely Pointers?

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

Main

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:

Main

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:

Main

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

Everything was clear?

Section 6. Chapter 5
We're sorry to hear that something went wrong. What happened?