Solved

obfuscating array declaration ?!?

Posted on 2003-10-28
10
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

738 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