Newbie. Microsoft Visual Express 2008. Problem with Assignment

H

HansWernerMarschke

// 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_string = (char *)
malloc(strlen(string)*sizeof(char));
// Encrypt the string
for (i = 0; i < strlen(string); ++i)
{
encrypted_string = encrypt_char(string);
}
encrypted_string = "\0"; <------------- Something goes wrong
warning C4047
return encrypted_string;
}

------------------> Anzahl der Dereferenzierungen bei 'char' und 'char
[2]' unterschiedlich
 
W

Walter Roberson

char *encrypted_string = (char *)
malloc(strlen(string)*sizeof(char));
encrypted_string = "\0"; <------------- Something goes wrong


encrypted_string = '\0';
OR
encrypted_string = 0;

"\0" is a string, which devolves to a pointer to the string,
so you are trying to store a pointer in a char.
 
B

Barry Schwarz

// 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_string = (char *)
malloc(strlen(string)*sizeof(char));
// Encrypt the string
for (i = 0; i < strlen(string); ++i)
{
encrypted_string = encrypt_char(string);
}
encrypted_string = "\0"; <------------- Something goes wrong
warning C4047
return encrypted_string;
}


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
 
L

lawrence.jones

char *encrypt_string (char *string)
{
unsigned int i;
// Allocate memory
char *encrypted_string = (char *)
malloc(strlen(string)*sizeof(char));

Casting the return value of malloc in C is generally considered to
be a bad idea. Be sure to #include <stdlib.h> and <string.h> to 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top