Passing Pointers


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

Program.h
#pragma once //______________________________________ Program.h
#include "resource.h"

class Program: public Win::Dialog
{
public:
     Program()
     {
     }
     ~Program()
     {
     }
     void Tunning(int* n);
protected:
     ...
};

Program.cpp
void Program::Window_Open(Win::Event& e)
{
     int x = 10;
     int *y = &x;
     Tunning(y);
     this->Text = Sys::Convert::ToString(x);
}

void Program::Tunning(int* n)
{
     (*n) = 5;
}

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

Program.h
#pragma once //______________________________________ Program.h
#include "resource.h"

class Program: public Win::Dialog
{
public:
     Program()
     {
     }
     ~Program()
     {
     }
     int Function(int* x);
protected:
     ...
};

Program.cpp
void Program::Window_Open(Win::Event& e)
{
     int n=20;
     int x = Function(&n);
     wstring texto;
     Sys::Format(texto, L"%d - %d", n, x);
     this->MessageBox(texto, texto, MB_OK);
}

int Program::Function(int* x)
{
     (*x)++;
     return (*x)*2;
}

Tip
An array without an index is a pointer to the first element of the array. When an array is passed to a function, only a pointer to the first element of the array is passed to the function. That is, the compiler does not create a copy of the whole array.
Un arreglo sin un índice es un puntero al primer elemento del arreglo. Cuando se pasa un arreglo a una función, solamente se pasa la dirección del primer elemento del arreglo. Esto es, el compilador no crea una copia del arreglo completo.

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

Program.h
#pragma once //______________________________________ Program.h
#include "resource.h"

class Program: public Win::Dialog
{
public:
     Program()
     {
     }
     ~Program()
     {
     }
     void funa(double* x, int n);
     double funi(double*y, int n);
protected:
     ...
};

Program.cpp
void Program::Window_Open(Win::Event& e)
{
     wstring text;
     double x[3];
     funa(x, 3);
     text = Sys::Convert::ToString(funi(x, 3));
     this->MessageBox(text, L"funi", MB_OK);
     for(int i=0; i<3; i++)
     {
          text = Sys::Convert::ToString(x[i]);
          this->MessageBox(text, L"x", MB_OK);
     }
}

void Program::funa(double* x, int n)
{
     for(int i=0; i<n; i++) x[i] = 2*i+1;
}

double Program::funi(double*y, int n)
{
     double z=0.0;
     for(int i=0; i<n; i++) z+=y[i];
     return z/n;
}

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

Program.h
#pragma once //______________________________________ Program.h
#include "resource.h"

class Program: public Win::Dialog
{
public:
     Program()
     {
     }
     ~Program()
     {
     }
     void funa(double* x, int n);
     double funi(double*x, int n);
protected:
     ...
};

Program.cpp
void Program::Window_Open(Win::Event& e)
{
     wstring texto;
     double x[3];
     double y[]={2.0, -2.0, 3.0, 5.0};
     funa(x, 3);

     texto = Sys::Convert::ToString(funi(x, 3));
     this->MessageBox(texto, L"x", MB_OK);

     texto = Sys::Convert::ToString(funi(y,sizeof(y)/sizeof(y[0])));
     this->MessageBox(texto, L"x", MB_OK);
}

void Program::funa(double* x, int n)
{
     for(int i=0; i<n; i++) x[i] = 2*i+1;
}

double Program::funi(double*x, int n)
{
     double z=0.0;
     for(int i=0; i<n; i++) z+=x[i];
     return z/n;
}

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

Program.h
#pragma once //______________________________________ Program.h
#include "resource.h"

class Program: public Win::Dialog
{
public:
     Program()
     {
     }
     ~Program()
     {
     }
     double fune(int x[], int n);
protected:
     ...
};

Program.cpp
void Program::Window_Open(Win::Event& e)
{
     wstring texto;
     int y[]={2, -20, 3, 5};

     texto = Sys::Convert::ToString(fune(y, 4));
     this->MessageBox(texto, L"x", MB_OK);

     texto = Sys::Convert::ToString(y[1]);
     this->MessageBox(texto, L"x", MB_OK);
}

double Program::fune(int x[], int n)
{
     for(int i=0; i<n; i++) x[i]=-2;
     return x[0]/(double)n;
}

Passing Text Strings to a Function

A text string is an array of characters that ends with the character '\0'. Thus, when a text string is passed to a function, only a pointer to the beginning of the string is passed to the function. This pointer is of the data type wchar_t .
Las cadenas de texto son simplemente arreglos de letras que terminan con el caracter '\0'. Asi, cuando se pasa una cadena de texto a una función, solamente se pasa un puntero al inicio de la cadena de texto. Este puntero es del tipo wchar_t .

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

Program.h
#pragma once //______________________________________ Program.h
#include "resource.h"

class Program: public Win::Dialog
{
public:
     Program()
     {
     }
     ~Program()
     {
     }
     void Magic(wchar_t* p);
protected:
     ...
};

Program.cpp
void Program::Window_Open(Win::Event& e)
{
     wchar_t invitacion[]= L"Hello";
     Magic(invitacion);
     this->MessageBox(invitacion, L"Program", MB_OK);
}

void Program::Magic(wchar_t* p)
{
     while(*p != '\0')
     {
          switch(*p)
          {
          case'e':
               *p = 'o';
               break;
          case'o':
               *p = 'a';
               break;
          }
          p++;
     }
}

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

Program.h
#pragma once //______________________________________ Program.h
#include "resource.h"

class Program: public Win::Dialog
{
public:
     Program()
     {
     }
     ~Program()
     {
     }
     wchar_t* GoTo(wchar_t* p, int n);
protected:
     ...
};

Program.cpp
void Program::Window_Open(Win::Event& e)
{
     wchar_t nombre[]=L"ABCDEFGH";
     this->MessageBox(GoTo(nombre, 5), L"Program", MB_OK);
}

wchar_t* Program::GoTo(wchar_t* p, int n)
{
     wchar_t* q=NULL;
     if (*p != '\0')
     {
          p += n;
          q = p;
     }
     return q;
}

© Copyright 2000-2019 Wintempla selo. All Rights Reserved. Sep 05 2019. Home