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

book
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).

c

Main

copy
#include <stdio.h>

void Ohm(double R)
{
R = R * 1000;
}

int main()
{
double r = 1.5; // kOhm
printf("The value of resistance before using function: %f\n", r);

Ohm(r);

printf("The value of resistance after using function: %f", r);

return 0;
}
12345678910111213141516171819
#include <stdio.h> void Ohm(double R) { R = R * 1000; } int main() { double r = 1.5; // kOhm printf("The value of resistance before using function: %f\n", r); Ohm(r); printf("The value of resistance after using function: %f", r); return 0; }

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

copy
#include <stdio.h>

void Ohm(double* R)
{
// dereferencing the entered address and changing the object it points to
*R = *R * 1000;
}

int main()
{
double r = 1.5; // kOhm

printf("The value of resistance before using function: %f\n", r);

Ohm(&r);

printf("The value of resistance after using function: %f\n", r);

return 0;
}
1234567891011121314151617181920
#include <stdio.h> void Ohm(double* R) { // dereferencing the entered address and changing the object it points to *R = *R * 1000; } int main() { double r = 1.5; // kOhm printf("The value of resistance before using function: %f\n", r); Ohm(&r); printf("The value of resistance after using function: %f\n", r); return 0; }

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

copy
#include <stdio.h>
#include <stdlib.h>

int* func()
{
int* x = (int*)malloc(sizeof(int));

printf("Address into function: %p\n", x);

return x;
}

int main()
{
int* pointerToFunc = func();

printf("Address after using function: %p\n", pointerToFunc);
return 0;
}
1234567891011121314151617181920
#include <stdio.h> #include <stdlib.h> int* func() { int* x = (int*)malloc(sizeof(int)); printf("Address into function: %p\n", x); return x; } int main() { int* pointerToFunc = func(); printf("Address after using function: %p\n", pointerToFunc); return 0; }

Are Arrays Merely Pointers?

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

c

Main

copy
#include <stdio.h>

int main()
{
int x = 100;

int* pX = &x;

printf("Address: %p | Adress + 1: %p", pX, pX + 1);

return 0;
}
123456789101112
#include <stdio.h> int main() { int x = 100; int* pX = &x; printf("Address: %p | Adress + 1: %p", pX, pX + 1); return 0; }

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

copy
#include <stdio.h>

int main()
{
int* pX = NULL; // pointer to `int` type (4 bites)

for (int i = 0; i < 3; i++)
printf("Address: %p\n", pX + i);

return 0;
}
1234567891011
#include <stdio.h> int main() { int* pX = NULL; // pointer to `int` type (4 bites) for (int i = 0; i < 3; i++) printf("Address: %p\n", pX + i); return 0; }

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

copy
#include <stdio.h>

int main()
{
int array[] = {1,2,3,4,5};

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

for(int i = 0; i < 5; i++)
printf("Value: %d | Address of element with index %d: %p\n", *(array + i), i , &array[i]);

return 0;
}
12345678910111213
#include <stdio.h> int main() { int array[] = {1,2,3,4,5}; printf("Address of array: %p\n", array); for(int i = 0; i < 5; i++) printf("Value: %d | Address of element with index %d: %p\n", *(array + i), i , &array[i]); return 0; }

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

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 6. Kapitel 5

Spørg AI

expand
ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

some-alt