c 面试题

 

 

#include<iostream.h>
int main()
{
 char str1[]="abc";
 char str2[]="abc";
 const char str3[]="abc";
 const char str4[]="abc";
 char *str5="abc";
 char *str6="abc";
 cout<<(str1==str2)<<endl;
 cout<<(str3==str4)<<endl;
 cout<<(str5==str6)<<endl;
 return 0;
}
0
0
1


#include<stdio.h>
#include<stdlib.h>
void test1(char str[100])
{
 printf("%d\n",sizeof(str));
}
int main()
{
 char *p1="hello";
 char *p2;
 int  n;
 char *p3=(char*) malloc(100);
 char str[100];

 printf("%d\n",sizeof(p1));
 printf("%d\n",sizeof(p2));
 printf("%d\n",sizeof(n));
 printf("%d\n",sizeof(p3));
 test1(str);
 return 0;
}
4
4
4
4
4


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void test(char *p)
{
 p=(char *) malloc(100 * sizeof(char));
}
int main()
{
 char *str="helloworld";
 char *p1;
 test(p1);
 strcpy(p1,str);
 printf("%s\n",p1);
 free(p1);
}
出错


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char *test()
{
 char p[100];
 return p;
}

int main()
{
 char *str="helloworld";
 char *p1;
 p1=test();
 strcpy(p1,str);
 printf("%s\n",p1);
 free(p1);
}
出错


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char *test()
{
 char *p;
 p=(char *) malloc(100 * sizeof(char));
 return p;
}

int main()
{
 char *str="helloworld";
 char *p1;
 p1=test();
 strcpy(p1,str);
 printf("%s\n",p1);
 free(p1);
}
helloworld


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void test(char **p)
{
 *p=(char *) malloc(100 * sizeof(char));
}

int main()
{
 char *str="helloworld";
 char *p1;
 test(&p1);
 strcpy(p1,str);
 printf("%s\n",p1);
 free(p1);
}
helloword

 
const用途(至少说2种):1.修饰只读变量;2.用在函数声明部分允许const的类对象成员访问const成员;3.用来代替define,define只是简单代替,但const还会进行类型检查 4.节省空间,避免不必要的内存分配,同时提高效率


extern "C"的用法?
extern "C" 是连接申明,被extern "C"修饰的变量和函数是按照C语言方式编译和连接的。


int a[10]; &a标识号数组的首地址,a是数组首元素的首地址


for(i=0;i<N;i++)
if(condition)
DoSomething1();
else
DoSomething2();
优点:   缺点:

if(condition)
{
  for(i=0;i<N;i++)
  DoSomething1();
}
else
{
  for(i=0;i<N;i++)
  DoSomething2();
}
优点:   缺点:


bool 变量与“零值”进行比较
if(bTestFlag);
if(!bTestFlag);

float 变量与“零值”进行比较
if((fTestVal >= -EPSINON) && (fTestVal <= EPSINON)); //EPSINON 为定义好的
 
指针变量与“零值”进行比较
if(NULL == p); if(NULL != p);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值