Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Array Pointers Can Be Backwards, (Kind of)
hippiman
post May 3 2008, 01:08 AM
Post #1


Premium Member
********

Group: Members
Posts: 150
Joined: 9-April 07
From: Nebraska
Member No.: 41,301



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:

CODE
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:
CODE
a[3]
//OR
3[a]


It doesn't really make sense, but I found this on a forum, as someone's signature:
CODE
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. wink.gif
Go to the top of the page
 
+Quote Post
osknockout
post May 11 2008, 05:51 PM
Post #2


Super Member
*********

Group: Members
Posts: 399
Joined: 14-November 04
From: Elysium
Member No.: 2,280



blink.gif 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?
Go to the top of the page
 
+Quote Post
hippiman
post May 20 2008, 07:13 PM
Post #3


Premium Member
********

Group: Members
Posts: 150
Joined: 9-April 07
From: Nebraska
Member No.: 41,301



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.
Go to the top of the page
 
+Quote Post
osknockout
post May 23 2008, 02:30 PM
Post #4


Super Member
*********

Group: Members
Posts: 399
Joined: 14-November 04
From: Elysium
Member No.: 2,280



QUOTE
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;
Go to the top of the page
 
+Quote Post
seba1killer
post Jun 1 2008, 07:02 PM
Post #5


Member [Level 1]
****

Group: [HOSTED]
Posts: 61
Joined: 1-June 08
Member No.: 62,997



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.
Go to the top of the page
 
+Quote Post
iGuest
post Aug 18 2008, 02:27 AM
Post #6


Trap Double Mocha Member
***************

Group: Members
Posts: 2,360
Joined: 21-September 07
Member No.: 50,369



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
Go to the top of the page
 
+Quote Post
dimumurray
post Aug 27 2008, 01:02 PM
Post #7


Member [Level 1]
****

Group: [HOSTED]
Posts: 60
Joined: 30-July 08
From: Bronx, NY
Member No.: 65,739



QUOTE(hippiman @ May 2 2008, 09:08 PM) *
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.

This post has been edited by dimumurray: Aug 27 2008, 01:03 PM
Go to the top of the page
 
+Quote Post
TerroR
post Aug 28 2008, 08:10 AM
Post #8


Newbie [Level 1]
*

Group: [HOSTED]
Posts: 14
Joined: 28-August 08
Member No.: 66,981



QUOTE(iGuest-ishi @ Aug 18 2008, 12:27 PM) *
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:
CODE
#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;
}
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Opengl Pointer To Array(1)
  2. Array Problem(4)