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;
}
}
}
}
}
}
Total of 3 problems:
Problem 1:
Your
arris a dangling pointer and you are dereferencing it inscanf.You need:
in place of
Problem 2:
The comparison involving
jandkis incorrectly paranthesized:should be
Problem 3:
Even with above 2 fixes your program will run into infinite loop, because your
forloops have no terminating conditions.Since you are asking user to enter 4 digits, all your loop should go from
0till9as:Add similar check for other 3 loops aswell.