C - 当您将取消引用的值传递给函数以便稍后与另一个字符串进行比较时,如何获取原始值?
C - How to get original value of when you pass a dereferenced value to a function to later be compare with another string?
我知道作业帮助是回避的,但是,我有强烈的编码障碍。
我最需要帮助理解。
所以当我获取变量的地址时 (&c) 我知道我得到了它在内存中的位置的地址,但我不知道如何取消引用它address 以便访问其特定值 ('b') 在函数中进行比较 (color(&c, total) 它是用于.
因作业要求,不能以任何理由更改主体
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]);
在我使用下面的行之前,因为从我的第一个角度来看,它们会 char
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]);
}
}
}
}
}
非常感谢,这是我的第一个问题。
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])
我知道作业帮助是回避的,但是,我有强烈的编码障碍。
我最需要帮助理解。
所以当我获取变量的地址时 (&c) 我知道我得到了它在内存中的位置的地址,但我不知道如何取消引用它address 以便访问其特定值 ('b') 在函数中进行比较 (color(&c, total) 它是用于.
因作业要求,不能以任何理由更改主体
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]);
在我使用下面的行之前,因为从我的第一个角度来看,它们会 char
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]);
}
}
}
}
}
非常感谢,这是我的第一个问题。
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])