我知道作业帮助是被回避的,然而,我有强烈的编码障碍。
我最需要的是帮助理解。
所以当我获取变量的地址时(
&c
)我知道我得到了一个地址到它在内存中的位置,但我不知道如何取消对该地址的引用以访问它的特定值(
“b”
)在函数中进行比较(
颜色(&c),总计
)在中使用。
由于任务的要求,不能以任何理由更改干管
typedef struct dragon
{
char *name;
char *color[3];
int numHead;
int numTail;
}dragon;
void color(char* color, dragon *d);
int main()
{
dragon total[4];
dragon_info(total);
char c = 'b';
color(&c, total);
return 0;
}
最后,我用这条线来看看颜色是否匹配
if(strcmp(color, d[currentDra].color[currentColor]);
在我使用下面这行之前,因为从我的第一个角度来看,他们会
if(color == d[currentDra].color[currentColor])
但经过一段时间的调试,我意识到
颜色
只是个地址
总的来说,我需要以某种方式获得
颜色
以某种方式使用地址。
*颜色找不到值。
&颜色也不一样。
函数的其余部分
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]);
}
}
}
}
}
非常感谢,这是我第一个问题。