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 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 int entradaX=(int)&entrada1;//PSEUDOPUNTERO THIS XD int inicio_bloque=(int)&entrada1; int fin_bloque=(int)&entrada10; int c = 0; const int MAX_NUMB =10; /* // Logs de depuramiento (IGNORE! XD) 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("entradaX 0x%X\n",&entradaX); printf("entradaX 0x%X\n",(int*)entradaX); printf("*entradaX 0x%X\n",*(int*)entradaX); system("pause");*/ //////////////////////////////////////////// //Ingresar los 10 números { for(int i=0;i<MAX_NUMB;i++) { printf("Introduzca un numero\n"); scanf("%d",(int*)entradaX); c=0; int apuntador1=(int)&entradaX;//Doble puntero for(int j=inicio_bloque;j<(inicio_bloque+(0x4*i));j+=0x4)// 4 BYTEZ { int apuntador2=(int)&j;//Doble puntero if(**(int**)apuntador1 == **(int**)apuntador2){ c++; } } if(c) { printf("El número %d se repiti?%d veces\n",**(int**)apuntador1,c); //while(getchar()!='\n'); system("pause"); } entradaX+=0x4; } entradaX=(int)&entrada1;//Arreglar el pseudopuntero THIS } /////////////////////////////////////////////////////// // Logs de depuramiento (IGNORE! XD) /* 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("entradaX 0x%X\n",&entradaX); printf("entradaX 0x%X\n",(int*)entradaX); printf("*entradaX %d\n",*(int*)entradaX); system("pause");*/ /////////////////////////////////////////////////////// //Listar todos los números (opcional) { for(int i=1;i<=MAX_NUMB;i++) { printf("%d\n",*(int*)entradaX); entradaX+=0x4; } entradaX=(int)&entrada1;//Arreglar el pseudopuntero THIS } /////////////////////////////////////////////////////// //Listar todos los números (opcional) /* // Logs de depuramiento (IGNORE! XD) 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("entradaX 0x%X\n",&entradaX); printf("entradaX 0x%X\n",(int*)entradaX); printf("*entradaX %d\n",*(int*)entradaX); 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; 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 pseudopointer to printf Code: for(int i=1;i<=MAX_NUMB;i++) { printf("%d\n",*(int*)entradaX); entradaX+=0x4; } 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.
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! 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 } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// CODE: http://www.mediafire.com/?q3l5yqplduxpfi6
Thank you for sharing this information with us. It is realy very interesting information. Most of the beginners shurely get benefit from that. The concept of variable you discuss is very infomative i also learn many things from your post.