-2

When I try to convert a char[] to char* there are unneeded characters added to the char* variable

 int keySize = getKeySize(key2);
 char* key = (char*)malloc(sizeof(keySize));
 int i;
 char s[keySize-1];
 int i2;

 for(i2=0; i2<keySize; i2++)
 {
     s[i2] = getCharacter(key2, i2);
 }

 strncpy(key, s, keySize);
Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
  • Please be more specific, what you want to do and what really happens. But the last iteration of the loops writes on not allocated memory(1 behind your array), so it is UB already. – Kami Kaze Jan 13 '17 at 11:13
  • sizeof(keySize) returns the size of int, which is usually 4 bytes. you sure it's what you want? afterwards you are copying (keySize) number of bytes to key – Einheri Jan 13 '17 at 11:13
  • 1
    `char s[keySize-1];` is wrong, if the keysize was `1`, you try to allocate memory for `0` bytes. Get rid of the `-1`. Also, `sizeof(keysize)` will return your the size of the variable in memory, which is 4/8 bytes in your case, get rid of the `sizeof`. That will, however, not solve your problem. Extend your question and show us the code of `getCharacter()` function. – ProXicT Jan 13 '17 at 11:15
  • Does `keySize` include the string terminator? If it doesn't you end up with a `key` that isn't `NUL`-terminated which would explain the "unneeded characters". – Klas Lindbäck Jan 13 '17 at 11:19
  • 1
    What is `key2` and how had it been initialised? – alk Jan 13 '17 at 11:33
  • Also what *exactly* is returned by `getKeySize()` – alk Jan 13 '17 at 11:34
  • I want the code to be able to assign the same key to inputs which are the same. – harleydave22 Jan 13 '17 at 11:37
  • getCharacter currently gets a char from an array list, so it has no problem and getkeySize, gets the value of an inputted string – harleydave22 Jan 13 '17 at 11:38

3 Answers3

1

There is no string data type in C programming language. Strings in C are represented as array of characters.

Note: C-Strings are actually character array terminated by '\0' character. That means, last character in any C-String in C will be used to store a '\0' character which marks the end of the string. To store n characters in C-String in C, you should define a character array of size (n+1).

Why should we terminated it by '\0'?

The '\0' termination is what differentiates a char array from a c-string in C programming language. Most string-manipulating functions (like strcpy) relies on '\0' character to know when the string is finished (and its job is done!), and won't work with simple char-array (eg. they'll keep on working past the boundaries of the array, and continue until it finds a '\0' character somewhere in memory - often corrupting memory as it goes).

Therefore, storing a '\0' character (at the end) is necessary if you want to use functions of #include <string.h> like strcpy() as they rely on '\0' character to mark the end of the character array.

'\0' is defined to be a null character - that is a character with all bits set to zero (and thus has a value 0). This has nothing to do with pointers. Read more about it here.

In your program, you want two character arrays key (dynamically allocated) and s to hold a copy of another character array key2 of size keysize. Then, both character arrays should be of atleast keysize + 1 (+1 to hold a '\0' character) size.

Change:

char* key = (char*)malloc(sizeof(keySize));

To:

char* key = malloc(keySize+1); // Don't Type-Cast malloc

And

Change:

char s[keySize-1];

To

char s[keySize+1];
Community
  • 1
  • 1
abhiarora
  • 9,743
  • 5
  • 32
  • 57
0
  1. While allocating, you should allocate one more than the size. Currently you are allocating 4 bytes only.

    char* key = (char*)malloc(keySize+1);
    //instead of
    char* key = (char*)malloc(sizeof(keySize));
    
  2. s should have a size of keySize+1

    char s[keySize+1];
    // instead of
    char s[keySize-1];
    
Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31
  • Actually, he doesn't really need to add `+1` to the array size, if he doesn't want null terminated string, however, he does not provide enough information what he is about to do. – ProXicT Jan 13 '17 at 11:18
  • Still does the same thing the value of key is "can°<<<<<<<..." when I debug – harleydave22 Jan 13 '17 at 11:19
  • @harleydave22 You also need to change the `strncpy` to `strncpy(key, s, keySize + 1);` to include the string terminator. Or add it explicitly if the source string isn't `NUL`-terminated – Klas Lindbäck Jan 13 '17 at 11:21
  • I want the code to be able to assign the same key to inputs which are the same. – harleydave22 Jan 13 '17 at 11:36
0

What about this? There are some errors about the dimension of s, I suggest you tu use strncpy

       #include <string.h>
    int main(){
             //bla bla ... 
      int keySize = getKeySize(key2);
      char* key = malloc(keySize+1);;
      int i2;

      for(i2=0; i2<keySize; i2++){
        s[i2] = getCharacter(key2, i2);
      }
      char s[keySize+1];
      strncpy(s, key, sizeof s - 1); 
      s[keySize] = '\0';
      r

return 0;
}

Anyway more information about it please,I supposed you wanted this

Teshtek
  • 1,212
  • 1
  • 12
  • 20