Usuario: anónimo

Usuarios en línea: 1
Invitados: 1

FAQ

Hilo

58m
Declaración e inicialización de punteros y typedef.
U(marcelinux) 2018-03-02 08:59:05

Después de completar el curso propuesto en esta web me he propuesto complementarlo con el libro de Bjarne Stroustrup, The C++ Programming language.

En el capítulo 5 se proponen unos ejercicios, que transcribo.

Me gustaría saber si los he realizado correctamente.

No se me ocurre cómo serían las dos últimas definiciones. Si alguien pudiera

explicarme cómo deberían ser, le estaría muy agradecido.

<code>

/* The C++ Programming language

 * Ejercicios del capítulo 5.

 *

 * ej_01   Write declarations for the following:

 *         a pointer to a character,

 *         an array of 10 integers,

 *         a reference to an array of 10 integers,

 *         a pointer to an array of character strings,

 *         a pointer to a pointer to a character,

 *         a constant integer,

 *         a pointer to a constant integer

 *         and a constant pointer to an integer.

 *         Initialize each one.

 *

 * ej_03   Use typedef to define the types:

 *         unsigned char,

 *         const unsigned char,

 *         pointer to integer,

 *         pointer to pointer to char,

 *         pointer to arrays of char,

 *         array of 7 pointers to int,

 *         pointer to an array of 7 pointers to int

 *         and array of 8 arrays of 7 pointers to int.

 */

#include iostream

using namespace std;

void ej_01()

{

   char c = 'M';

   cout << "char c = " << c << '\n';

   // a pointer to a character

   char* pc = &c;

   cout << "char* pc = " << pc << " apunta a c = " << *pc << '\n';

   // an array of 10 integers

   int arr_i[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 32};

   cout << "arr_i[10] ... " << arr_i[0] << '\n';

   // a reference to an array of 10 integers

   int (&ref_i)[10] = arr_i;

   cout << "ref_i = " << ref_i << " hace referencia a " << ref_i[0] << '\n';

   // a pointer to an array of character strings

   char* arr_c[] = {"primero",

                  "segundo",

                  "tercero"};

   cout << "arr_c[] ... " << arr_c[0] << " .. o .. " << *arr_c << '\n';

   // a pointer to a pointer to a character

   char** ppc = arr_c;

   cout << "ppc = " << ppc << " que apunta a " << *ppc << '\n';

   // a constant integer

   const int ci = 356;

   cout << "ci = " << ci << '\n';

   // a pointer to a constant integer

   const int* pci = &ci;

   cout << "pci = " << pci << " que apunta a " << *pci << '\n';

   // a constant pointer to an integer

   int ii = 88;

   int *const icons = &ii;

   cout << "icons = " << icons << " que apunta a " << *icons << '\n';

}

void ej_03()

{

   // unsigned char

   typedef unsigned char uchar_t;

   uchar_t uc = 179;

   cout << "unsigned char 179 se representa con " << uc << '\n';

   // const unsigned char

   typedef const unsigned char cuchar_t;

   cuchar_t cuc = 179;

   cout << "const unsigned char 179 se representa con " << cuc << '\n';

   //pointer to integer

   typedef int* pint_t;

   int i = 6;

   pint_t pi = &i;

   cout << "pointer to integer sobre i = 6 está en " << pi << " y contiene " << *pi << '\n';

   //pointer to pointer to char

   typedef char** ppchar_t;

   char ch1 = 'c';

   char* pch1 = &ch1;

   ppchar_t ppch1 = &pch1;

   cout << "pointer to pointer to char está en " << ppch1

      << " y contiene " << *ppch1

      << " que es, a su vez " << **ppch1 << '\n';

   // pointer to arrays of char --->>>  es equivalente a pointer to pointer to char

   //array of 7 pointers to int

   int i7_1, i7_2, i7_3, i7_4, i7_5, i7_6, i7_7;

   i7_7 = i7_6 = i7_5 = i7_4 = i7_3 = i7_2 = i7_1 = 101;

   typedef int* pint7_t;

   pint7_t i7[7] = {&i7_1, &i7_2, &i7_3, &i7_4, &i7_5, &i7_6, &i7_7};

   cout << "array of 7 pointers to int: " << i7[0] << " => " << *i7[0]

         << ", " << i7[1] << " => " << *i7[1]

         << ", " << i7[2] << " => " << *i7[2]

         << ", " << i7[3] << " => " << *i7[3]

         << ", " << i7[4] << " => " << *i7[4]

         << ", " << i7[5] << " => " << *i7[5]

         << ", " << i7[6] << " => " << *i7[6] << '\n';

   //pointer to an array of 7 pointers to int

   //array of 8 arrays of 7 pointers to int

}

int main()

{

   ej_01();

   ej_03();

   return 0;

}

</code>

_______________________________________________

Lista de correo Cconclase Cconclase@listas.conclase.net

http://listas.conclase.net/mailman/listinfo/cconclase_listas.conclase.net

Bajas: http://listas.conclase.net/index.php?gid=2&mnu=FAQ