1

I am trying to create a new c-string with the length of a passed in c-string. I set a variable to the length of the old string and then try to set a new c-string with that length but checking that length returns 1. Why does this happen?

//This function will return a pointer to a char array
char *encrypt(char plainText[], char code[]){
    int i = 0,j = 0;
    //length of plainText
    int len = strlen(plainText);
    printf("len %i\n",len); //8
    //I am declaring a pointer here
    char *x;
    //make a string for the cipherText
    char cipher[len];
    printf("cipher %lu\n",strlen(cipher)); //1
    return "Nothing";
}
4
  • Did you mean to use sizeof(cipher)? Commented Jun 12, 2018 at 22:58
  • It's probably better to use char* in preference to char[] for consistency's sake here. Commented Jun 12, 2018 at 22:58
  • You define x; you never use it. You define cipher; you never initialize it. You don't make cipher big enough to hold the null byte at the end of a string. The length of a string does not include the null byte, though the "value" of the string does include it (at least, the string ends at the first null byte). Commented Jun 12, 2018 at 23:27
  • @JonathanLeffler Thank you. I apologize, this was from a big function I cut down to try and isolate this question and in haste left some mess. I've been really confused on how to initialize a c-string of the same size as the string I pass in. Commented Jun 12, 2018 at 23:38

1 Answer 1

2

The array cipher is uninitialized. It's contents is indeterminate (and will seem almost random). It might not contain the string-terminator at all, which means strlen will go out of bounds in its search for it. In your specific case you seem to be "lucky" that the second element in your array just happens to be the terminator.

And you seem to forget that char strings are really called null-terminated byte strings. That null-terminator also needs space. And exist of course.

All in all plenty of chances for undefined behavior.

Sign up to request clarification or add additional context in comments.

18 Comments

That and cipher goes out of scope the moment that function ends, so it can't be returned.
There is no "chance" for undefined behaviour. Either it is UB or it isn't.
@Ctx: While a particular code path with particular data might deterministically have behavior not defined by the C standard or not, it can be non-deterministic whether the code goes down that path with that data. For example, if a jump is conditional on indeterminate data, it might or might not go down a path with behavior not defined by the C standard.
Thank you for your answer. I'm confused now because I've seen code like char myString[10]; That makes space for 10 characters, with the last being a null terminated string. I thought that since I was passing 8 into the brackets I'd be making space in memory for an 8 char array?
@Someprogrammerdude: Yes, but the likely fix is not to use strlen, rather than to try appending a null terminator to data that may contain internal zeros.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.