© Slobodan Dmitrović 2021
S. DmitrovićModern C for Absolute Beginnershttps://doi.org/10.1007/978-1-4842-6643-4_25

25. Dynamic Memory Allocation

Slobodan Dmitrović1  
(1)
Belgrade, Serbia
 
So far, we have used pointers that point to regular, statically allocated variables. We used an address-of operator & to assign the address of an existing object to our pointer. Example:
#include <stdio.h>
int main(void)
{
      int x = 123;
      int *p = &x;
      printf("The value of a pointed-to object is: %d\n", *p);
}
Output:
The value of a pointed-to object is: 123
We also showed how a pointer could point to an array:
#include <stdio.h>
int main(void)
{
      int arr[] = {10, 20, 30, 40, 50};
      int *p = arr;
      printf("The first array element is: %d\n", *p);
}
Output: ...

Get Modern C for Absolute Beginners: A Friendly Introduction to the C Programming Language now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.