易截截图软件、单文件、免安装、纯绿色、仅160KB

C/C++ 面试题

第一题:
下面程序的输出结果?
#include <stdio.h>
#include <iostream>
void main()
{
char str1[] = "";
char str2[] = "";
const char str3[] = "abc";
const char str4[] = "abc";
const char* str5 = "abc";
const char* str6 = "abc";
char* str7="abc";
char* str8="abc";
std::cout << std::boolalpha << ( str1==str2 ) << std::endl; // 输出什么?
std::cout << std::boolalpha << ( str3==str4 ) << std::endl; // 输出什么?
std::cout << std::boolalpha << ( str5==str6 ) << std::endl; // 输出什么?
std::cout << std::boolalpha << ( str7==str8 ) << std::endl; // 输出什么?
int i;
 scanf("%d",&i);
}
结果:false,false,true,true. 因为前两个的内存是在heap上分配的,当然是不同的地址;而后两个是在静态区分配的,后面的先检查前面有无该字符串,有的话,就不再分配,所以str5,str6,str7,str8地址都是相同的。
第二题:
1.检查下面程序是否有错误并写出运行结果
(1).
void GetMemory(char *p)
{
    p = (char *)malloc(100);
}
void Test(void)
{
    char *str = NULL;
    GetMemory(str);
    strcpy(str, "hello world");
    printf(str);
}
(2).
char *GetMemory(void)
{
    char p[] = "hello world";
    return p;
}
void Test(void)
{
    char *str = NULL;
    str = GetMemory();
    printf(str);
}
(3).
void GetMemory2(char **p, int num)
{
    *p = (char *)malloc(num);
}
void Test(void)
{
    char *str = NULL;
    GetMemory2(&str, 100);
    strcpy(str, "hello");
    printf(str);
}
结果:(1)程序崩溃。因为str并未被赋值,仍然是0;
       (2)可能是乱码。因为"hello world"过了生命期,不在内存了


相关文档:

转载:Hadoop 应该用C++实现,而不是Java

http://www.trendcaller.com/2009/05/hadoop-should-target-cllvm-not-java.html
Sunday, May 10, 2009
Hadoop should target C++/LLVM, not Java (because of watts)
< type="text/javascript">
digg_url="http://www.trendcaller.com/2009/05/hadoop-should-target-cllvm-not-java.html";
Over the years, ......

c 语言中的字符串操作

http://blog.chinaunix.net/u1/41817/showart_342390.html
6.5       
怎样将字符串打印成指定长度
?   
 
   
如果要按表格形式打印一组字符串,你就需要将字符串打印成指定长度。利用
printf()函数可以很方便地实现这一点,请看下例
......

ubuntu下编译c/c++

若想在ubuntu下编译c/c++代码
首先,安装g++和gdb,可以在新立得中直接安装
若要编译c,如:
#include
<stdio.h>
int main()
{
   
printf("Hello,World!\n");
    return 0;

}




......

c 读取文件

#include <stdio.h>
int main ()
{
    FILE *fp1;
    fp1=fopen("test0.txt","rt");
    if(fp1==NULL)
    {
        printf("can not open test0.txt\n");return 0;
 &n ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号