C-当您将解引用的值传递给一个函数以便稍后与另一个字符串进行比较时,如何获取的原始值?
我知道家庭作业帮助是被回避的,然而,我有强烈的编码障碍 我最需要的是帮助理解。 因此,当我获取变量(&c)的地址时,我知道我得到了它在内存中的位置的地址,但我不知道如何取消引用该地址,以便访问它的特定值('b'),以便在它所使用的函数(color(&c,total)中进行比较 由于任务的要求,不得以任何理由更改干管 最后,我用这条线看颜色是否匹配C-当您将解引用的值传递给一个函数以便稍后与另一个字符串进行比较时,如何获取的原始值?,c,reference,dereference,C,Reference,Dereference,我知道家庭作业帮助是被回避的,然而,我有强烈的编码障碍 我最需要的是帮助理解。 因此,当我获取变量(&c)的地址时,我知道我得到了它在内存中的位置的地址,但我不知道如何取消引用该地址,以便访问它的特定值('b'),以便在它所使用的函数(color(&c,total)中进行比较 由于任务的要求,不得以任何理由更改干管 最后,我用这条线看颜色是否匹配 if(strcmp(color, d[currentDra].color[currentColor]); 在我使用下面这行之前,因为从我的第一个角度
if(strcmp(color, d[currentDra].color[currentColor]);
在我使用下面这行之前,因为从我的第一个角度来看,他们会
if(color == d[currentDra].color[currentColor])
但经过一段时间的调试,我意识到color只是一个地址
总的来说,我需要以某种方式使用地址获得color的值。
*颜色找不到值。
&颜色也不一样
函数的其余部分
void color(char *color, dragon *d)
{
char *colorList[5] = {"red","blue","white","green","yellow"};
int colorShow;
int knownColor = 1;
printf("what is color? ==== %p\n", color);
if(*color == 'r')
{
colorShow = 0;
}
else if(*color == 'b')
{
colorShow = 1;
}
else if(*color == 'w')
{
colorShow = 2;
}
else if(*color == 'g')
{
colorShow = 3;
}
else if(*color == 'y')
{
colorShow = 4;
}
else
{
printf("Sorry that is an unknown color, exiting...\n");
knownColor = 0;
}
//if a char then = numbers 0-1
//one loop for the dragons
if(knownColor)
{
printf("***All the %s dragons:***\n", colorList[colorShow]);
int currentDra;
for(currentDra = 0; currentDra < 4; currentDra++)
{
//another loop for the colors of the dragon
int currentColor;
for(currentColor = 0; currentColor < 3; currentColor++)
{
//printf("%c\n\n", (char*)color);
if(strcmp(color, d[currentDra].color[currentColor]))
{
printf("%s is %s\n", d[currentDra].name, colorList[colorShow]);
}
}
}
}
}
void颜色(char*color,dragon*d)
{
char*colorList[5]={“红色”、“蓝色”、“白色”、“绿色”、“黄色”};
int colorShow;
int knownColor=1;
printf(“什么是颜色?=%p\n”,颜色);
如果(*颜色=='r')
{
colorShow=0;
}
else if(*颜色=='b')
{
colorShow=1;
}
else if(*颜色=='w')
{
colorShow=2;
}
如果(*颜色=='g'),则为else
{
colorShow=3;
}
如果(*颜色=='y'),则为else
{
colorShow=4;
}
其他的
{
printf(“对不起,这是未知颜色,正在退出…\n”);
knownColor=0;
}
//如果是字符,则=数字0-1
//龙的一个环
如果(已知颜色)
{
printf(“***所有%s龙:**\n”,colorList[colorShow]);
int-currentDra;
用于(currentDra=0;currentDra<4;currentDra++)
{
//另一个龙的颜色循环
int-currentColor;
用于(currentColor=0;currentColor<3;currentColor++)
{
//printf(“%c\n\n”,(char*)颜色);
if(strcmp(color,d[currentDra].color[currentColor]))
{
printf(“%s是%s\n”,d[currentDra]。名称,颜色列表[colorShow]);
}
}
}
}
}
非常感谢这是我的第一个问题。if(strcmp(color,d[currentDra].color[currentColor]);
这不起作用,因为传递的color
不是以null结尾的。因此这是未定义的行为
if(color==d[currentDra].color[currentColor])
这不起作用,因为您正在比较指针,而不是它们引用的值
如果dragon.color
是一个包含单个字符串的数组,则可以与以下内容进行比较:
if(color[0]==d[currentDra].color[currentColor][0])
color
指向的值?如何定义dragon
类型定义结构dragon{char*name;char*color[3];int numHead;int numTail;}dragon;抱歉,我不知道如何在注释中对代码块进行编码:(要格式化注释中的代码,请使用向后勾选“”`…`
。但不要使用它向问题添加其他信息。相反,您的问题会将其添加到注释中。
void color(char *color, dragon *d)
{
char *colorList[5] = {"red","blue","white","green","yellow"};
int colorShow;
int knownColor = 1;
printf("what is color? ==== %p\n", color);
if(*color == 'r')
{
colorShow = 0;
}
else if(*color == 'b')
{
colorShow = 1;
}
else if(*color == 'w')
{
colorShow = 2;
}
else if(*color == 'g')
{
colorShow = 3;
}
else if(*color == 'y')
{
colorShow = 4;
}
else
{
printf("Sorry that is an unknown color, exiting...\n");
knownColor = 0;
}
//if a char then = numbers 0-1
//one loop for the dragons
if(knownColor)
{
printf("***All the %s dragons:***\n", colorList[colorShow]);
int currentDra;
for(currentDra = 0; currentDra < 4; currentDra++)
{
//another loop for the colors of the dragon
int currentColor;
for(currentColor = 0; currentColor < 3; currentColor++)
{
//printf("%c\n\n", (char*)color);
if(strcmp(color, d[currentDra].color[currentColor]))
{
printf("%s is %s\n", d[currentDra].name, colorList[colorShow]);
}
}
}
}
}