我在上一篇关于c的指针问题上遇到了麻烦,我从这个链接中找到了这个问题, http://www.cl.cam.ac.uk/teaching/exams/pastpapers/y2007p3q4.pdf
问题是:
AC程序员正在使用一个小端机器,一个字节为8位,一个字为4个字节.编译器支持未对齐访问,并使用1,2和4字节分别存储char,short和int.程序员编写以下定义(右下方)来访问主存储器中的值(左下方):
地址字节偏移
--------- 0 --1-- 2-- 3
0x04 | 10 00 00 00
0x08 | 61 72 62 33
0x0c | 33 00 00 00
0x10 | 78 0c 00 00
0x14 | 08 00 00 00
0x18 | 01 00 4c 03
0x1c | 18 00 00 00
int **i=(int **)0x04;
short **pps=(short **)0x1c;
struct i2c {
int i;
char *c;
}*p=(struct i2c*)0x10;
Run Code Online (Sandbox Code Playgroud)
(a)记下以下C表达式的值:
**i
p->c[2]
&(*pps)[1]
++p->i
Run Code Online (Sandbox Code Playgroud)
我明白了
**i == 0xc78
p->c[2] == '62'
++p->i …Run Code Online (Sandbox Code Playgroud) 我有一个char**,基本上是一个字符串数组,我需要删除.这样做的正确方法是什么,以确保所有指针都被清除?
鉴于数组的名称实际上是指向数组的第一个元素的指针,以下代码:
#include <stdio.h>
int main(void)
{
int a[3] = {0, 1, 2};
int *p;
p = a;
printf("%d\n", p[1]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
1按预期打印.
现在,鉴于我可以创建一个指向指针的指针,我写了以下内容:
#include <stdio.h>
int main(void)
{
int *p0;
int **p1;
int (*p2)[3];
int a[3] = {0, 1, 2};
p0 = a;
p1 = &a;
p2 = &a;
printf("p0[1] = %d\n(*p1)[1] = %d\n(*p2)[1] = %d\n",
p0[1], (*p1)[1], (*p2)[1]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望它能够编译和打印
p0[1] = 1
(*p1)[1] = 1
(*p2)[1] = 1
Run Code Online (Sandbox Code Playgroud)
但相反,它在编译时出错,说:
test.c: In function ‘main’:
test.c:11:5: …Run Code Online (Sandbox Code Playgroud) 给定一个包含一个double和三个int变量的结构定义(总共4个变量),如果p是指向此结构的指针,其值为0x1000,那么p ++有什么值?
这不是作业问题,所以不要担心.我只是想为测试做准备,我无法弄清楚这个练习题.谢谢
这是在C.是的,我希望增加后的p值.这是一台32位机器
我试图在C++中递归取消引用指针.
如果传递的对象不是指针(这包括智能指针),我只想在可能的情况下通过引用返回对象本身.
我有这个代码:
template<typename T> static T &dereference(T &v) { return v; }
template<typename T> static const T &dereference(const T &v) { return v; }
template<typename T> static T &dereference(T *v) { return dereference(*v); }
Run Code Online (Sandbox Code Playgroud)
在大多数情况下,我的代码似乎工作正常,但是在给定函数指针时它会中断,因为取消引用函数指针会导致相同类型的函数指针,从而导致堆栈溢出.
那么,当解除引用类型与原始对象具有相同类型时,如何"停止"解除引用过程?
我看到我的问题被标记为使用Boost的类似问题的副本; 但是,我需要一个没有Boost(或任何其他库)的解决方案.
例:
template<typename T> T &dereference(T &v) { return v; }
template<typename T> const T &dereference(const T &v) { return v; }
template<typename T> T &dereference(T *v) { return dereference(*v); }
template<typename TCallback …Run Code Online (Sandbox Code Playgroud) 我正在读这篇文章,遗憾的是,我无法深入理解为什么编译器不允许从Derived**转换为Base**.此外,我已经看到这个没有提供比parashift.com的链接更多的信息.
编辑:
让我们逐行分析这个代码:
Car car;
Car* carPtr = &car;
Car** carPtrPtr = &carPtr;
//MyComment: Until now there is no problem!
Vehicle** vehiclePtrPtr = carPtrPtr; // This is an error in C++
//MyComment: Here compiler gives me an error! And I try to understand why.
//MyComment: Let us consider that it was allowed. So what?? Let's go ahead!
NuclearSubmarine sub;
NuclearSubmarine* subPtr = ⊂
//MyComment: this two line are OK too!
*vehiclePtrPtr = subPtr;
//MyComment: the important …Run Code Online (Sandbox Code Playgroud) 我对这两个功能感到困惑:
void Swap_byPointer1(int *x, int *y){
int *temp=new int;
temp=x;
x=y;
y=temp;
}
Run Code Online (Sandbox Code Playgroud)
void Swap_byPointer2(int *x, int *y){
int *temp=new int;
*temp=*x;
*x=*y;
*y=*temp;
}
Run Code Online (Sandbox Code Playgroud)
为什么Swap_byPointer2成功地在x和y之间交换,而Swap_byPointer1不是?
免责声明:这个问题严格来说是学术性的.我即将给出的例子可能是糟糕的风格.
假设在CI中编写此表单的子例程:
char *foo(int x)
{
static char bar[9];
if(x == 0)
strcpy(bar, "zero");
else
strcpy(bar, "not zero"),
return bar;
}
Run Code Online (Sandbox Code Playgroud)
然后,在其他地方,我使用foo如下:
printf("%i is %s\n", 5, foo(5));
Run Code Online (Sandbox Code Playgroud)
我的指针和静态变量的心理模型预测,在实践中,这个printf的输出将是
5不是零
......但这实际上是C标准所要求的,还是我在鼻恶魔领域?
更糟糕的事情是什么呢?
strcpy(foo(5), "five");
Run Code Online (Sandbox Code Playgroud)
我的心理模型说这应该"工作",除非它明确是非法的,虽然它有点没有意义,因为它不会影响输出foo.但同样,这实际上是由标准定义的吗?
它看起来像std::cout无法打印成员函数的地址,例如:
#include <iostream>
using std::cout;
using std::endl;
class TestClass
{
void MyFunc(void);
public:
void PrintMyFuncAddress(void);
};
void TestClass::MyFunc(void)
{
return;
}
void TestClass::PrintMyFuncAddress(void)
{
printf("%p\n", &TestClass::MyFunc);
cout << &TestClass::MyFunc << endl;
}
int main(void)
{
TestClass a;
a.PrintMyFuncAddress();
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
结果是这样的:
003111DB
1
Run Code Online (Sandbox Code Playgroud)
如何MyFunc使用打印地址std::cout?
pointers ×10
c++ ×6
c ×4
arrays ×1
c++-faq ×1
declaration ×1
dereference ×1
inheritance ×1
scope ×1
struct ×1
templates ×1
terminology ×1