Solved

obfuscating array declaration ?!?

Posted on 2003-10-28
10
236 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:marcpres
  • 4
  • 4
  • 2
10 Comments
 
LVL 8

Expert Comment

by:akshayxx
ID: 9632288
>>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.

0
 

Author Comment

by:marcpres
ID: 9632390
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.
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 9632565
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
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 9632573
>>, 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).

0
 
LVL 5

Expert Comment

by:snehanshu
ID: 9632829
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
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 5

Accepted Solution

by:
snehanshu earned 500 total points
ID: 9632853
I had to use std::cout because of my visual studio.
read the test code as:
 char str = "ABCDEF"[4];
 cout << str<< endl;
 for(int i=0;i<4;i++)
 {
         str = "ABCDEF"[i];
         cout << str;
 }

:)
...Shu
0
 
LVL 5

Expert Comment

by:snehanshu
ID: 9632890
OK, cout is for c++.
Use printf instead!

:)++
...S
0
 

Author Comment

by:marcpres
ID: 9632941
thx snehanshu,

your explanation was very comprehensive !


0
 
LVL 5

Expert Comment

by:snehanshu
ID: 9632999
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 :)
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 9635231
>>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..
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now