这是一棵三叉树,我想实现一个搜索来查找 id 是否在树中。该函数在当前状态下返回 NULL(这很好),即使我没有在函数末尾添加 return NULL 行,为什么?
struct T {
int id;
char * name;
struct T *left;
struct T *middle;
struct T *right;
};
T* findId(T* root, int id) {
if (root == NULL) {
return NULL;
}
if (root->id == id) {
return root;
}
T* sLeft = findId(root->left, id);
if (sLeft != NULL) {
return sLeft;
}
T* sMiddle = findId(root->middle, id);
if (sMiddle != NULL) {
return sMiddle;
}
T* sRight = findId(root->right, id);
if (sRight …Run Code Online (Sandbox Code Playgroud) 这是一个来自测试的问题。
int main() {
const int i=1;
const int *p=&i;
int j=2;
const int *q=&j;
j=3;
printf("%d", *p+*q);
int k = 0;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
据我了解,双方p并q都指向一个const int。
我认为当我声明指向非常量值的指针时会出现编译错误,但它工作正常;这是为什么?
#include <stdio.h>
int main()
{
repeat:
printf("choose an option: \n1:Draw\n2:Even or Odd:\n3:Text type\n4:How to Dec\n5:Base to Dec:\n6:Count to Bits\n0:Exit\n");
int x;
scanf("%d", &x);
if (x > 6)
printf("Wrong option!\n");
goto repeat;
if (x == 1)
printf("Drawing");
if (x == 0)
printf("we are out!\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我正在使用在线编译器。
我想看到的最终结果是一个简单的菜单,它要求用户输入数字并根据数字执行代码,然后转到起点并再次打印菜单。(这就是我选择 goto 的原因。如果您有其他解决方案可以在执行后重新启动主程序,请告诉我。)
我遇到的问题是,无论我输入多少数字,它都会再次输出菜单而没有消息或不退出(当我输入0时)。
这是以下代码:
int main () {
int x = 3, y = 0 ;
if(x >= 3 && y+=1){
if(y<=5){
printf("%d\n", ++x);
}
}
else{
printf("%d\n", x++);
}
printf("x=%d, y=%d", x, y++);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它不起作用,并且y+=1是它的原因,我认为它的行为方式与我将其替换为++y(有效)相同。
为什么如果我用 ++y 替换 y+=1 代码有效?为什么 y+=1 导致代码失败?