views:

91

answers:

6

I want the user to enter a 4 digit number and the program must tell what that 4 digit number was i.e generate that 4 digit number by Brute force attack.But at the line mentioned below the compiler says invalid indirection.I would also like to have some comments about they way I am implementing it,is it a good practise?

#include<stdio.h>
void BruteForceAttack(int *arr);
int main()
{
 int *arr,i;
 printf("Enter 4 digits ,press enter after entring each digit:\n");
 for(i=0;i<4;i++)
 scanf("%d",arr+i);
 BruteForceAttack(arr);
 getchar();
 return 0;
}
void BruteForceAttack(int *arr)
{
 int i,j,k,l;
 for(i=0;;i++)
 {
  for(j=0;;j++)
  {
   for(k=0;;k++)
   {
    for(l=0;;l++)
    {
      if((*(arr+0)==i)&&(*(arr+1==j))&&(*(arr+2==k))&&(*(arr+3)==l))   /*Here the compiler says invalid indirection*/
     {
      printf("The number is %d%d%d%d",i,j,k,l);
      return;
     }
    }
   }
  }
 }
}
A: 

You didn't allocate any space for arr! Allocate the space using malloc.

...
int *arr,i;
arr = (int *) malloc(4*sizeof(int));
...

Also, you are converting a boolean (int in c) into an address! The braces are wrong in the line of error.

...
if((*(arr+0)==i)&&((*(arr+1)==j))&&((*(arr+2)==k))&&((*(arr+3)==l))
..
lalli
+2  A: 

I see a couple of problems:

  1. You're not allocating any memory for *arr. Perhaps you should define arr as

    int arr[4];
    

    Then, in scanf, you can do something like:

    scanf("%d", &arr[i]);
    
  2. You can just use the array offsets notation on the problem line:

    if(arr[0] == i && arr[1] == j && arr[2] == k && arr[3] == l)
    
Mark E
+4  A: 

Total of 3 problems:

Problem 1:

Your arr is a dangling pointer and you are dereferencing it in scanf.

You need:

int arr[4]; 

in place of

int *arr;

Problem 2:

The comparison involving j and k is incorrectly paranthesized:

&&(*(arr+1==j))&&(*(arr+2==k))

should be

&&(*(arr+1)==j)&&(*(arr+2)==k)
          ^              ^

Problem 3:

Even with above 2 fixes your program will run into infinite loop, because your for loops have no terminating conditions.

Since you are asking user to enter 4 digits, all your loop should go from 0 till 9 as:

for(i=0;i<10;i++)
        ^^^^^

Add similar check for other 3 loops aswell.

codaddict
you explained beautifully..
fahad
+2  A: 

Your parentheses are misplaced. *(arr+1==j) should be *(arr+1)==j, etc. That will fix the compiler warning, but arr[1]==j (etc.) would be even better.

Jim Lewis
That is, of course, if you really really want to use pointer arithmetic -- somewhat pointless here. `*(arr+b)` is equivalent to `arr[b]`.
Christian Mann
@Christian: Agreed! But, coming from a Lisp background, I feel compelled to fix the parentheses...
Jim Lewis
How to make friends and influence people: Because `arr[b]` is equivalent to `*(arr + b)`, it's also equivalent to `*(b + arr)`, which is equivalent to `b[arr]`. So you can write array notation as `1[array]` and it will work the same as `array[1]`.
Christian Mann
+3  A: 

I would also like to have some comments about they way I am implementing it,is it a good practise?

With regards to this particular portion of your question, consider the algorithm you're trying to implement for a moment. You have the numbers available to you, stored in arr. If the user picks the number 9999 you will iterate through 10000 numbers before you reach it. Conversely, if you iterate through each digit one at a time and stop when you find the correct digit (since it is known beforehand) you iterate 40 times.

In terms of mathematical complexity, your current algorithm has a worst-case performance of 10n, whereas it could be implemented as 10n.

eldarerathis
I actually want something like Brute-force to find what the numbers were,like we do to crack the passwords
fahad
@fahad: If it's a purely numerical password then you'll be able to implement faster algorithms by first converting the password to an integer, then. Unless you wanted to implement a dictionary attack or something of that sort (but then it's not really "brute force", I suppose). For example, though, you could implement a [binary search](http://en.wikipedia.org/wiki/Binary_search_algorithm#Number_guessing_game) fairly easily on a numerical password.
eldarerathis
I should probably add that a binary search also presumes you have a means of knowing if you're "greater" or "less than" the password. If you're strictly guessing via brute force (only yes/no responses) then your algorithm is pretty much how you'd have to do it. Hence why brute forcing a password takes an incredible amount of time and why longer passwords are more secure. Even with a number, each extra digit increases the worst-time performance by a factor of 10.
eldarerathis
@eldarerathis:I was wondering how binary search algorithm could word with integer passwords as in it the numbers are first sorted in order.
fahad
+1  A: 

Consider your innermost loop

for(l=0;;l++)
    {
      if((*(arr+0)==i)&&(*(arr+1==j))&&(*(arr+2==k))&&(*(arr+3)==l))   /*Here the compiler says invalid indirection*/
     {
      printf("The number is %d%d%d%d",i,j,k,l);
      return;
     }
    }

If the number entered by the user is anything that is not staring with 000 - how will this loop ever get terminated? Will this not just go on looping infinitely for i, j, k ==0 ?

InSane
thanks for mentioning that break conditions were necessary.
fahad