请在下面的代码段中找到错误。 [英] Please find the bugs in below code snippents.
问题描述
1。 char * PrintMe(){
char str [12];
strncpy(str,hello world,11);
str [ 11] = 0;
返回str;
}
2. struct my_struct {int val; };
int my_func(struct my_struct * ptr,int x,int y){
int z = 0;
if((ptr!= NULL)&&(x< y)||(ptr-> val> 0)){
z = x + y - ptr-> val;
}
返回z;
}
3. class base {
public:
int x ;
};
类派生:公共基地{
public:
int y;
};
void init(Base * b){
for(int i = 0; i< 3; ++ i){
b [i] .x = 0;
}
}
void foo(){
派生的arr [3];
init(arr);
}
我的尝试:
我来知道代码中的错误。
1. char * PrintMe() {
char str[12];
strncpy (str, "hello world", 11);
str[11] = 0;
return str;
}
2. struct my_struct { int val; };
int my_func( struct my_struct *ptr, int x, int y) {
int z = 0;
if((ptr != NULL) && (x < y) || (ptr->val > 0)) {
z = x + y - ptr->val;
}
return z;
}
3. class Base {
public:
int x;
};
class Derived : public Base {
public:
int y;
};
void init(Base *b) {
for( int i = 0; i < 3; ++i) {
b[i].x = 0;
}
}
void foo() {
Derived arr[3];
init(arr);
}
What I have tried:
Let me know the bugs in the code.
推荐答案
正在按计划学习一些东西。所以你应该将它用于学习C ++ 。重要的还有调试器的使用,这也是解释的。
代码中有一些错误和一些气味(坏样式): - O
Homeworks are on schedule for learning some stuff. So you should use it for Learning C++. Important is also the use of the Debugger which is also explained.
There are some bugs and some smells (bad style) in the code :-O
这篇关于请在下面的代码段中找到错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!