#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);