Newbie. Microsoft Visual Express 2008. Problem with Assignment

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • HansWernerMarschke@web.de

    Newbie. Microsoft Visual Express 2008. Problem with Assignment

    // This will be a simple example for Matlab programming in C
    // But I have to first test the C program before I incorporate it into
    Matlab

    char *encrypt_string (char *string)
    {
    unsigned int i;
    // Allocate memory
    char *encrypted_stri ng = (char *)
    malloc(strlen(s tring)*sizeof(c har));
    // Encrypt the string
    for (i = 0; i < strlen(string); ++i)
    {
    encrypted_strin g[i] = encrypt_char(st ring[i]);
    }
    encrypted_strin g[i] = "\0"; <------------- Something goes wrong
    warning C4047
    return encrypted_strin g;
    }

    ------------------Anzahl der Dereferenzierun gen bei 'char' und 'char
    [2]' unterschiedlich
  • Walter Roberson

    #2
    Re: Newbie. Microsoft Visual Express 2008. Problem with Assignment

    In article <d255244f-467b-4dce-8b86-e92061d0a663@34 g2000hsh.google groups.com>,
    <HansWernerMars chke@web.dewrot e:
    char *encrypted_stri ng = (char *)
    >malloc(strlen( string)*sizeof( char));
    > encrypted_strin g[i] = "\0"; <------------- Something goes wrong
    encrypted_strin g[i] = '\0';
    OR
    encrypted_strin g[i] = 0;

    "\0" is a string, which devolves to a pointer to the string,
    so you are trying to store a pointer in a char.
    --
    "The first draught serveth for health, the second for pleasure,
    the third for shame, the fourth for madness." -- Sir Walter Raleigh

    Comment

    • Barry Schwarz

      #3
      Re: Newbie. Microsoft Visual Express 2008. Problem with Assignment

      On Thu, 24 Apr 2008 15:26:48 -0700 (PDT), HansWernerMarsc hke@web.de
      wrote:
      >// This will be a simple example for Matlab programming in C
      >// But I have to first test the C program before I incorporate it into
      >Matlab
      >
      >char *encrypt_string (char *string)
      >{
      unsigned int i;
      // Allocate memory
      char *encrypted_stri ng = (char *)
      >malloc(strlen( string)*sizeof( char));
      // Encrypt the string
      for (i = 0; i < strlen(string); ++i)
      {
      encrypted_strin g[i] = encrypt_char(st ring[i]);
      }
      > encrypted_strin g[i] = "\0"; <------------- Something goes wrong
      >warning C4047
      return encrypted_strin g;
      >}
      The syntax error is because you should have used single quotes(')
      instead of double quotes("). However, after that is fixed, the
      statement invokes undefined behavior because you did not malloc enough
      space. strlen(string)* sizeof(char) evaluates to enough bytes to hold
      the encrypted data except for the terminal '\0'. You need to add one
      to the expression to make room for the '\0'.


      Remove del for email

      Comment

      • lawrence.jones@siemens.com

        #4
        Re: Newbie. Microsoft Visual Express 2008. Problem with Assignment

        HansWernerMarsc hke@web.de wrote:
        >
        char *encrypt_string (char *string)
        {
        unsigned int i;
        // Allocate memory
        char *encrypted_stri ng = (char *)
        malloc(strlen(s tring)*sizeof(c har));
        Casting the return value of malloc in C is generally considered to
        be a bad idea. Be sure to #include <stdlib.hand <string.hto get
        appropriate declarations for the library functions you're using.

        sizeof(char) is 1, by definition.

        You need to add 1 more to the size you're allocating to have room to
        store the null terminator.

        -Larry Jones

        I always send Grandma a thank-you note right away. ...Ever since she
        sent me that empty box with the sarcastic note saying she was just
        checking to see if the Postal Service was still working. -- Calvin

        Comment

        Working...