Thalasotsanta
Thalasotsanta

Reputation: 5

Error when I subtract from the ASCII value

I copied a piece of code i found online that encrypts a string and i added the function of choosing the encryption key. However, i get an error


int main()
{
   int i, x, v;
   char str[100];

   printf("\nPlease enter a string:\t");
   gets(str);
   printf("\nPlease enter encryption key:\t"); //i leave a message next to the lines of code i added myself like here
   scanf("%d", &v);//here

   printf("\nPlease choose following options:\n");
   printf("1 = Encrypt the string.\n");
   printf("2 = Decrypt the string.\n");
   scanf("%d", &x);

   switch(x)
   {
   case 1:
      for(i = 0; (i < 100 && str[i] != '\0'); i++)
        str[i] = str[i] + &v;// here i changed the value from 3 to the value of int v

      printf("\nEncrypted string: %s\n", str);
      break;

   case 2:
      for(i = 0; (i < 100 && str[i] != '\0'); i++)
        str[i] = str[i] - &v;/*here i changed the value from 3 to the value of int v as well,
the error message appears here, which is strange since when i add the value it works fine but when i subtract it 
i get this error message 
" error: invalid operands to binary - (have 'int' and 'int *') " */

      printf("\nDecrypted string: %s\n", str);
      break;

   default:
      printf("\nError\n");
   }
   return 0;
}

However, if I use the same code but instead of &v I use any number it works fine


int main()
{
   int i, x, v;
   char str[100];

   printf("\nPlease enter a string:\t");
   gets(str);
   printf("\nPlease enter encryption key:\t");
   scanf("%d", &v);

   printf("\nPlease choose following options:\n");
   printf("1 = Encrypt the string.\n");
   printf("2 = Decrypt the string.\n");
   scanf("%d", &x);

   switch(x)
   {
   case 1:
      for(i = 0; (i < 100 && str[i] != '\0'); i++)
        str[i] = str[i] + &v;

      printf("\nEncrypted string: %s\n", str);
      break;

   case 2:
      for(i = 0; (i < 100 && str[i] != '\0'); i++)
        str[i] = str[i] - 3;//here

      printf("\nDecrypted string: %s\n", str);
      break;

   default:
      printf("\nError\n");
   }
   return 0;
}

I can't figure out why this is happening especially on subtraction and not on addition

Upvotes: 0

Views: 70

Answers (1)

Sourav Kannantha B
Sourav Kannantha B

Reputation: 3299

& is an operator in C, and it returns the address of its argument. So, in your case &v returns address of v, and not value stored in v. So basically, both str[i] = str[i] + &v and str[i] = str[i] - &v are wrong. You need to change them to str[i] = str[i] + v and str[i] = str[i] - v. Also, you may want to understand pointers in C properly before going anymore deep in C.

To know why first case is not showing an error, you must know about pointer operations in C. Basically, you can add or subtract an integer x to a address a (supposedly to get the data at a+x and a-x). So int* + int and int* - int are valid.

Since str[i] + &v is of type int + int*, and addition is commutative, you are not getting an error. But note that, result of this is int* and not int. So, a good enough compiler/IDE will warn here for implicit cast from int* to int when you assign the result of addition in str[i] = str[i] + &v.

However, str[i] - &v is of type int - int*, and subtraction is not commutative. So, it is indeed invalid in C and you get an error.

Upvotes: 1

Related Questions