Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
So I was told to study a code,

#include <stdio.h>
#include "q.h"
#define ASCII_COUNT 128

void encrypt(char *letter, char key)
{
    letter=(char)((letter+key)%ASCII_COUNT);
}

void decrypt(char* letter, char key)
{
    letter=(char)(((letter+ASCII_COUNT)-key)%ASCII_COUNT);
}


Linked to q.h

#ifndef Q_H
#define Q_H

// Limiting ASCII to basic character set from 0 to 127.
#define ASCII_COUNT 128

#ifdef INCLUDE_KEYWORD

const char key[] = {'D','i','g','i','P','e','n'};

#endif

// Encrypt a single letter of an input text.
void encrypt(char* letter, char key);

// Decrypt a single letter of an input text.
void decrypt(char* letter, char key);

#endif // Q_H


And to do so I attempted to compile it, but it gave me the error "invalid operands to binary % (have ‘char *’ and ‘int’)"

What does this mean and how do i fix it?

What I have tried:

Honestly kinda lost, beginner so not sure what the problem even is
Posted
Updated 2-Nov-21 5:44am
v2

The compiler is right, you are misusing pointers.
Try

C
#ifndef Q_H
#define Q_H

// Limiting ASCII to basic character set from 0 to 127.
#define ASCII_COUNT 128

#ifdef INCLUDE_KEYWORD

const char key[] = {'D','i','g','i','P','e','n'};

#endif

// Encrypt a single letter of an input text.
void encrypt(char* pletter, char key);

// Decrypt a single letter of an input text.
void decrypt(char* pletter, char key);

#endif // Q_H


and

C
#include <stdio.h>
#include <string.h>
#include "q.h"
#define ASCII_COUNT 128

void encrypt(char *pletter, char key)
{
    *pletter=(char)((*pletter+key)%ASCII_COUNT);
}

void decrypt(char* pletter, char key)
{
    *pletter=(char)(((*pletter+ASCII_COUNT)-key)%ASCII_COUNT);
}


int main()
{
  char s[] = "foobar";

  printf("plain '%s'\n", s);

  for (unsigned n=0; n<strlen(s); ++n)
    encrypt(&s[n], 'x');

  printf("encrypted '%s'\n", s);

  for (unsigned n=0; n<strlen(s); ++n)
    decrypt(&s[n], 'x');

  printf("decrypted '%s'\n", s);

  return 0;
}


Note: You have to use pointers only if you really need to encrypt/decrypt in-place (that is changing the original string content).
 
Share this answer
 
v2
Look at the definition of encrypt() and decrypt(). In both cases letter is defined as a pointer to char. So the expression letter = (char)((letter+key)%ASCII_COUNT) has 3 issues:
1) (letter+key) is still a pointer to char. This is the equivalent of the &letter[key]. I'm don't know if that's what you wanted, perhaps you meant to (*letter+key) which adds the value of key to what letter points to.
2) The cast of the right hand side of the assignment to (char) then assigns the value of the expression to letter, not the object that letter points to. After you do that, attempts to dereference letter will almost certainly result in a crash, since its quite likely you do not own the rights to read/write items in range [0 .. 127] of RAM.
3) The assignment in the encrypt/decrypt to the variable letter, only changes the value of the local pointer, but does not change the value of the pointer of the routine that called the function, so that's probably a bug too.
I think probably what you wanted is
C
/* writing this out in steps so  I can comment it as I go along */
void encrypt(char *letter, char key)
{
    char newchar = *letter;  /* newchar ges the value that letter points to */
    newchar += key;    /* now add the value of key to newchar */
    newchar %= ASCII_COUNT; /* get rid of any high bits */
    *letter = newchar;      /* assign the value of new char to what letter points to */

   /* or as a single line it would be:
    *letter = ( (*letter + key) % ASCII_COUNT;
   */
    
}
 
Share this answer
 
Such compiler errors are the result that the compiler has found some severe language errors which you should learn to understand and avoid.
The compiler is telling you dont do that and helps you to avoid bogus code, crashes and false results.

The worst "crash" was the loss of an Ariane 5 rocket. For that reasons the 9th of september is computer bug day.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900