我正在搞乱多维数组和指针.我一直在看一个程序,它打印出一个简单数组的内容和地址.这是我的数组声明:
int zippo[4][2] = { {2,4},
{6,8},
{1,3},
{5,7} };
Run Code Online (Sandbox Code Playgroud)
我目前的理解是它zippo是一个指针,它可以保存其他几个指针的地址.默认情况下,zippo持有指针的地址zippo[0],也可以容纳指针的地址zippo[1],zippo[2]和zippo[3].
现在,请采取以下声明:
printf("zippo[0] = %p\n", zippo[0]);
printf(" *zippo = %p\n", *zippo);
printf(" zippo = %p\n", zippo);
Run Code Online (Sandbox Code Playgroud)
在我的机器上,它提供以下输出:
zippo[0] = 0x7fff170e2230
*zippo = 0x7fff170e2230
zippo = 0x7fff170e2230
Run Code Online (Sandbox Code Playgroud)
我完全理解为什么zippo[0]并*zippo拥有相同的价值.它们都是指针,它们都存储整数2的地址(默认),或者zippo[0][0].但是,zippo共享相同的内存地址又是什么呢?不zippo应该存储指针的地址zippo[0]?Whaaaat?
例如:
#include <stdio.h>
void why_cant_we_switch_him(void *ptr)
{
switch (ptr) {
case NULL:
printf("NULL!\n");
break;
default:
printf("%p!\n", ptr);
break;
}
}
int main(void)
{
void *foo = "toast";
why_cant_we_switch_him(foo);
return 0;
}
gcc test.c -o test
test.c: In function 'why_cant_we_switch_him':
test.c:5: error: switch quantity not an integer
test.c:6: error: pointers are not permitted as case values
Run Code Online (Sandbox Code Playgroud)
只是好奇.这是技术限制吗?
人们似乎认为只有一个常量指针表达式.这是真的吗?举例来说,这里是在Objective-C(一种常见的范例是真的是仅含有C除了NSString,id并且nil,这仅仅是一个指针,所以它仍然是相关的-我只是想指出,有是,事实上,一个共同的使用它,尽管这只是一个技术问题):
#include <stdio.h>
#include <Foundation/Foundation.h>
static NSString * const kMyConstantObject = @"Foo";
void why_cant_we_switch_him(id ptr) …Run Code Online (Sandbox Code Playgroud) 只是想知道它是如何在不同的编译器和调试/发布配置中实现的.标准是否以某种方式提供有关其实施的建议?它在哪里有所不同?
我试图运行一个简单的程序,我已经从函数返回非const引用和指向局部变量的指针,但它的工作方式相同.那么内部引用只是一个指针是真的吗?
我有一张地图宣称为
std::map<std::string, Texture*> textureMap;
Run Code Online (Sandbox Code Playgroud)
我用它来将纹理文件的路径与实际纹理配对,这样我就可以通过路径引用纹理,而不会为单个精灵加载相同的纹理.我不知道该怎么做才能正确销毁ResourceManager类(地图所在的)的析构函数中的纹理.
我想过使用像这样的迭代器的循环:
ResourceManager::~ResourceManager()
{
for(std::map<std::string, Texture*>::iterator itr = textureMap.begin(); itr != textureMap.end(); itr++)
{
delete (*itr);
}
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用,它说删除预期指针.现在已经很晚了,所以我可能只是错过了一些明显的东西,但我想让它在睡前工作.所以我是关闭还是我完全朝着错误的方向?
我很难理解C中数组名称的类型和用法.这可能看起来很长,但请耐心等待.
我理解以下语句声明a是类型,int []即整数数组.
int a[30];
Run Code Online (Sandbox Code Playgroud)
同时a也指出数组的第一个元素和类似*(a+2)的东西是有效的.因此,a看起来像一个指向整数的指针.但实际上类型int []和int*不同; 前者是数组类型,后来是指向整数的 指针.
类型int []变量int*在传递给函数时也会转换为类型变量; 因为C数组是通过引用传递的(sizeof运算符除外).
这就是让我垂涎的观点.看看下面这段代码:
int main()
{
int (*p)[3];
int a[3] = { 5, 4, 6 };
p = &a;
printf("a:%d\t&a:%d\n",a,&a);
printf("%d",*(*p + 2));
}
Run Code Online (Sandbox Code Playgroud)
OUTPUT:
a:2686720 &a:2686720
6
Run Code Online (Sandbox Code Playgroud)
那么,上面的代码是如何工作的呢?我有两个问题:
a并&a具有相同的值.为什么?int (*p)[3];做什么的?它声明了一个 …我试图将类中的成员函数传递给一个带有成员函数类指针的函数.我遇到的问题是我不确定如何使用this指针在类中正确执行此操作.有没有人有建议?
这是传递成员函数的类的副本:
class testMenu : public MenuScreen{
public:
bool draw;
MenuButton<testMenu> x;
testMenu():MenuScreen("testMenu"){
x.SetButton(100,100,TEXT("buttonNormal.png"),TEXT("buttonHover.png"),TEXT("buttonPressed.png"),100,40,&this->test2);
draw = false;
}
void test2(){
draw = true;
}
};
Run Code Online (Sandbox Code Playgroud)
函数x.SetButton(...)包含在另一个类中,其中"object"是模板.
void SetButton(int xPos, int yPos, LPCWSTR normalFilePath, LPCWSTR hoverFilePath, LPCWSTR pressedFilePath, int Width, int Height, void (object::*ButtonFunc)()) {
BUTTON::SetButton(xPos, yPos, normalFilePath, hoverFilePath, pressedFilePath, Width, Height);
this->ButtonFunc = &ButtonFunc;
}
Run Code Online (Sandbox Code Playgroud)
如果有人对如何正确发送此功能有任何建议,以便我以后可以使用它.
我知道,我们有不同的指针一样int,float和char.一个void指针是可以容纳所有其他的唯一指针.
其他指针是否存在只是为了灵活地进行指针运算?
除了voidC语言之外的其他指针还有其他原因吗?
在各种代码中,我已经看到调试版本中的内存分配NULL...
memset(ptr,NULL,size);
Run Code Online (Sandbox Code Playgroud)
或者0xDEADBEEF......
memset(ptr,0xDEADBEEF,size);
Run Code Online (Sandbox Code Playgroud)
0xDEADBEEF,是否仍然不符合有效数据?我想将B int数组指针传递给func函数,并能够从那里更改它,然后查看main函数中的更改
#include <stdio.h>
int func(int *B[10]){
}
int main(void){
int *B[10];
func(&B);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码给了我一些错误:
In function 'main':|
warning: passing argument 1 of 'func' from incompatible pointer type [enabled by default]|
note: expected 'int **' but argument is of type 'int * (*)[10]'|
Run Code Online (Sandbox Code Playgroud)
编辑:新代码:
#include <stdio.h>
int func(int *B){
*B[0] = 5;
}
int main(void){
int B[10] = {NULL};
printf("b[0] = %d\n\n", B[0]);
func(B);
printf("b[0] = %d\n\n", B[0]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在我得到这些错误:
||In function 'func':|
|4|error: invalid type …Run Code Online (Sandbox Code Playgroud) 如果我有这个代码,例如:
int num = 5;
int *ptr = #
Run Code Online (Sandbox Code Playgroud)
以下两个功能有什么区别?
void func(int **foo);
void func(int *foo);
Run Code Online (Sandbox Code Playgroud)
我在哪里调用函数:
func(&ptr);
Run Code Online (Sandbox Code Playgroud)
我意识到前两者采用指针作为参数指针,而第二种只采用指针.
如果我传入func(&ptr),我实际上是在传入一个指针.指针指向另一个指针有什么区别?
我相信后者会给出一个不兼容的警告,但只要你知道你在做什么,似乎细节无关紧要.似乎可能为了可读性和理解,前者是更好的选择(2星指针),但从逻辑的角度来看,有什么区别?