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";
}
sizeof(cipher)?char*in preference tochar[]for consistency's sake here.x; you never use it. You definecipher; you never initialize it. You don't makecipherbig 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).