http://blog.csdn.net/maxsky/article/details/41547399
附上jna 使用demo实例:
http://pan.baidu.com/s/1gdD4dEz
先来一段代码:
- #include <stdio.h>
- #include <tchar.h>
-
- char* test(void)
- {
- char arr[] = "Hello,World\n";
- return arr;
-
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- printf("%s", test());
- getchar();
- return 0;
- }
代码运行是正常的,无任何错误,但是有一段警告
警告 1 warning C4172: 返回局部变量或临时变量的地址 main.cpp 7 1 ConsoleAppTest
当函数返回时,局部变量和临时对象被销毁,所以返回的地址是无效的。需要修改代码,使其不返回局部对象的地址。
那么如何解决?该怎么修改代码?
以下内容来自(稍作修改):http://hi.baidu.com/dotcloud/item/302c4414f7f30e5f2b3e222c
以上方代码为例
方法1:
- #include <stdio.h>
- #include <tchar.h>
-
- char* test(void)
- {
- char *arr = "Hello,World\n";
- return arr;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- printf("%s", test());
- getchar();
- return 0;
- }
方法2:
- #include <stdio.h>
- #include <tchar.h>
-
- char* test(void)
- {
- static char arr[] = "Hello,World\n";
- return arr;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- printf("%s", test());
- getchar();
- return 0;
- }