Link to home
Start Free TrialLog in
Avatar of marcpres
marcpres

asked on

obfuscating array declaration ?!?

Dear experts,

Shortly i have seen some C-Code where a array was declared like:

char str="ABC"[4];

Is someone kindly to explain me whats going on and how i could access this array-type !

P.S.: I alway thought a anonymous array should be e.g.  char *ptr=(char[4]){"ABC"};

thanks in advance,

Marc
Avatar of akshayxx
akshayxx
Flag of United States of America image

>>char str="ABC"[4];
i think u missed something there... can u post more of the code, because i dont think it can be so obfuscated to break some basic rules.

show the snippet of the original code, and how s/he has used the variable,  also what compiler have u tested it with, that didnt work with my gcc on cygwin.. it compiled but the intentional string constant didnt come to effect.

Avatar of marcpres
marcpres

ASKER

hi,

the problem is i haven´t this code anymore, but i remember that the identifier was never used in a context,
could it be, that is some sort of padding bytes, which is wanted to not access it ?

Btw. I use gcc/linux and it isn´t complaining while compiling, but i have not a glue also, how to use this identifier actually i compile if with -Wall -pedantic without any problems.
see as for the variable type is concerned, i am 100% sure about the type of the 'str' variable here. which is just a 'char'.
and in no ways it is that 'str' holds the address of the string constant "ABC";.. so if u do prinf "%s" on str, it will either crash of give junk( u got to be lucky for second case).
so the 'trick' lies on the right hand side of the assignment operator... which will probably map to some integer value, which when typecasted to 'char' will give some special character.
it would have been helpful to have the original code, to say something more to-the-point, becuase usually the parts of obfuscated codes  are so tightly bound with each other, that any one part of it, if used separately, will make little sense.
still lets try to figure out what it could have been used for here.
akshay
>>, it will either crash of give junk( u got to be lucky for second case).
read as
, it will either crash OR  give junk( u got to be lucky for second case).

marcpres,
  str is not an array.
  A simple explanation is that str is assigned the 5th element of the constant array "ABC".
  The actual code would have been something like
char str="ABCDEFG"[4];
   Here it would mean str is initialized to be 'E'.
Here's a test for this, try this out:

 char str = "ABCDEF"[4];
 std::cout << str<< std::endl;
 for(int i=0;i<4;i++)
 {
         str = "ABCDEF"[i];
         cout << str;
 }

Cheers,
...Shu
ASKER CERTIFIED SOLUTION
Avatar of snehanshu
snehanshu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK, cout is for c++.
Use printf instead!

:)++
...S
thx snehanshu,

your explanation was very comprehensive !


Yes, I guess the 3 posts for one simple thing made it VERY COMPREHENSIVE ;)

Sometimes it helps to be a non-standard language programmer: I had to do this in Delphi quite often!

Cheers!
...Shu

Akshay, so we meet again :)
>>Akshay, so we meet again :)
Oh Hi, how r u doing, nice catch there, it took me to go out for dinner to get the same idea.. just that i didnt have any net connection there..
and i was disappointed to see that you already got it solved.. just-kidding..