65,208
社区成员
发帖
与我相关
我的任务
分享
#include <conio.h>
#include <stdio.h>
#include <iostream>
void foo1(char * p)
{
std::cout << sizeof(p) << std::endl;
}
void foo2(char str [])
{
std::cout << sizeof(str) << std::endl;
}
void foo3(char (& str)[10])
{
std::cout << sizeof(str) << std::endl;
}
template < typename > struct Is_Array { enum nRes { val }; };
template < typename ElementType, size_t Nx >
struct Is_Array< ElementType[Nx] >
{
enum nRes { val = 1 };
};
template < typename ElementType, size_t Nx >
struct Is_Array< ElementType (&)[Nx] >
{
enum nRes { val = 1 };
};
template < typename ElementType, size_t Nx >
struct Is_Array< ElementType (&&)[Nx] >
{
enum nRes { val = 1 };
};
template < typename My_Type >
void foo4(My_Type && raw_arg)
{
std::cout << sizeof(raw_arg) << std::endl;
std::cout << "The 'raw_arg' " << (Is_Array<decltype(raw_arg)>::val ? "is" : "isn't") << " an array!" << std::endl;
}
template < typename My_Type >
void foo5(My_Type arg [])
{
std::cout << sizeof(arg) << std::endl;
std::cout << "The 'arg' " << (Is_Array<decltype(arg)>::val ? "is" : "isn't") << " an array!" << std::endl;
}
template < typename My_Type >
void foo6(My_Type arg)
{
std::cout << sizeof(arg) << std::endl;
std::cout << "The 'arg' " << (Is_Array<decltype(arg)>::val ? "is" : "isn't") << " an array!" << std::endl;
}
int main(void)
{
char a[10] = "";
foo1(a);
std::cout << "--------" << std::endl;
foo2(a);
std::cout << "--------" << std::endl;
foo3(a);
std::cout << "--------" << std::endl;
foo4(a);
std::cout << "--------" << std::endl;
foo4((char *)a);
std::cout << "--------" << std::endl;
foo5(a);
std::cout << "--------" << std::endl;
foo6(a);
_getch();
return(0);
}
#include <conio.h>
#include <stdio.h>
#include <iostream>
void foo1(char * p)
{
std::cout << sizeof(p) << std::endl;
}
void foo2(char str [])
{
std::cout << sizeof(str) << std::endl;
}
void foo3(char (& str)[10])
{
std::cout << sizeof(str) << std::endl;
}
template < typename > struct Is_Array { enum nRes { val }; };
template < typename ElementType, size_t Nx >
struct Is_Array< ElementType[Nx] >
{
enum nRes { val = 1 };
};
template < typename ElementType, size_t Nx >
struct Is_Array< ElementType (&)[Nx] >
{
enum nRes { val = 1 };
};
template < typename ElementType, size_t Nx >
struct Is_Array< ElementType (&&)[Nx] >
{
enum nRes { val = 1 };
};
template < typename My_Type >
void foo4(My_Type && raw_arg)
{
std::cout << sizeof(raw_arg) << std::endl;
std::cout << "The 'raw_arg' " << (Is_Array<My_Type &>::val ? "is" : "isn't") << " an array!" << std::endl;
}
template < typename My_Type >
void foo5(My_Type arg [])
{
std::cout << sizeof(arg) << std::endl;
std::cout << "The 'arg' " << (Is_Array<My_Type &>::val ? "is" : "isn't") << " an array!" << std::endl;
}
template < typename My_Type >
void foo6(My_Type arg)
{
std::cout << sizeof(arg) << std::endl;
std::cout << "The 'arg' " << (Is_Array<My_Type &>::val ? "is" : "isn't") << " an array!" << std::endl;
}
int main(void)
{
char a[10] = "";
foo1(a);
std::cout << "--------" << std::endl;
foo2(a);
std::cout << "--------" << std::endl;
foo3(a);
std::cout << "--------" << std::endl;
foo4(a);
std::cout << "--------" << std::endl;
foo4((char *)a);
std::cout << "--------" << std::endl;
foo5(a);
std::cout << "--------" << std::endl;
foo6(a);
_getch();
return(0);
}
不过应该是无所谓的,我们都知道作为函数参数的时候数组在C/C++中不能按值传递(全部内容,而不是仅仅首地址)。
#include <conio.h>
#include <stdio.h>
#include <iostream>
void foo1(char * p)
{
std::cout << sizeof(p) << std::endl;
}
void foo2(char str [])
{
std::cout << sizeof(str) << std::endl;
}
void foo3(char (& str)[10])
{
std::cout << sizeof(str) << std::endl;
}
template < typename > struct Is_Array { enum nRes { val }; };
template < typename ElementType, size_t Nx >
struct Is_Array< ElementType (&)[Nx] >
{
enum nRes { val = 1 };
};
template < typename ElementType, size_t Nx >
struct Is_Array< ElementType (&&)[Nx] >
{
enum nRes { val = 1 };
};
template < typename My_Type >
void foo4(My_Type && raw_arg)
{
std::cout << sizeof(raw_arg) << std::endl;
std::cout << "The 'raw_arg' " << (Is_Array<My_Type &>::val ? "is" : "isn't") << " an array!" << std::endl;
}
template < typename My_Type >
void foo5(My_Type arg [])
{
std::cout << "The 'arg' " << (Is_Array<My_Type &>::val ? "is" : "isn't") << " an array!" << std::endl;
}
int main(void)
{
char a[10] = "";
foo1(a);
std::cout << "--------" << std::endl;
foo2(a);
std::cout << "--------" << std::endl;
foo3(a);
std::cout << "--------" << std::endl;
foo4(a);
std::cout << "--------" << std::endl;
foo4((char *)a);
std::cout << "--------" << std::endl;
foo5(a);
_getch();
return(0);
}
最后使用的模板,是为了公平起见(貌似也找不到方便的不用模板就能说明问题的方法)。因为考虑到可能有些人会问“你这foo3不是故意用了数组吗?那当然又可以得到长度了”这类问题。
使用foo4,捕捉到的参数raw_arg因为在这个模板未实例化时事先只是个“未知”类型,而类模板(Is_Array)匹配特化时不像函数模板那样承认隐式转换的结果(最后那个foo4的调用可以证明这一点),这样就可以看看raw_arg的原始类型到底是什么。
sizeof(arrayName) == arrayLength#include <iostream>
using namespace std;
void arrayTest(char str[])
{
cout << sizeof(str) << endl;
cout<<"**"<<str++<<"**"<<endl;//运算符重载,可以自加是因为这个不是指向常量的指针,是你把一个数组的地址赋值给了一个普通指针
}
int main(int argc, char* argv[])
{
char str1[10] = "I Love U";
arrayTest(str1);
return 0;
}
#include <iostream>
using namespace std;
void arrayTest(char *const str)
{
cout << sizeof(str) << endl;
cout<<"**"<<str<<"**"<<endl;
}
int main(int argc, char* argv[])
{
char str1[10] = "I Love U";
arrayTest(str1);
return 0;
} char str1[10] = "I Love U";
在执行上面这段代码的时候,编译器会把文字常量区里面的这段字符串拷贝出来,复制到编译器分配给str1数组的内存当中去,注意这是后str1所指向的内存并非一个常量,也就不存在什么指向常量的指针
”输出了整个字符串“,在函数中,str退化成了一个char*的指针,在main函数里面,上面也讲到了会把文字常量区里面的这段字符串拷贝出来,复制到编译器分配给str1数组的内存当中去,那么字符串里面的'\0'也复制过去了,在<<输出运算符遇到该字符串结尾符的时候就停止输出后面内存中的内容了。
说的有点乱,见谅,不过大体意思到了//.h
_declspec(dllexport) void testfun(char str[]);
//.cpp
void testfun(char str[])
{
str[0]=1;
return;
}
导入函数为什么是
extrn __declspec(dllimport) void __cdecl testfun(char * const)
这样呢?
参数类型为什么是char * const,若参数类型是被const修饰的,那为什么可以就该str指向的空间的值呢?[/quote]
谁写的导入?[/quote]
导出导入都是我写的//.h
_declspec(dllexport) void testfun(char str[]);
//.cpp
void testfun(char str[])
{
str[0]=1;
return;
}
导入函数为什么是
extrn __declspec(dllimport) void __cdecl testfun(char * const)
这样呢?
参数类型为什么是char * const,若参数类型是被const修饰的,那为什么可以就该str指向的空间的值呢?[/quote]
谁写的导入?//.h
_declspec(dllexport) void testfun(char str[]);
//.cpp
void testfun(char str[])
{
str[0]=1;
return;
}
导入函数为什么是
extrn __declspec(dllimport) void __cdecl testfun(char * const)
这样呢?
参数类型为什么是char * const,若参数类型是被const修饰的,那为什么可以就该str指向的空间的值呢?