字符串的两种不同风格: C++风格和C风格
这个提法有点怪异,但还是常常出现:
char *p = "abcd";
和
string str = "abcdefg";
第一个叫做C风格的字符串,原因是有null作为结尾; 第二个为C++风格的, 不是以null结尾.
实质上: C风格的字符串是:
char[] pArr = {'a', 'b', 'c', 'd', '\0'};
这样决定了处理方式的不同
相关文档:
1.下面哪种代码风格更好,why?
A . if ('A' == a)
{a++;}
B. if( a == 'A')
{a++;}
答案:A,如果把==错写成=,因为编译器不允许对常量赋值,容易差错。
2.#define MUTI(x) (x*x)
int i=3,j, ......
最近在开发中,对常量参与运算时候,出了几个问题,特记录如下:
1.例子一(KEIL-51)
unsigned char recsum,xorsum;
recsum == 0xFF;
xorsum == 0x00;
if(recsum != (xorsum-1)) //这时候不相等
if(recsum != (unsigned ch ......
#include<stdio.h>
#include<stdarg.h>
#include<string.h>
void demo(char *msg,...)
{
va_list argp;
int arg_number=0;
char *para = msg;
va_start(argp,msg);
while(1){
if ( strcmp( para, "\0") != 0 ) {
arg_number++;
printf("parameter %d is: %s\n",arg_number,p ......
华为C/C++笔试题2 收藏
1. 某32位系统下, C++程序,请计算sizeof 的值
#include <stdio.h>
#include <malloc.h>
void Foo ( char str[100] )
{
printf("sizeof(str)=%d \n", sizeof(str) );//此处使用char *str与char str[100]是一样的,char str[100]不指明大小(char str[]) ......