-3

So i am trying to use the Ceasar Cipher with char *'s, I've written a simple function out like this:

char * Encrypt(char * s, int k)
{
    char * c = s;

    for(int i = 0; i < strlen(s); i++)
       c[i] += k;

    return c;
} 

that seems to look like it should work but it doesn't. It throws an error when running program.

Here is an example of how i call this function:

int main()
{
    cout << Encrypt("hello", 2) << endl;
    system("pause");
    return 0;
}

And before you say "why not just use string?", well the answer is I'm writing C++ on a certain SDK that causes compiler errors when using string. Ok but yeah, any form of help will greatly be appreciated, thanks!

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
JeffCoderr
  • 283
  • 1
  • 4
  • 16
  • If you are not (or cannot) use `std::string`, then you should use the correct C library functions to *copy* the contents of `Source` into an appropriately sized array. Otherwise you are modifying `Source`. – crashmstr Jul 15 '16 at 19:29
  • Keep in mind that char is unsigned short, your encryption may not work if your key exceeds the value beyond char's range. – Daniel Protopopov Jul 15 '16 at 19:29
  • 2
    Let me guess, you call this function using a string literal? Please try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) and show us. especially how you call this function. – Some programmer dude Jul 15 '16 at 19:29
  • 3
    @DanielProtopopov Ehr what? A `char` is a `char`, `unsigned short` is a completely different type. The only commonality between `char` and `unsigned short` is that both are integer types. – Some programmer dude Jul 15 '16 at 19:31
  • A good starting point is to study a "C" language reference and/or an online course. You need to understand memory allocation. We all had to do that. – zaph Jul 15 '16 at 19:31
  • 1
    @DanielProtopopov: What do you mean "char is unsigned short"? Those are two different types, almost certainly with different ranges. – Keith Thompson Jul 15 '16 at 19:32
  • @JoachimPileborg ok, i have added an example of how i call. – JeffCoderr Jul 15 '16 at 19:32
  • @JoachimPileborg Can you please share your crystal ball with me? – NathanOliver Jul 15 '16 at 19:33
  • 1
    @JeffCoderr You need to turn up your warnings in your compiler. `Encrypt("hello", 2)` should generate a compiler warning/error. – NathanOliver Jul 15 '16 at 19:33
  • 4
    What SDK complains about `std::string`? And what exactly does "throws an error" mean? Please update your question to show us the actual error message. – Keith Thompson Jul 15 '16 at 19:34
  • Your function is flawed. You need use modulo 26. For example, what letter is 'x` + 5? – Thomas Matthews Jul 15 '16 at 19:36
  • @NathanOliver It's a very typical thing to happen when passing string literals to a function and try to modify it. I've seen it countless of times before. :) – Some programmer dude Jul 15 '16 at 19:37
  • 2
    *well the answer is I'm writing C++ on a certain SDK that causes compiler errors when using string** -- More than likely, [PEBCAK](https://en.wiktionary.org/wiki/PEBCAK) – PaulMcKenzie Jul 15 '16 at 19:38
  • They may be different types for the compiler, but in the way they're being used here suggest they're treated as numbers - https://msdn.microsoft.com/ru-ru/library/cc953fe1.aspx – Daniel Protopopov Jul 15 '16 at 19:45
  • @DanielProtopopov A `char` is always a `char`. It can be signed or unsigned but that is the only difference. In every implementation it the `sizeof(char)` has to equal 1. It is a integer type but it is never a `short` although it could take up the same space as a `short` if the system did 16 bit bytes. – NathanOliver Jul 15 '16 at 19:59
  • @NathanOliver to be pedantic; `char`, `signed char` & `unsigned char` are all different types, so not just two (signed/unsigned) but three distinct types. – Jesper Juhl Jul 15 '16 at 21:34

1 Answers1

6

String literals like "Hello" are read only. If you try to modify such a string you will have undefined behavior.

In C++ string literals are actually arrays of constant characters.

Using char* to access a string literal should have your compiler to scream a warning at you. If not you need to turn up your warning level or enable more warnings.

If you're really programming in C++ I suggest you learn about std::string and find a good beginners book to read.

Community
  • 1
  • 1
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • It is the modifying that is the problem. A good compiler/execution system will but the constant string in read-only memory. – zaph Jul 15 '16 at 19:35