I recently helped a beginner with solving some situation. he needed
to load 10 integer numbers, getting to list them and to tell how many of them
resulted in a repetition (and how many times!).
well, that is not the point because the point is the idea I got from that XD.
I decided to solve that using pointer arithmetics and using 10 integer variables.
the reason will tell you later.
A variable uses memory space and it has a memory address too.
we all know that, and also we know a variable can save a value.
that value could be a memory address for example, just like pointers do.
For that, in this topic I will show how to make use of variables to work
as pointers, but obviously not using pointer type variables at all.
Honestly, there's no reason at all to make something like this, moreover,
you can make use of pointers, just the way the language makes it possible for
us. But it also allows us to do in the other way so let's continue.
Neither I'm sure whether the term "pseudopointer" is correct or not,
i was searching by GOOGLE so I can know if the expression was used before.
it seems it was used to mean something oriented to OBJECTS.
But no one talks about a formal definition, therefore I see a reason to call
these variables like this XD.
So the problem was to enter 10 numbers (integers), get them listed and get
the program to tell us how many repetitions were there, more or less XD
so we're going to use 10 integer variables, and they are to be stored
contiguously. let's take into account that 1 integer are 4 bytes, having
all this we're going to the code.
//
// By 85
// elhacker.net
// etalking.com.ar
// boyscout_arg@hotmail.com
// 2013
//
/////////////////////////////////////////////////////////////////////////////////////////////////
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <typeinfo>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// No tenemos garantÃ*a de que sean almacenadas contiguamente!
static int entrada1=0;//entrada1+0x0
static int entrada2=0;//entrada1+0x4
static int entrada3=0;//entrada1+0x8
static int entrada4=0;//entrada1+0xC
static int entrada5=0;//entrada1+0x10
static int entrada6=0;//entrada1+0x14
static int entrada7=0;//entrada1+0x18
static int entrada8=0;//entrada1+0x1C
static int entrada9=0;//entrada1+0x20
static int entrada10=0;//entrada1+0x24
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Test1(){
/*
////////////////////////////////////
// Basic check (Designed for 10 variables no more)
//int entrada10=0;// Hacerlo cagar XD
printf("Estas direcciones deben estar en forma contigua!\n");
printf("0x%X\n",&entrada1);
printf("0x%X\n",&entrada2);
printf("0x%X\n",&entrada3);
printf("0x%X\n",&entrada4);
printf("0x%X\n",&entrada5);
printf("0x%X\n",&entrada6);
printf("0x%X\n",&entrada7);
printf("0x%X\n",&entrada8);
printf("0x%X\n",&entrada9);
printf("0x%X\n",&entrada10);
int flag_exit=0;
if( ((&entrada2)-((&entrada1)+0x1)) !=0){flag_exit=1; goto error;}
if( ((&entrada3)-((&entrada2)+0x1)) !=0){flag_exit=2; goto error;}
if( ((&entrada4)-((&entrada3)+0x1)) !=0){flag_exit=3; goto error;}
if( ((&entrada5)-((&entrada4)+0x1)) !=0){flag_exit=4; goto error;}
if( ((&entrada6)-((&entrada5)+0x1)) !=0){flag_exit=5; goto error;}
if( ((&entrada7)-((&entrada6)+0x1)) !=0){flag_exit=6; goto error;}
if( ((&entrada8)-((&entrada7)+0x1)) !=0){flag_exit=7; goto error;}
if( ((&entrada9)-((&entrada8)+0x1)) !=0){flag_exit=8; goto error;}
if( ((&entrada10)-((&entrada9)+0x1)) !=0){flag_exit=9; goto error;}
error:
if(flag_exit>0){
printf("It seems they are not stored contiguously. Error %d\n", flag_exit);
system("pause");
return;
}*/
////////////////////////////////////
#define XPOINTERTYPE unsigned long
#define XVARTYPE int
//if(sizeof(XVARTYPE) != sizeof(entrada1)) return;
unsigned char OFFS = (unsigned char)sizeof(XVARTYPE);// Up to 255
XPOINTERTYPE entradaX=(XPOINTERTYPE)&entrada1;// Una especie de puntero THIS
XPOINTERTYPE inicio_bloque=(XPOINTERTYPE)&entrada1;
XPOINTERTYPE fin_bloque=(XPOINTERTYPE)&entrada10;
int rep = 0;
const int MAX_NUMB =10;
char* format;
if(!strcmp(typeid(XVARTYPE).name(), "int")) format="%d";
else if(!strcmp(typeid(XVARTYPE).name(), "long")) format="%d";
else if(!strcmp(typeid(XVARTYPE).name(), "long int")) format="%d";
else if(!strcmp(typeid(XVARTYPE).name(), "long long")) format="%d";
else if(!strcmp(typeid(XVARTYPE).name(), "unsigned long")) format="%X";
else if(!strcmp(typeid(XVARTYPE).name(), "unsigned int")) format="%d";
else if(!strcmp(typeid(XVARTYPE).name(), "unsigned short")) format="%d";
else if(!strcmp(typeid(XVARTYPE).name(), "float")) format="%f";
else if(!strcmp(typeid(XVARTYPE).name(), "double")) format="%f";
else if(!strcmp(typeid(XVARTYPE).name(), "double long")) format="%f";
else if(!strcmp(typeid(XVARTYPE).name(), "long double")) format="%f";
else return;
for(int i=0;i<MAX_NUMB;i++)
{
printf("Introduzca un número\n");
scanf(format,(XPOINTERTYPE*)entradaX);
rep=0;
XPOINTERTYPE apuntador1=(XPOINTERTYPE)&entradaX;//Doble puntero
for(XPOINTERTYPE j=inicio_bloque;j<(inicio_bloque+(OFFS*i));j+=OFFS)
{
XPOINTERTYPE apuntador2=(XPOINTERTYPE)&j;//Doble puntero
if(**(XVARTYPE**)apuntador1 == **(XVARTYPE**)apuntador2){
rep++;
}
}
if(rep)
{
char* info;
if(!strcmp(typeid(XVARTYPE).name(), "int")) info="%d had %d repetitions\n";
else if(!strcmp(typeid(XVARTYPE).name(), "long")) info="%d had %d repetitions\n";
else if(!strcmp(typeid(XVARTYPE).name(), "long int")) info="%d had %d repetitions\n";
else if(!strcmp(typeid(XVARTYPE).name(), "long long")) info="%d had %d repetitions\n";
else if(!strcmp(typeid(XVARTYPE).name(), "unsigned long")) info="%X had %d repetitions\n";
else if(!strcmp(typeid(XVARTYPE).name(), "unsigned int")) info="%d had %d repetitions\n";
else if(!strcmp(typeid(XVARTYPE).name(), "unsigned short")) info="%d had %d repetitions\n";
else if(!strcmp(typeid(XVARTYPE).name(), "float")) info="%f had %d repetitions\n";
else if(!strcmp(typeid(XVARTYPE).name(), "double")) info="%f had %d repetitions\n";
else if(!strcmp(typeid(XVARTYPE).name(), "double long")) info="%f had %d repetitions\n";
else if(!strcmp(typeid(XVARTYPE).name(), "long double")) info="%f had %d repetitions\n";
printf(info,**(XVARTYPE**)apuntador1,rep);
//while(getchar()!='\n');
system("pause");
}
entradaX+=OFFS;
}
entradaX=(XPOINTERTYPE)&entrada1;//Arreglar el pseudopuntero THIS
for(int k=1;k<=MAX_NUMB;k++)
{
printf(format,*(XVARTYPE*)entradaX);
entradaX+=OFFS;
}
printf("\n");
entradaX=(XPOINTERTYPE)&entrada1;//Arreglar el pseudopuntero THIS
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pseudopointers2.cpp
Code:
//
// By 85
// elhacker.net
// etalking.com.ar
// boyscout_arg@hotmail.com
// 2013
//
/////////////////////////////////////////////////////////////////////////////////////////////////
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <typeinfo>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Vamos a usar un array para asegurarnos de que los 10 enteros
// sean almacenados de forma contigua.
static int entradas[10] = {0};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Test2(){
#define XPOINTERTYPE unsigned long
#define XVARTYPE int
//if(sizeof(XVARTYPE) != sizeof(entradas[0])) return;
unsigned char OFFS = (unsigned char)sizeof(XVARTYPE);// Up to 255
XPOINTERTYPE entradaX=(XPOINTERTYPE)&entradas[0];// Una especie de puntero THIS
XPOINTERTYPE inicio_bloque=(XPOINTERTYPE)&entradas[0];
XPOINTERTYPE fin_bloque=(XPOINTERTYPE)&entradas[9];
int rep = 0;
const int MAX_NUMB =10;
char* format;
if(!strcmp(typeid(XVARTYPE).name(), "int")) format="%d";
else if(!strcmp(typeid(XVARTYPE).name(), "long")) format="%d";
else if(!strcmp(typeid(XVARTYPE).name(), "long int")) format="%d";
else if(!strcmp(typeid(XVARTYPE).name(), "long long")) format="%d";
else if(!strcmp(typeid(XVARTYPE).name(), "unsigned long")) format="%X";
else if(!strcmp(typeid(XVARTYPE).name(), "unsigned int")) format="%d";
else if(!strcmp(typeid(XVARTYPE).name(), "unsigned short")) format="%d";
else if(!strcmp(typeid(XVARTYPE).name(), "float")) format="%f";
else if(!strcmp(typeid(XVARTYPE).name(), "double")) format="%f";
else if(!strcmp(typeid(XVARTYPE).name(), "double long")) format="%f";
else if(!strcmp(typeid(XVARTYPE).name(), "long double")) format="%f";
else return;
for(int i=0;i<MAX_NUMB;i++)
{
printf("Introduzca un número\n");
scanf(format,(XPOINTERTYPE*)entradaX);
rep=0;
XPOINTERTYPE apuntador1=(XPOINTERTYPE)&entradaX;//Doble puntero
for(XPOINTERTYPE j=inicio_bloque;j<(inicio_bloque+(OFFS*i));j+=OFFS)
{
XPOINTERTYPE apuntador2=(XPOINTERTYPE)&j;//Doble puntero
if(**(XVARTYPE**)apuntador1 == **(XVARTYPE**)apuntador2){
rep++;
}
}
if(rep)
{
char* info;
if(!strcmp(typeid(XVARTYPE).name(), "int")) info="%d had %d repetitions\n";
else if(!strcmp(typeid(XVARTYPE).name(), "long")) info="%d had %d repetitions\n";
else if(!strcmp(typeid(XVARTYPE).name(), "long int")) info="%d had %d repetitions\n";
else if(!strcmp(typeid(XVARTYPE).name(), "long long")) info="%d had %d repetitions\n";
else if(!strcmp(typeid(XVARTYPE).name(), "unsigned long")) info="%X had %d repetitions\n";
else if(!strcmp(typeid(XVARTYPE).name(), "unsigned int")) info="%d had %d repetitions\n";
else if(!strcmp(typeid(XVARTYPE).name(), "unsigned short")) info="%d had %d repetitions\n";
else if(!strcmp(typeid(XVARTYPE).name(), "float")) info="%f had %d repetitions\n";
else if(!strcmp(typeid(XVARTYPE).name(), "double")) info="%f had %d repetitions\n";
else if(!strcmp(typeid(XVARTYPE).name(), "double long")) info="%f had %d repetitions\n";
else if(!strcmp(typeid(XVARTYPE).name(), "long double")) info="%f had %d repetitions\n";
printf(info,**(XVARTYPE**)apuntador1,rep);
//while(getchar()!='\n');
system("pause");
}
entradaX+=OFFS;
}
entradaX=(XPOINTERTYPE)&entradas[0];//Arreglar el pseudopuntero THIS
for(int k=1;k<=MAX_NUMB;k++)
{
printf(format,*(XVARTYPE*)entradaX);
entradaX+=OFFS;
}
printf("\n");
entradaX=(XPOINTERTYPE)&entradas[0];//Arreglar el pseudopuntero THIS
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pseudopointers3.cpp
Code:
//
// By 85
// elhacker.net
// etalking.com.ar
// boyscout_arg@hotmail.com
// 2013
//
/////////////////////////////////////////////////////////////////////////////////////////////////
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <typeinfo>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Vamos a usar una estructura para asegurarnos de que los 10 enteros
// sean almacenados de forma contigua.
struct Entradas{
int a,b,c,d,e,f,g,h,i,j;
};
static struct Entradas entradas;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Test3(){
#define XPOINTERTYPE unsigned long
#define XVARTYPE int
//if(sizeof(XVARTYPE) != sizeof(entradas.a)) return;
unsigned char OFFS = (unsigned char)sizeof(XVARTYPE);// Up to 255
XPOINTERTYPE entradaX=(XPOINTERTYPE)&entradas.a;// Una especie de puntero THIS
XPOINTERTYPE inicio_bloque=(XPOINTERTYPE)&entradas.a;
XPOINTERTYPE fin_bloque=(XPOINTERTYPE)&entradas.j;
int rep = 0;
const int MAX_NUMB =10;
char* format;
if(!strcmp(typeid(XVARTYPE).name(), "int")) format="%d";
else if(!strcmp(typeid(XVARTYPE).name(), "long")) format="%d";
else if(!strcmp(typeid(XVARTYPE).name(), "long int")) format="%d";
else if(!strcmp(typeid(XVARTYPE).name(), "long long")) format="%d";
else if(!strcmp(typeid(XVARTYPE).name(), "unsigned long")) format="%X";
else if(!strcmp(typeid(XVARTYPE).name(), "unsigned int")) format="%d";
else if(!strcmp(typeid(XVARTYPE).name(), "unsigned short")) format="%d";
else if(!strcmp(typeid(XVARTYPE).name(), "float")) format="%f";
else if(!strcmp(typeid(XVARTYPE).name(), "double")) format="%f";
else if(!strcmp(typeid(XVARTYPE).name(), "double long")) format="%f";
else if(!strcmp(typeid(XVARTYPE).name(), "long double")) format="%f";
else return;
for(int i=0;i<MAX_NUMB;i++)
{
printf("Introduzca un número\n");
scanf(format,(XPOINTERTYPE*)entradaX);
rep=0;
XPOINTERTYPE apuntador1=(XPOINTERTYPE)&entradaX;//Doble puntero
for(XPOINTERTYPE j=inicio_bloque;j<(inicio_bloque+(OFFS*i));j+=OFFS)
{
XPOINTERTYPE apuntador2=(XPOINTERTYPE)&j;//Doble puntero
if(**(XVARTYPE**)apuntador1 == **(XVARTYPE**)apuntador2){
rep++;
}
}
if(rep)
{
char* info;
if(!strcmp(typeid(XVARTYPE).name(), "int")) info="%d had %d repetitions\n";
else if(!strcmp(typeid(XVARTYPE).name(), "long")) info="%d had %d repetitions\n";
else if(!strcmp(typeid(XVARTYPE).name(), "long int")) info="%d had %d repetitions\n";
else if(!strcmp(typeid(XVARTYPE).name(), "long long")) info="%d had %d repetitions\n";
else if(!strcmp(typeid(XVARTYPE).name(), "unsigned long")) info="%X had %d repetitions\n";
else if(!strcmp(typeid(XVARTYPE).name(), "unsigned int")) info="%d had %d repetitions\n";
else if(!strcmp(typeid(XVARTYPE).name(), "unsigned short")) info="%d had %d repetitions\n";
else if(!strcmp(typeid(XVARTYPE).name(), "float")) info="%f had %d repetitions\n";
else if(!strcmp(typeid(XVARTYPE).name(), "double")) info="%f had %d repetitions\n";
else if(!strcmp(typeid(XVARTYPE).name(), "double long")) info="%f had %d repetitions\n";
else if(!strcmp(typeid(XVARTYPE).name(), "long double")) info="%f had %d repetitions\n";
printf(info,**(XVARTYPE**)apuntador1,rep);
//while(getchar()!='\n');
system("pause");
}
entradaX+=OFFS;
}
entradaX=(XPOINTERTYPE)&entradas.a;//Arreglar el pseudopuntero THIS
for(int k=1;k<=MAX_NUMB;k++)
{
printf(format,*(XVARTYPE*)entradaX);
entradaX+=OFFS;
}
printf("\n");
entradaX=(XPOINTERTYPE)&entradas.a;//Arreglar el pseudopuntero THIS
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-------------------------------------------------------------------
EARLIER EXPLANATION
---------------------------
Considering the 10 variables like a memory block of 10*4BYTES, we're
going to store the blockstart memory address and the blockend one.
Code:
int inicio_bloque=(int)&entrada1;
int fin_bloque=(int)&entrada10;
This variable stores the first-variable memory address which stores the
first number. This variable is more or less like a THIS pointer, to call it
something, because it gets displaced along of the block of 10 variables,
it is, that it goes changing its value to the address of any of the other
10 integer variables we use to store the numbers.
For all that garbage, I have no drama in calling it a "THIS pseudopointer" XD.
Code:
int entradaX=(int)&entrada1;
To store the 10 numbers we do 10 iterations, but scanf saves the input in the
address that is contained in the variable called 'entradaX'. Note the typecasting,
so scanf will know it is about a pointer.
Code:
for(int i=0;i<MAX_NUMB;i++)
{
printf("Introduzca un numero\n");
scanf("%d",(int*)entradaX);
...
}
quoting this part, is where it is made the comparision of each input against
all the numbers already were inputted till the moment.
Notice that it is used a local variable called 'apuntador1', that in fact is
doing as a double pointer or a pointer to pointer, because it holds the address
of another variable, which at the same time holds the value of another variable again.
For that, to access the final value (a number), it is implemented the double pointer
notation.
In what is related to the FOR loop, as we said it were 10 integers 4 bytes each one,
so the loop will count from-to 4bytes till it does 10 iterations.
Code:
...
int apuntador1=(int)&entradaX;//Doble puntero
for(int j=inicio_bloque;j<(inicio_bloque+(0x4*i));j+=0x4)
{
int apuntador2=(int)&j;//Doble puntero
if(**(int**)apuntador1 == **(int**)apuntador2){
c++;
}
}
...
The variable 'entradaX' or "pseudo THIS pointer" it's incremented, so it
points to the last number or the current one (it depends).
This is done by incrementing it in the FOR loop
Code:
entradaX+=0x4;
point now is, that this variable gets reimplemented, so it needs to be
arranged its address in order it always points to the first integer variable,
everytime it will be reimplemented again.
Code:
entradaX=(int)&entrada1;
All that has certain analogy to the THIS pointer, but I want to say
it is not the THIS pointer, not any close to it. It was just a mere
comparision, that's why I said "pseudo"
To list it is the same, it is passed the content in the pseudopointer to printf
If you think this is a pile of junk, it's alright. I'm not discovering America,
but see that this is not the way to make something like this, you normally use
pointer type variables, so I thought it was alright to show how to make it without.
I know some beginners will appreciate it XD.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.