# Pseudopointers

Discussion in 'C' started by david_BS, Mar 19, 2013.

1. ### david_BSNew Member

Joined:
Apr 5, 2012
Messages:
17
3
Trophy Points:
3
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.

Exampling (is exampling a word? exemplifying)
http://www.ehow.com/info_11400371_difference-between-static-method-class-method.html
http://users.auth.gr/users/6/9/076096/public_html/courses/DataStructures/Implementations.html

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 da code.

Code:
```
//
// By 85
// elhacker.net
// etalking.com.ar
// boyscout_arg@hotmail.com
// 2013
//

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Escriba un programa que reciba como entrada 10 números introducidos por teclado,
//y nos indique si es que se repitió algún número, y si es as? cuántas veces se repite?

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int main(){

// Hace que se almacenen en el segmento de datos y no en la pila
// y se guardan de forma contigua.
// INT: 4 bytes
int c = 0;
const int MAX_NUMB =10;

/*	// Logs de depuramiento (IGNORE! XD)
system("pause");*/

////////////////////////////////////////////
//Ingresar los 10 números
{
for(int i=0;i<MAX_NUMB;i++)
{
printf("Introduzca un numero\n");

c=0;

for(int j=inicio_bloque;j<(inicio_bloque+(0x4*i));j+=0x4)// 4 BYTEZ
{

c++;
}
}

if(c)
{
printf("El número %d se repiti?%d veces\n",**(int**)apuntador1,c);
//while(getchar()!='\n');
system("pause");
}

}
}

///////////////////////////////////////////////////////

// Logs de depuramiento (IGNORE! XD)
system("pause");*/

///////////////////////////////////////////////////////
//Listar todos los números (opcional)

{
for(int i=1;i<=MAX_NUMB;i++)
{

}
}

///////////////////////////////////////////////////////
//Listar todos los números (opcional)

/*	// Logs de depuramiento (IGNORE! XD)
system("pause");*/

return 0;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

```
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;
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");
...
}```
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:
```...
for(int j=inicio_bloque;j<(inicio_bloque+(0x4*i));j+=0x4)
{

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 pseudopointer to printf
Code:
```for(int i=1;i<=MAX_NUMB;i++)
{

}```
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.

See attachment if you want the project.

#### Attached Files:

• ###### pseudopunteros.rar
File size:
8.3 KB
Views:
43
Last edited: Mar 19, 2013
2. ### david_BSNew Member

Joined:
Apr 5, 2012
Messages:
17
3
Trophy Points:
3
I updated my code since the first one wasn't considering a lot of aspects.

In the first code I just wanted to point out the use of typecasting in basic variables, and using them as pointers, but not using pointer type variables at all.

main.cpp
Code:
```//
// By 85
// elhacker.net
// etalking.com.ar
// boyscout_arg@hotmail.com
// 2013
//

/////////////////////////////////////////////////////////////////////////////////////////////////

//#include<windows.h>
#include<stdio.h>
#include<stdlib.h>

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void Test1();
void Test2();
void Test3();

/////////////////////////////////////////////////////////////////////////////////////////////////

int main(){

system("cls");
printf("Test1:\n");
Test1();
system("pause");

system("cls");
printf("Test2:\n");
Test2();
system("pause");

system("cls");
printf("Test3:\n");
Test3();
system("pause");

return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////////////```
pseudopointers1.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>

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// No tenemos garantía de que sean almacenadas contiguamente!

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void Test1(){
/*
////////////////////////////////////
// Basic check (Designed for 10 variables no more)

printf("Estas direcciones deben estar en forma contigua!\n");
int flag_exit=0;
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
unsigned char OFFS = (unsigned char)sizeof(XVARTYPE);// Up to 255
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");

rep=0;

for(XPOINTERTYPE j=inicio_bloque;j<(inicio_bloque+(OFFS*i));j+=OFFS)
{

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";
//while(getchar()!='\n');
system("pause");
}

}

for(int k=1;k<=MAX_NUMB;k++)
{

}
printf("\n");
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
```
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.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void Test2(){

#define XPOINTERTYPE unsigned long
#define XVARTYPE int
unsigned char OFFS = (unsigned char)sizeof(XVARTYPE);// Up to 255
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");

rep=0;

for(XPOINTERTYPE j=inicio_bloque;j<(inicio_bloque+(OFFS*i));j+=OFFS)
{

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";
//while(getchar()!='\n');
system("pause");
}

}

for(int k=1;k<=MAX_NUMB;k++)
{

}
printf("\n");
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
```
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.
int a,b,c,d,e,f,g,h,i,j;
};

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void Test3(){

#define XPOINTERTYPE unsigned long
#define XVARTYPE int
unsigned char OFFS = (unsigned char)sizeof(XVARTYPE);// Up to 255
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");

rep=0;

for(XPOINTERTYPE j=inicio_bloque;j<(inicio_bloque+(OFFS*i));j+=OFFS)
{

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";
//while(getchar()!='\n');
system("pause");
}

}

for(int k=1;k<=MAX_NUMB;k++)
{

}
printf("\n");
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
```
CODE:
http://www.mediafire.com/?q3l5yqplduxpfi6

Joined:
Apr 11, 2013
Messages:
1