Question

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;
     }
    }
   }
  }
 }
}
Was it helpful?

Solution

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.

OTHER TIPS

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.

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)
    

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.

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 ?

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))
..
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top