Pointers and addresses

Let's take a closer look at pointers and addresses.

What is an address?

An address is an address in memory. Imagine memory like a road that stretches straight from left to right. And it is divided into sections for each street address.

For CPUs that can handle 64-bit addresses, the address is from "0" to "2 to the 64th power -1". The unit is bytes. It starts from 0.

| | | | ... | | |
0 1 2 3 "2 to the 64th power --2" "2 to the 64th power --1"
(Unit is bytes)

Imagine if the virtual memory that your application sees is like this, aside from whether the actual physical memory is so widely available.

This is the area where application data can be stored. It's like a very big desk for working.

What is a pointer?

A pointer is a variable that stores an address. Also known as a pointer variable.

"Well, that's it?"

Yes. The commentary is over (laughs). All you have to do is learn the grammar.

Declaration of pointer variables

Let's declare a pointer variable. To declare a pointer variable, use "type name * variable name;".

// Declaration of pointer type variable of int32_t
int32_t * num_ptr;

// Declaration of pointer type variable of int64_t
int64_t * num_ptr;

// Declaration of a pointer type variable of float
float * num_ptr;

// Declaration of double pointer type variable
double * num_ptr;

// Declaration of pointer type variable of struct myapp_book
struct myapp_book * book_ptr;

// Declaration of pointer type variable of union myapp_value
union myapp_value * value;

Look at the rules. Add "*" after the actual model name.

int32_t pointer type, int64_t pointer type, float pointer type, double pointer type, structure and pointer type of union.

As shown below, I think there is a reference book that describes "*" just before the variable name. This is correct with this.

int32_t * num_ptr;

The C99-compatible introduction to C language consistently treats the understanding of "*" as a pointer type. In a sense, I feel that this is not a strictly correct explanation of C language, but in practice, there is no contradiction and it is easy to understand.

Get the address

Use the address operator "&" to get the address.

&Variable name

Let's get the address and assign it to the pointer. Let's write an example of an int32_t pointer, a double pointer, and a structure pointer. The address value can be output with the "%p" format specifier of the printf function.

#include <stdint.h>
#include <stdio.h>

// Structure
struct myapp_book {
  int32_t id;
  const char * name;
  int32_t price;
};;

int main (void) {
  // Actual value of int32_t
  int32_t numi = 5;
  
  // Declare a pointer type pointer of int32_t to get the address and assign it
  int32_t * numi_ptr = & numi;

  // Output address
  printf("numi_ptr%p\n", numi_ptr);
  
  // Actual value of double
  double numd = 2.5;

  // Declare a double pointer type pointer to get the address and assign it
  double * numd_ptr = & numd;

  // Output address
  printf("numd_ptr%p\n", numd_ptr);
  
  // Actual value of myapp_book
  struct myapp_book book = {id: 1, name: "C99 Book", price: 1500};
  
  // Declare a pointer type pointer of myapp_book to get the address and assign it
  struct myapp_book * book_ptr = & book;
  
  // Output address
  printf("book_ptr%p\n", book_ptr);
}

This is the output result in my environment. The address is output in hexadecimal.

numi_ptr 0x7fff6d615824
numd_ptr 0x7fff6d615818
book_ptr 0x7fff6d615800

Extract the entity from the pointer

Let's get the actual value (called an entity) from the pointer. This is the opposite of getting the address.

To retrieve the entity, add "*" before the variable name.

* Pointer variable

Let's write a sample to get the entity.

#include <stdint.h>
#include <stdio.h>

// Structure
struct myapp_book {
  int32_t id;
  const char * name;
  int32_t price;
};;

int main (void) {
  // Actual value of int32_t
  int32_t numi = 5;
  
  // Declare a pointer type pointer of int32_t to get the address and assign it
  int32_t * numi_ptr = & numi;
  
  // Get the entity and output
  int32_t numi_real = * numi_ptr;
  printf("numi_real%d\n", numi_real);
  
  // Actual value of double
  double numd = 2.5;

  // Declare a double pointer type pointer to get the address and assign it
  double * numd_ptr = & numd;

  // Get the entity and output
  double numd_real = * numd_ptr;
  printf("numd_real%f\n", numd_real);
  
  // Actual value of myapp_book
  struct myapp_book book = {id: 1, name: "C99 Book", price: 1500};
  
  // Declare a pointer type pointer of myapp_book to get the address and assign it
  struct myapp_book * book_ptr = & book;
  
  // Get the entity and output
  struct myapp_book book_real = * book_ptr;
  printf("book_real id:%d, name:%s, price:%d\n", book_real.id, book_real.name, book_real.price);
}

This is the output result. It is the actual value.

numi_real 5
numd_real 2.500000
book_real id: 1, name: C99 Book, price: 1500

Access the member variables of the structure directly from the pointer

Use "->" instead of "." To access the structure's member variables directly from the pointer.

#include <stdint.h>
#include <stdio.h>

// Structure
struct myapp_book {
  int32_t id;
  const char * name;
  int32_t price;
};;

int main (void) {
  
  // Actual value of myapp_book
  struct myapp_book book = {id: 1, name: "C99 Book", price: 1500};
  
  // Declare a pointer type pointer of myapp_book to get the address and assign it
  struct myapp_book * book_ptr = & book;
  
  // Output address
  printf("id:%d, name:%s, price:%d", book_ptr->id, book_ptr->name, book_ptr->book_ptr%p\n ", book_ptr);
}

What is the variable name of the pointer when writing in practice?

When writing in practice, it is not so common to add "_ptr" like "book_ptr". In my case, I don't add "_ptr" except for the exception that I want to explicitly indicate that it is a pointer.

// Variable name sample when allocating memory dynamically for structure
struct myapp_book * book = calloc (sizeof (struct myapp_)book), 1);

Memory of structure data using calloc function and sizeof operator It is allocated and assigned to the pointer of the structure.

Where is the pointer operation explained?

Pointer operations will be explained soon.

Where are general pointers explained?

General-purpose pointers will be explained soon.

Associated Information