I have a file which has a static constant string and a function which will return pointer to that string. File looks like this:
我有一个文件,它有一个静态常量字符串和一个函数,它将返回指向该字符串的指针。文件看起来像这样:
typedef unsigned char BOOLEAN;
#define TRUE 1
#define FALSE 0
static const unsigned char MAL_Version[8] = "2.001";
/* Function to return Version string */
BOOLEAN GetVersion ( unsigned char* pu8Version )
{
BOOLEAN success = FALSE;
if(pu8Version != NULL)
{
pu8Version = &MAL_Version[0];
success = TRUE;
printf("\r\nTRUE");
printf("\r\n%s", pu8Version);
}
return success;
}
and in main(), I declare an array and pass it's address to GetVersion function. When I do this, I am getting random characters.
在main()中,我声明一个数组并将其地址传递给GetVersion函数。当我这样做时,我会得到随机字符。
int main() {
unsigned char buffer[10];
GetVersion(buffer);
printf("\r\n%s", buffer);
}
Output is:
TRUE
2.001
D�3�
What I am missing? The pointer in function is correctly printing the string, but when it returns, it prints garbage.
我错过了什么?函数中的指针正确打印字符串,但是当它返回时,它会打印垃圾。
3 个解决方案
#1
5
This statement
pu8Version = &MAL_Version[0];
only modifies the local pointer pu8Version
in GetVersion()
and that doesn't change buffer
in main()
.
只修改GetVersion()中的本地指针pu8Version,并且不会更改main()中的缓冲区。
Instead of:
pu8Version = &MAL_Version[0];
you can copy the MAL_Version
to buffer
with:
你可以将MAL_Version复制到缓冲区:
strcpy(pu8Version, MAL_Version);
If you really don't need a copy of MAL_Version
, you can also return the pointer to MAL_Version
directly. Something like:
如果你真的不需要MAL_Version的副本,你也可以直接返回指向MAL_Version的指针。就像是:
/* Function to return Version string */
const char *GetVersion(void)
{
return MAL_Version;
}
int main(void) {
const char *version = GetVersion();
printf("\n%s", version);
}
Note that you don't define a "BOOLEAN" yourself. bool
(from <stdbool.h>
header) type is available in C since C99.
请注意,您自己没有定义“BOOLEAN”。自C99起,bool(来自
#2
0
The function parameter pu8Version
is the local copy of the pointer passed.
函数参数pu8Version是传递的指针的本地副本。
You change pu8Version
to point to the static string MAL_Version
which prints correctly. On returning fom the function, the changed version of pu8Version
is forgotten.
您将pu8Version更改为指向正确打印的静态字符串MAL_Version。在返回功能时,pu8Version的更改版本被遗忘了。
The original unsigned char buffer[10];
is uninitialised and remains so, so rubbish is printed.
原始的unsigned char缓冲区[10];是未经初始化的,仍然如此,所以打印垃圾。
Note you cannot copy a C string with the =
operator. What that does is to change the pointer, but not what it is pointing to. You should use strcpy
.
请注意,您无法使用=运算符复制C字符串。这样做是为了改变指针,而不是它指向的指针。你应该使用strcpy。
#3
0
Or you can pass a pointer to a pointer:
或者您可以将指针传递给指针:
/* Function to return Version string */
BOOLEAN GetVersion ( unsigned char **pu8Version )
{
BOOLEAN success = FALSE;
if(*pu8Version != NULL)
{
*pu8Version = MAL_Version;
success = TRUE;
printf("\r\nTRUE");
printf("\r\n%s", *pu8Version);
}
return success;
}
int main() {
unsigned char *buffer;
GetVersion(&buffer);
printf("\r\n%s", buffer);
}
#1
5
This statement
pu8Version = &MAL_Version[0];
only modifies the local pointer pu8Version
in GetVersion()
and that doesn't change buffer
in main()
.
只修改GetVersion()中的本地指针pu8Version,并且不会更改main()中的缓冲区。
Instead of:
pu8Version = &MAL_Version[0];
you can copy the MAL_Version
to buffer
with:
你可以将MAL_Version复制到缓冲区:
strcpy(pu8Version, MAL_Version);
If you really don't need a copy of MAL_Version
, you can also return the pointer to MAL_Version
directly. Something like:
如果你真的不需要MAL_Version的副本,你也可以直接返回指向MAL_Version的指针。就像是:
/* Function to return Version string */
const char *GetVersion(void)
{
return MAL_Version;
}
int main(void) {
const char *version = GetVersion();
printf("\n%s", version);
}
Note that you don't define a "BOOLEAN" yourself. bool
(from <stdbool.h>
header) type is available in C since C99.
请注意,您自己没有定义“BOOLEAN”。自C99起,bool(来自
#2
0
The function parameter pu8Version
is the local copy of the pointer passed.
函数参数pu8Version是传递的指针的本地副本。
You change pu8Version
to point to the static string MAL_Version
which prints correctly. On returning fom the function, the changed version of pu8Version
is forgotten.
您将pu8Version更改为指向正确打印的静态字符串MAL_Version。在返回功能时,pu8Version的更改版本被遗忘了。
The original unsigned char buffer[10];
is uninitialised and remains so, so rubbish is printed.
原始的unsigned char缓冲区[10];是未经初始化的,仍然如此,所以打印垃圾。
Note you cannot copy a C string with the =
operator. What that does is to change the pointer, but not what it is pointing to. You should use strcpy
.
请注意,您无法使用=运算符复制C字符串。这样做是为了改变指针,而不是它指向的指针。你应该使用strcpy。
#3
0
Or you can pass a pointer to a pointer:
或者您可以将指针传递给指针:
/* Function to return Version string */
BOOLEAN GetVersion ( unsigned char **pu8Version )
{
BOOLEAN success = FALSE;
if(*pu8Version != NULL)
{
*pu8Version = MAL_Version;
success = TRUE;
printf("\r\nTRUE");
printf("\r\n%s", *pu8Version);
}
return success;
}
int main() {
unsigned char *buffer;
GetVersion(&buffer);
printf("\r\n%s", buffer);
}