1.class和struct的默认权限分别是什么?
class:private
struct:public
2.const和static的作用,说的越多越好
3.c语言中链表
struct node{
int value;
struct node * next;
}
typedef struct node node;
现在给出链表的头部head,代码实现该链表的翻转。
4.发现代码的错误。
(1)
void test1()
{
char string[10];
char* str1="0123456789";
strcpy(string,str1);
}
str1长度为11(加上'\0')
string长度只有10,所以无法存储'\0',输出时会有乱码。
(2)
void GetMemory(char *p)
{
p=(char *)malloc(100);
}
void Test(void){
char * str=NULL;
GetMemory(str);
strcpy(str,"hello world");
printf(str);
}
测试:
void GetMemory(char *p)
{
qDebug()<<"p:"<<&p;
p=(char *)malloc(100);
}
void Test(void){
char * str=NULL;
qDebug()<<"str:"<<&str;
GetMemory(str);
if(str==NULL){
qDebug()<<"NULL";
}
strcpy(str,"hello world");
printf(str);
qDebug()<<str;
}
输出:
str: 0x65fe14
p: 0x65fe00
NULL
C++——函数传参三种形式(包含形参与实参)_c++形参到实参的传递方式-CSDN博客
形参是额外分配的内存
(3)
char * GetMemory(void)
{
char p[]="hello world";
return p;
}
void test(void){
char * str=NULL;
str=GetMemory();
}
return p;返回的是字符数据p的地址,函数调用结束后,该数组被销毁。
返回的指针是一个已经释放了空间的指针。
char * GetMemory(void)
{
char *p="hello world";
return p;
}
先定义一个字符串常量“hello world”,指针p指向它,字符串常量在编译时分配内存,程序退出时才被销毁。
测试:
char * GetMemory(void)
{
char p[]="hello world";
qDebug()<<"p:"<<&p;
return p;
}
void test(void){
char * str=NULL;
qDebug()<<"str:"<<&str;
str=GetMemory();
qDebug()<<"str:"<<&str;
}
输出:
str: 0x65fe14
p: 0x65fdcc
str: 0x65fe14
c函数中返回字符串数组、char[]和char*的区别与转换(详细)_char *和char[]返回值的区别-CSDN博客
(C++)三种常用的字符串表示方法——char* 和 char[] - 小念子 - 博客园 (cnblogs.com)
5.写出输出的内容。
class A{
public:
A(){std::cout<<"A:"<<std::endl;}
~A(){std::cout<<"~A:"<<std::endl;}
virtual void func(){std::cout<<"AFunc:"<<std::endl;}
};
class B:public A
{
int mmm;
public:
B(){std::cout<<"B:"<<std::endl;}
~B(){std::cout<<"~B:"<<std::endl;}
void func(){std::cout<<"BFunc:"<<std::endl;}
};
A * a=new B();
a->func();