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.c
12345678910111213141516#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; }
An 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, you 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.c
1234567891011121314151617#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 you 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.c
123456789101112131415#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; }
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.c
12345678910#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; }
You've projected three steps ahead. It's apparent from the derived addresses that there's a clear hierarchy.
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.c
1234567891011#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; }
You don't traverse directly through the array. You solely utilize its address, specifically the address of its initial element.a
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 2.63
Functions, Arrays and Two Pointers
Swipe to show menu
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.c
12345678910111213141516#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; }
An 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, you 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.c
1234567891011121314151617#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 you 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.c
123456789101112131415#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; }
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.c
12345678910#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; }
You've projected three steps ahead. It's apparent from the derived addresses that there's a clear hierarchy.
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.c
1234567891011#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; }
You don't traverse directly through the array. You solely utilize its address, specifically the address of its initial element.a
Thanks for your feedback!