Introduction to Pointers


Pointers

A pointer is a variable that can point to another variable. The pointer allows having access to another variable without creating or copying a variable. The secret of using pointer is to know all the time where they are pointing. A pointer is equivalent to an arrow that points to something or someone in real life. In the code below shows how to declare a pointer. Observe that when a pointer is declared, it is necessary to indicate the data type of the variable that the pointer will point; this will indicate the type of operations that can be performed with the pointer.
Un puntero es una variable que a punta a otra variable. El pointer permite tener acceso a otra variable apuntando a ella sin necesidad de crear otra variable. El secreto de usar punteros es saber a donde están apuntando todo el tiempo. Un puntero es equivalente a una flecha en la vida real que indica algo o a alguien. En el código mostrado debajo se muestra como declarar un puntero. Observe que siempre que se declara un puntero es necesario indicar a qué tipo de variable apuntará; esto con el fin de saber que operaciones y funciones se realizarán con el puntero.

Program.cpp
void Program::Window_Open(Win::Event& e)
{
     int x = 10; // it declares a variable
     int *y = nullptr; // it declares a pointer

     double z = 5.0; // it declares a variable
     double *w = nullptr; // it declares a pointer
}

Tip
A pointer can initially point to a neutral place called NULL. A pointer can initially point to a variable; later on, the same pointer can point to another variable. As a matter of fact, this is what makes pointers important, the ability to point to different variables.
Un puntero puede apuntar inicialmente a un valor neutral conocido como NULL. Un puntero puede apuntar inicialmente a una variable para accesarla; más adelante, el mismo puntero puede apuntar a otra variable. Incluso este es lo que hace importante los punteros, que puedan apuntar a diferentes variables.

Tip
When an ampersand is placed before a variable, the variable behaves as a pointer. In the code shown below, &x behaves as a pointer therefore the pointer can be set to &x.
Al colocar un amperson antes de una variable, la variable se comporta como un puntero. En el código de abajo, &x se comporta como puntero por lo que el puntero se puede fijar en &x.

Program.cpp
void Program::Window_Open(Win::Event& e)
{
     int x = 10; // it declares an integer variable
     int *y = &x; //the pointer y is pointing to x
}

Tip
When an asterisk is placed before a pointer, the pointer behaves like the variable it is pointing. In the code shown below, *y and x represent the same variable.
Cuando un asterisco se coloca antes de un puntero, el puntero se comporta como la variable a la que apunta. En el código de abajo, *y y x representan la misma variable.

Pointer

Problem 1
Indique whether the following code is right or not.
Indique si el código es correcto o incorrecto.

Program.cpp
void Program::Window_Open(Win::Event& e)
{
     int x = 100;
     int y = x;
}

Problem 2
Indique whether the following code is right or not.
Indique si el código es correcto o incorrecto.

Program.cpp
void Program::Window_Open(Win::Event& e)
{
     double x = 100.0;
     double* y = x;
}

Problem 3
Indique whether the following code is right or not.
Indique si el código es correcto o incorrecto.

Program.cpp
void Program::Window_Open(Win::Event& e)
{
     double x = 110.0;
     double* y = &x;
}

Problem 4
Indique whether the following code is right or not.
Indique si el código es correcto o incorrecto.

Program.cpp
void Program::Window_Open(Win::Event& e)
{
     double w = 110.0;
     int* y = &w;
}

Problem 5
Indique whether the following code is right or not.
Indique si el código es correcto o incorrecto.

Program.cpp
void Program::Window_Open(Win::Event& e)
{
     int age = 80;
     int* y = *age;
}

Problem 6
Indique whether the following code is right or not.
Indique si el código es correcto o incorrecto.

Program.cpp
void Program::Window_Open(Win::Event& e)
{
     double z = 30.5;
     double *p = &z;
     *p = p*3;
}

Problem 7
Indique whether the following code is right or not.
Indique si el código es correcto o incorrecto.

Program.cpp
void Program::Window_Open(Win::Event& e)
{
     bool isBig = true;
     bool *isSmall = &isBig;
     bool *q = isBig;
     bool *w = isSmall;
}

Problem 8
Compute the output of the program.
Calcule la salida del programa.

Program.cpp
void Program::Window_Open(Win::Event& e)
{
     wchar_t text[128];
     int i = 500;
     int* z = nullptr;
     z = &i;
     *z = 1010;
     _snwprintf_s(text, 128, _TRUNCATE, L"%d, %d", *z, i);
     this->MessageBox(text, L"Hello", MB_OK);
     i = 50;
     _snwprintf_s(text, 128, _TRUNCATE, L"%d, %d", *z, i);
     this->MessageBox(text, L"Hello", MB_OK);
}

Problem 9
Compute the output of the program.
Calcule la salida del programa.

Program.cpp
void Program::Window_Open(Win::Event& e)
{
     wchar_t text[128];
     double x = M_PI;
     double* p = &x;
     *p = (*p)*2;
     _snwprintf_s(text, 128, _TRUNCATE, L"%g, %g", *p, x);
     this->MessageBox(text, L"Hola", MB_OK);
}

Problem 10
Compute the output of the program.
Calcule la salida del programa.

Program.cpp
void Program::Window_Open(Win::Event& e)
{
      wchar_t text[128];
      double x = 1.1;
      double* a = &x;
      double* b = &x;
      x *= 5;
      _snwprintf_s(text, 128, _TRUNCATE, L"%g - %g - %g", *a, x, *b);
      this->MessageBox(text, L"Hola", MB_OK);
}

Problem 11
Compute the output of the program.
Calcule la salida del programa.

Program.cpp
void Programa::Window_Open(Win::Event& e)
{
     wchar_t text[128];
     double x = 1.1;
     double* a = &x;
     double* b = &x;
     x *= 5;
      _snwprintf_s(text, 128, _TRUNCATE, L"%x - %g - %X", a, x, b);
     this->MessageBox(text, L"Hola", MB_OK);
}

Problem 12
Compute the output of the program.
Calcule la salida del programa.

Program.cpp
void Program::Window_Open(Win::Event& e)
{
     wchar_t text[128];
     int x = 3;
     int y = 2;
     int* px = &x;
     int* py = &y;
      _snwprintf_s(text, 128, _TRUNCATE, L"%d / %d / %d / %d", x, *px, *py, (*px)*(*py));
     this->MessageBox(text, L"Hola", MB_OK);
}


Tip
  • An asterisk is used to declare a pointer
  • An asterisk is used before a pointer so that it behaves like a variable
  • An & is used before a variable so that it behaves like a pointer

  • Un asterisco es usado para declarar un puntero
  • Un asterisco es usado antes de un puntero para que este se comporte como una variable
  • Una & es usada antes de una variable para que este se comporte como un puntero

© Copyright 2000-2026 Wintempla selo. All Rights Reserved. abr. 17 2026. Home