|
-
February 20th, 2005, 09:30 AM
#1
Pointer Question? (Small fun about pointer :D)
Sometimes I am crazy about pointers in C/C++, but I love it.
Today I post this small fun about pointers 
Could you say what is printed on the consol window after the program finishs and explain why? How many bytes of memory are not free?
Code:
#include <stdio.h>
#include <memory.h>
void SetText(void***** p, char v){memcpy(****p,&*&v,1);}
void UcaseOrLcase(char& v, char u){v-=32;u-=32;}
int main(int argc, char* argv[])
{
char cs[5][3]={'h','l','e','l','o','m','m','r','r','r','s','e','o','y',' '};
char (*c2)[3] = &cs[2];
char (*c3)[3] = cs + 3;
char outtext[15]={0};
char *****tmp;
tmp=new char**** [1];
*tmp=new char*** [1];
**tmp=new char** [1];
***tmp=new char* [2];
****tmp=new char[6];
memcpy(tmp+(c3-c2),*c2,1);
memcpy(*tmp+1,&cs[0][2],1);
memcpy(**tmp+1,*(cs+3),1);
memcpy(***tmp+2,0[c3],1);
SetText((void*****)tmp,cs[4][1]);
outtext[0]=(char)*(tmp+1);
outtext[1]=(char)*(*tmp+1);
outtext[2]=(char)*(**tmp+1);
outtext[3]=(char)*(***tmp+2);
outtext[4]=(char)*(****tmp);
outtext[5]=*(*(cs+(int)c3-(int)c2+1)+2);
outtext[6]=0;
UcaseOrLcase(*outtext,outtext[5]);
char *****tmp2=new char**** [20];
*tmp2=(char****)(tmp2+1);
*(tmp2+1)=(char****)(tmp2+2);
*(tmp2+2)=(char****)(tmp2+3);
*(tmp2+3)=(char****)(tmp2+4);
4[tmp2]=(char****)&*&5[tmp2];
memcpy(****tmp2,"world\n\0",6);
memcpy((tmp2+4),"christmas\n\0",11);
UcaseOrLcase(0[****tmp2],(char)*(tmp2+4));
printf("%s%s",outtext,(char*)*&*&*&*&*&****tmp2);
return 0;
}
Have fun
(Tested in VC++ 6.0)
-
February 20th, 2005, 09:45 AM
#2
Re: Pointer Question? (Small fun about pointer :D)
I see that the number of sadist on CG grows stronger. (I mean no offence, just kidding.)
-
February 20th, 2005, 09:57 AM
#3
Re: Pointer Question? (Small fun about pointer :D)
I am not going to compete here. It makes my brain turn into liquid. I wonder if I will be able to boot up myself tommorow mornig, after I saw that code... hate ya
Hob
B+!
'There is no cat' - A. Einstein
Use [code] [/code] tags!
Did YOU share your photo with us at CG Members photo gallery ?
-
February 20th, 2005, 05:22 PM
#4
Re: Pointer Question? (Small fun about pointer :D)
Who in their right mind would even do anything remotely similar to that? This code is purely of academic interest, and therefore trying to understand it is pointless in my opinion.
Insert entertaining phrase here
-
February 20th, 2005, 05:39 PM
#5
Re: Pointer Question? (Small fun about pointer :D)
Not trying to be harsh or anything..
/me off to take a chill-pill...
Insert entertaining phrase here
-
February 20th, 2005, 06:25 PM
#6
Re: Pointer Question? (Small fun about pointer :D)
It was sitting in my mind all day, so i tried, and... Thank you, but you are 2 months late!
Code:
#include <stdio.h>
#include <memory.h>
//easy, copies char v to someplace pointed by p
void SetText(void***** p, char v){memcpy(****p,&*&v,1);}
//very easy, only v gets modified, cos is sent by reference
void UcaseOrLcase(char& v, char u){v-=32;u-=32;}
int main(int argc, char* argv[])
{
//2D Table of characters with 5 rows and 3 cols (rowwise):
//h l e
//l o m
//m r r
//r s e
//o y
char cs[5][3]={'h','l','e','l','o','m','m','r','r','r','s','e','o','y',' '};
//pointer to 3-element array of chars, set to 2nd row of cs
//'m r r'
char (*c2)[3] = &cs[2];
//pointer to 3-element array of chars, set to 3rd row of cs
//cs+3 => cs[3]
//'r s e'
char (*c3)[3] = cs + 3;
//its insane, so easy statement here in this code!
//all 15 elements initialized with '\0'
char outtext[15]={0};
//5ble poitnter to char
char *****tmp;
//1 pointer allocated, 4 bytes
tmp=new char**** [1];
//1 pointer allocated, 4 bytes
*tmp=new char*** [1];
//1 pointer allocated, 4 bytes
**tmp=new char** [1];
//2 pointer allocated, 2*4 bytes
***tmp=new char* [2];
//6 chars allocated, 6 bytes AFAIR
****tmp=new char[6];
//1 byte copied from cs[2][0]('m') to tmp+1
memcpy(tmp+(c3-c2),*c2,1);
//1 byte copied from cs[0][2]('e') to *tmp+1
memcpy(*tmp+1,&cs[0][2],1);
//1 byte copied from cs[0][3]('r') to **tmp+1
memcpy(**tmp+1,*(cs+3),1);
//1 byte copied from cs[3][0]('r') to ***tmp+2
//0[index] - FUNNY, never seen before!
memcpy(***tmp+2,0[c3],1);
//1 byte copied from cs[4][4]('y') to *****tmp
SetText((void*****)tmp,cs[4][1]);
//1st char of outtext set to value at (tmp+1) (m)
outtext[0]=(char)*(tmp+1);
//2nd char of outtext set to value at (tmp+1) (e)
outtext[1]=(char)*(*tmp+1);
//3rd char of outtext set to value at (**tmp+1) (r)
outtext[2]=(char)*(**tmp+1);
//4th char of outtext set to value at (***tmp+2) (r)
outtext[3]=(char)*(***tmp+2);
//5th char of outtext set to value at (*****tmp) (y)
outtext[4]=(char)*(****tmp);
//6th char of outtext set to value at cs[4][2] (' ')
//4 because c3 ad c2 are 3bytes away, and plus 1
outtext[5]=*(*(cs+(int)c3-(int)c2+1)+2);
//7th char of outtext set to '\0'
outtext[6]=0;
//value at outtext ('m') gets decreased by 32 and
//converted to 'M'
UcaseOrLcase(*outtext,outtext[5]);
//20element array of pointers allocated, 20*4 bytes
char *****tmp2=new char**** [20];
//nothing much important is going on from here...
*tmp2=(char****)(tmp2+1);
*(tmp2+1)=(char****)(tmp2+2);
*(tmp2+2)=(char****)(tmp2+3);
*(tmp2+3)=(char****)(tmp2+4);
4[tmp2]=(char****)&*&5[tmp2];
//... to here, just some pointer magjick,
//assigning non-initialized values and so on
//copy "world" to tmp2+4...
memcpy(****tmp2,"world\n\0",6);
//and immediately overwrite it witch "christmas"
memcpy((tmp2+4),"christmas\n\0",11);
//make 'c' uppercase
UcaseOrLcase(0[****tmp2],(char)*(tmp2+4));
//print strings at outtext ("Merry ")
//and in tmp2 ("Cristmas")
//each pair "*&" can be removed from expression
printf("%s%s",outtext,(char*)*&*&*&*&*&****tmp2);
//return, no deallocation done
//106 bytes left (i might screw this sum up, its late night here)
return 0;
}
//Total Time: 27 minutes
//No debuger used
Now I can go and die in peace...
If I will wake up tommorow morning, I will post my own "pointer riddle".
Last edited by Hobson; February 20th, 2005 at 06:32 PM.
B+!
'There is no cat' - A. Einstein
Use [code] [/code] tags!
Did YOU share your photo with us at CG Members photo gallery ?
-
February 20th, 2005, 08:58 PM
#7
Re: Pointer Question? (Small fun about pointer :D)
Thanks to all and sorry for taking your time
I am afraid of pointers so I made this "crazy" code to test myself.
Hobson, thank you for your attention, you did it so fast.
I am waitting for your own "pointer riddle"
Regards,
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|