Jump to content
xisto Community
Sign in to follow this  
hippiman

Array Pointers Can Be Backwards (Kind of)

Recommended Posts

I just found this out today:
When you're using arrays, when you reference a certain index, you can switch the pointer and the subscript around. I know I'm not explaining that very good, but here's and example:

int a[] = {1,2,3,4,5,6,7,8,9,10,11};int b =1;int c[] = {0,-5,11,0,0,5,0,0,0};cout << b[a][c];	//Same as c[a[b]];

See? 'b' is actually just an integer, it's not an array, or pointer, or anything. Usually, you would have to put the array first, then an integer.
Like if you have an array of type int, of length 5, you could reference something at index 3 using:
a[3]//OR3[a]

It doesn't really make sense, but I found this on a forum, as someone's signature:
int main() {	char o[1920]={0};	int _;	double l[]={0,-5,11,0,0,5,0,0,0},_1=0.174533;	for(_=0;_<36;++_)		(((int)(2[l]=((cos(_1)*(4[l]=2[l]))+(sin(_1)*7[l])))+12)*80		+((int)(7[l]=((cos(_1)*7[l])-(sin(_1)*4[l])))+40))[o]=_<27?(((int)(8[l]=((cos(-_1)		*(6[l]=8[l]))+(sin(-_1)*5[l])))+8)*80+((int)(5[l]=((cos(-_1)*5[l])-(sin(-_1)*6[l])))		+40))[o]=(((int)(1[l]=((cos(_1)*(0[l]=1[l]))+(sin(_1)*3[l])))+16)*80+((int)(3[l]=		((cos(_1)*3[l])-(sin(_1)*0[l])))+40))[o]='*':'*';	_=0;	while(_<1920)putchar(_++[o]);}//don't forget to include iostream and use namespace std
That was actually more garbled when it was in their signature, I just indented it, and added new lines where the semicolons were.
What really confused me was that they used and underscore as a variable(hard to see in a signature, I thought it was a space or something), and there were some parts where they switched the pointer and the subscript for an array (like in that last 'while').
All this really does is print out a bunch of asterisks in the shape of an 's' with a circle around it.

I'm pretty sure that the way they coded that isn't good practice, they were just trying to condense it a lot so they could fit it in their signature.
But hey, I guess I learn something new every day. :lol:

Share this post


Link to post
Share on other sites
:lol: WoW! That's really cool actually. Heh, after forever I'm learning something new about C++. yay. Wouldn't that mess up if both were defined though? E.g. your a[3] = 3[a] thing. If I made an int array named 3 and an int array named a, that wouldn't work anymore, would it?

Share this post


Link to post
Share on other sites

Actually I haven't really tried that. I didn't even think of it.I'm pretty sure it's impossible to name an array '3', though, but I get what you mean.

Share this post


Link to post
Share on other sites

I'm pretty sure it's impossible to name an array '3', though, but I get what you mean.

Hmm. This has always been true.
That being said, I think I've found my favorite line of non-ANSI code ever: 3[3]=3;

Share this post


Link to post
Share on other sites

Haha, that is imposible, You cannot do that. You should obtain a segmentation fault for doing so. If you have an array j={0,1,2,4,5,6}, if you refer to j you will obtain the adress of the first object in the array.And 4[j] will lead in a segmentation fault or will read a piece of code from the program.

Share this post


Link to post
Share on other sites

pointers

Array Pointers Can Be Backwards

 

How do you input a word/string from the keyboard then print it and it's reverse using pointers? help me please!

 

-question by ishi

Share this post


Link to post
Share on other sites

I'm pretty sure that the way they coded that isn't good practice...

It's probably not but there are competitions where programmers vie to create highly "obfuscated" code of the kind you've listed. That kind of code usually takes advantage of little known quirks in the syntax of a language and/or compiler like the one you've listed here. Google 'obfuscated code' and you will no doubt find even more oddities in code syntax and their applications. One thing though, some compilers maintain strict adherence to the specifications of its particular language, therefore some obfuscations can only be run with certain compilers. That's probably why @seba1killer got the errors he/she listed. Edited by dimumurray (see edit history)

Share this post


Link to post
Share on other sites

pointers

 

Array Pointers Can Be Backwards

How do you input a word/string from the keyboard then print it and it's reverse using pointers? help me please!

 

-question by ishi


Hopefully this is what you meant:

#include <cstdio>using namespace std;int main() {	char str[200];	for (int i = 0; i < 200; i++)		i[str] = 0;	gets(str);	int i = 0;	while (i[str] != 0) {		putc(i[str], stdout);		i++;	}		getchar();	return 0;}

Share this post


Link to post
Share on other sites

Wow, I like your thinking there but I tried it after reading your post and it is like declaring the array part [something] a variable and the variable an array...But I admire how you think like that. I would never really think outside the box like that.....

Share this post


Link to post
Share on other sites

int array[] = {1,2,3,4,5};

 

a[4] is same as 4[a] because, of the below mentioned reason.

 

Arrays are also pointers which has a constant address.

 

int array[]

 

is same as

 

int const *ptr; //means that ptr is an integer pointer whose address is constant

 

So to access the ith integer pointed by the pointer ptr,

 

*(ptr + i) is used.

 

while using array array is internally translated by the compiler as *(array + i).

So even if you use the syntax i[array] it is again internally translated as *(i + array).

 

By the commutative property of pointer addition, *(array + i) is same as *(i + array).

So both the syntax array and i[array] are equivalent. :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.