• C

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
marcpresAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

akshayxxCommented:
>>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
marcpresAuthor Commented:
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
akshayxxCommented:
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
Managing Security & Risk at the Speed of Business

Gartner Research VP, Neil McDonald & AlgoSec CTO, Prof. Avishai Wool, discuss the business-driven approach to automated security policy management, its benefits and how to align security policy management with business processes to address today's security challenges.

akshayxxCommented:
>>, 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
snehanshuCommented:
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
snehanshuCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
snehanshuCommented:
OK, cout is for c++.
Use printf instead!

:)++
...S
0
marcpresAuthor Commented:
thx snehanshu,

your explanation was very comprehensive !


0
snehanshuCommented:
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
akshayxxCommented:
>>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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.