Take a variable's address with &, reach the value at that address with *; change variables by passing addresses to functions, and learn pointer arithmetic and how it relates to arrays.
Think of the electrical panels in a building. Each panel has contents (the breakers) and a location (floor and room number). When you tell someone "3rd floor, room 12", you've described the panel without carrying it anywhere. Memory works the same way: every variable has a value and an address. A pointer is a variable that stores an address. On a microcontroller, hardware registers also sit at fixed addresses, and pointers are how you reach them from C. That makes this lesson the key to embedded C.
The address operator & and the dereference operator *
#include <stdio.h>
int main(void) {
int adc = 512;
int *p = &adc;
printf("adc degeri : %d\n", adc);
printf("adc adresi : %p\n", (void *)&adc);
printf("p'nin icerigi: %p\n", (void *)p);
printf("*p : %d\n", *p);
*p = 600;
printf("adc simdi : %d\n", adc);
return 0;
}
adc degeri : 512
adc adresi : 0x7ffc4a3e1b5c
p'nin icerigi: 0x7ffc4a3e1b5c
*p : 512
adc simdi : 600
Address values differ on every run and on every system; on Windows they may also appear without the 0x prefix, padded with zeros. What matters is that the two addresses are the same.
int *p: means "p holds the address of an int". In a declaration, the asterisk is part of the type.&adc: the address of adc. Now you know why we give scanf an&: to show scanf where to write the value.*p: outside a declaration it means "the value at the place p points to".*p = 600;changes the contents of adc directly.%pprints an address; the standard requires the argument to be avoid *. That's why we cast with(void *).
Passing an address to a function
In the functions lesson, sifirla could only change a copy. If we pass the address, the function reaches the real variable. With the same technique we can also get more than one result out of a function:
#include <stdio.h>
void sifirla(int *x) {
*x = 0;
}
void min_max(const int *dizi, int n, int *enk, int *enb) {
*enk = dizi[0];
*enb = dizi[0];
for (int i = 1; i < n; i++) {
if (dizi[i] < *enk) *enk = dizi[i];
if (dizi[i] > *enb) *enb = dizi[i];
}
}
int main(void) {
int sayac = 42;
sifirla(&sayac);
printf("sayac = %d\n", sayac);
int adc[5] = {498, 512, 505, 530, 487};
int en_kucuk, en_buyuk;
min_max(adc, 5, &en_kucuk, &en_buyuk);
printf("Min: %d, Max: %d, Fark: %d\n", en_kucuk, en_buyuk, en_buyuk - en_kucuk);
return 0;
}
sayac = 0
Min: 487, Max: 530, Fark: 43
min_max wrote its two results to the addresses the caller provided. The difference between max and min (43 counts) gives a rough idea of the noise in the signal. The parameter const int *dizi is a promise that "this function only reads the array and doesn't change it"; if you accidentally try to write to it, the compiler reports an error.
Pointer arithmetic and arrays
In most expressions, the name of an array turns into the address of its first element. Adding 1 to a pointer moves the address forward not by 1 byte but by the size of the type it points to. So dizi[i] and *(dizi + i) mean exactly the same thing.
#include <stdio.h>
#include <stdint.h>
int main(void) {
uint16_t ornek[4] = {100, 200, 300, 400};
uint16_t *p = ornek; // &ornek[0] ile ayni
printf("ornek[2] = %d\n", ornek[2]);
printf("*(p + 2) = %d\n", *(p + 2));
printf("Adres farki: %d bayt\n", (int)((char *)(p + 1) - (char *)p));
long toplam = 0;
for (uint16_t *q = ornek; q < ornek + 4; q++) {
toplam += *q;
}
printf("Toplam = %ld\n", toplam);
return 0;
}
ornek[2] = 300
*(p + 2) = 300
Adres farki: 2 bayt
Toplam = 1000
Because uint16_t is 2 bytes, p + 1 is 2 bytes ahead of p. In the loop we advanced the pointer itself instead of an index; you'll see this pattern often in embedded code that processes DMA buffers or bytes coming in over a UART.
When you pass an array to a function, only the address of its first element actually goes across. Inside the function, sizeof(dizi) gives the size of the pointer (8 on most PCs), not of the array. That's why we pass the element count as a parameter along with the array, just as in min_max(adc, 5, ...).
NULL: a pointer that points nowhere
A pointer that doesn't have a valid address yet is given the value NULL. Accessing what a NULL pointer points to crashes the program; so in doubtful cases, check before using it: if (p != NULL) { ... }. In the next lesson we'll see that memory allocation functions return NULL when they fail.
Common mistakes
Using an uninitialised pointer: int *p; *p = 5; writes to a random address. The program may crash or silently corrupt another variable. Give a pointer either a valid address or NULL.
- Forgetting the asterisk when declaring two pointers on one line: in
int *a, b;onlyais a pointer;bis a plain int. The correct form isint *a, *b;. - Mixing up
pand*p:p = 5changes the address,*p = 5changes the value pointed to. The compiler warning "makes pointer from integer" usually points to this mix-up.
Exercise
Write the function void adc_donustur(const int *adc, double *volt, int n). It should convert 10-bit ADC values to volts for a 5 V reference and write them into the second array. Try it with the values 0, 256, 512 and 1023.
Hint: inside the function, volt[i] = adc[i] * 5.0 / 1023; is enough. Define the result array in main and pass its name to the function; an array name already means an address.
Solution
#include <stdio.h>
void adc_donustur(const int *adc, double *volt, int n) {
for (int i = 0; i < n; i++) {
volt[i] = adc[i] * 5.0 / 1023;
}
}
int main(void) {
int adc[4] = {0, 256, 512, 1023};
double volt[4];
adc_donustur(adc, volt, 4);
for (int i = 0; i < 4; i++) {
printf("ADC %4d -> %.3f V\n", adc[i], volt[i]);
}
return 0;
}
ADC 0 -> 0.000 V
ADC 256 -> 1.251 V
ADC 512 -> 2.502 V
ADC 1023 -> 5.000 V
You've learned about addresses, pointers and how they connect to arrays. In the next lesson we'll use pointers to allocate memory at run time and meet dangers such as leaks and "dangling pointers".
// Spot the bug +10 XP
Exactly one line below is wrong. Click the faulty line.
Line 6 assigns an integer to the pointer itself (the address) instead of to the value it points to. This triggers a compiler warning/error and doesn't change r2 either. Correct version: *b = gecici;
// Mini quiz
Click an option; you will see right away, with the reason, whether it is correct or not.
Mark it when you finish: your progress is saved and you earn +20 XP.
Tip: ← → keys let you move between lessons.


