我是C的新手,目前我遇到了一些麻烦.请看下面的代码:
int main (int argc, char *argv[]) {
int j = 2;
int i = 100;
int *pi = &i;
pi = &j; //those 2 lines should do nothing, in my opinion
pi = &i; //
pi[1] = -4;
printf("i = %d, j = %d, *pi = %d\n", i, j, *pi);
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
代码因SegFault失败.用gdb进行一些调查:
(gdb) print &j
$1 = (int *) 0x7fffffffde80
(gdb) print &i
$2 = (int *) 0x7fffffffde84
Run Code Online (Sandbox Code Playgroud)
但是,没有2行,代码工作正常,因为我和j似乎交换内存中的位置 - 但为什么??
(gdb) print &j
$1 = (int …Run Code Online (Sandbox Code Playgroud) 在这个例子中,如何调用第二个函数?
template<class T>
T square(T a) {
std::cout << "using generic version to square " << a << std::endl;
return a*a;
}
/// "int" is so special -- provide a specialized function template
template<>
int square(int a) {
std::cout << "using specialized generic version to square " << a << std::endl;
return a*a;
}
/// and there's one more: a non-template square function for int
int square(int a) {
std::cout << "using explicit version to square " << a << …Run Code Online (Sandbox Code Playgroud)