我正在使用带有CLR的Visual Studio 2010(公共语言运行时)。 我想将unsigned char *从compress函数返回到main函数,但是unsigned char *和char *都给erro
“错误7错误C2440:'=':无法从'char *'转换为'unsigned char *'F:\ 4-2 \ Thesis \ PROJECT \ Database Compression Main \ Database Compression 2 \ db_comp_main.cpp 21”
unsigned char* compressor(char *data)
{
unsigned char *compressed_string;
//With some process I had compressed string of data variable as unsigned char* in variable compressed_string successfully and also printed here. Now want to return it from here.
return compressed_string;
}
int main()
{
unsigned char *main_data;
main_data=compressor("Muhammad Ashikuzzaman.Student from Khulna University Of Engineering And Technology from Bangladesh");// When I click the error from error list the cursor is put here beside str by visual studio.
printf("%s",main_data);
}
需要从压缩器函数返回unsigned char *类型数据。请帮忙。
答案 0 :(得分:4)
更改任何数据类型(函数参数*data
或局部变量*main_data
)以匹配类型。试试这个
unsigned char *main_data;
或者....
简单的类型转换可以使其正常工作:
unsigned char* compressor(char *data)
{
unsigned char *compressed_string;
compressed_string = (unsigned char*) data;
return compressed_string;
}
int main()
{
char *main_data;
main_data=(char*) compressor("Muhammad Ashikuzzaman.Student from Khulna University Of Engineering And Technology from Bangladesh");
printf("%s",main_data);
}
或......
(使用char*
更改所有内容)
有三种字符类型:(普通)char
,signed char
和unsigned char
。任何char
通常是一个8位整数*(C语言中没有专用的“字符类型”)从这个意义上讲,signed
和unsigned char
具有有用的含义(通常等价到uint8_t
和int8_t
)。当用作文本意义上的字符时,请使用char
(也称为普通字符)。这通常是signed char
,但可以由编译器以任一方式实现。所以我认为在这里使用char*
是安全的。
工作示例(将char *
视为数据类型):
char* compressor(char *data)
{
char *compressed_string;
compressed_string = "compressed";
// compressed_string = data;
return compressed_string;
}
int main()
{
char *main_data;
main_data=compressor("Muhammad Ashikuzzaman.Student from Khulna University Of Engineering And Technology from Bangladesh");
printf("%s",main_data);
}
另外,为简单起见,您可以使用指针指向
void compressor(char **data) {
*data = "compressed"; // assuming the compressed data will be smaller in length of main data, so buffer overflow won't occur
}
int main() {
char *main_data = "Muhammad Ashikuzzaman.Student from Khulna University Of Engineering And Technology from Bangladesh";
compressor(&main_data);
printf("%s", main_data);
return 0;
}
修改强>
当你问一个通过引用传递的替代方法时,除了通过引用传递之外没有办法将数组作为函数参数传递。因为当您编写void func(char str[])
时,您可能会认为您正在执行按值传递。但是g ++编译器会将语句优化为void func(char &str[0])
,以避免将整个数组作为函数参数进行处理。因此,在所有情况下,您实际上是通过引用传递的。
答案 1 :(得分:0)
这里你必须为字符串分配内存存储空间。指针只指向一个字节而不是像array一样的字节序列。你必须使用calloc或malloc作为C和new来为C ++分配所需的内存地址。使用类型转换将char *转换为unsigned char *。
#include <bits\stdc++.h>
unsigned char* compressor(unsigned char *data)
{
unsigned char *compressed_string = new unsigned char[100];
//With some process I had compressed string of data variable as unsigned unsigned char** in variable compressed_string successfully and also printed here. Now want to return it from here.
compressed_string = (unsigned char*)"working";
return compressed_string;
}
int main()
{
unsigned char *main_data = new unsigned char[1000];
main_data=compressor((unsigned char*)"Muhammad Ashikuzzaman.Student from Khulna University Of Engineering And Technology from Bangladesh");// When I click the error from error list the cursor is put here beside str by visual studio.
printf("%s",main_data);
}
您可以根据需要使用不同的输入变量来使用内存,以避免不必要的动态分配。
#include <bits\stdc++.h>
unsigned char* compressor(unsigned char *data) {
unsigned char *compressed_string = new unsigned char[strlen((char*)data)+1];
//With some process I had compressed string of data variable as unsigned char* in variable compressed_string successfully and also printed here. Now want to return it from here.
compressed_string = (unsigned char*)"working";
return compressed_string;
}
int main() {
unsigned char *inp = new unsigned char[1000];
inp = (unsigned char*)"Muhammad Ashikuzzaman.Student from Khulna University Of Engineering And Technology from Bangladesh";
unsigned char *main_data = new unsigned char[strlen((char*)inp)+1];
main_data = compressor(inp);
printf("%s",main_data);
}