您的位置:首页 > 编程语言 > C语言/C++

华清远见C语言学习笔记五

2012-07-07 15:39 274 查看
/*

* test.c

*

* Created on: Jul 4, 2012

* Author: 孙旭

* 华清远见实验室

*/

/******1*****/

#include<stdio.h>

int main()

{

int i=0;

int j=0;

char a[5];

for(i=0;i<5;i++)

a[j]=j++; //强烈建议不这样使用容易出现不能预料的错误

//error"operation on ‘j’ may be undefined"

for(i=0;i<5;i++)

printf("%d",a[i]);

return 0;

}

/*****2******/

#include<stdio.h>

int main() //在同一个表达式中使用导致同一对象修改两次或修改以后

{ //又被引用的自增,自减或赋值操作的任何组合

int i=7; //包含多个不缺定的副作用的代码的行为总是认为未定义的

printf("%d",i++*i++);//operation on ‘i’ may be undefined

return 0; //打印结果49

}

/*****3******/

#include<stdio.h>

int main()

{

int i=1; //包含多个不缺定的副作用的代码的行为总是认为未定义的

i=i++; //operation on ‘i’ may be undefined

printf("%d",i);

return 0;

}

/****4******/

#include<stdio.h>

int main()

{

int a=123;

int b=234; //输出结果a=234 b=123

a^=b^=a^=b; //error"operation on ‘a’ may be undefined"

printf("a=%d\n",a); //在同一个表达式修改变量的值然后再赋值的组合会出现副作用

printf("b=%d\n",b);

return 0;

}

/*****5*****/

#include<stdio.h>

int fun()

{

int a=1;

printf("this is fun\n");

return a;

}

int goo()

{

int b=2;

printf("this is goo\n");

return b;

}

int foo()

{

int c=3;

printf("this is foo\n");

return c;

}

int main()

{

int x=fun()+goo()*foo(); //对于函数调用是从左到右依次执行,不能使用运算符规则即先乘再加的运算

printf("%d",x);

return 0;

}

/******6*****/

#include<stdio.h>

int main()

{

int i=1,j;

int a[5]={1,2,3,4,5};

for(j=0;j<5;j++) //Multiple markers at this line

pirntf("a[%d]=%d",i,i++); //- operation on ‘i’ may be undefined

return 0;

}

/*****7******/

#include<stdio.h>

int main()

{

int a=65530;

int b=20;

long c=a*b; //LINUX 下可以这样计算

printf("%ld",c);

return 0;

}

/******8*******/

#include<stdio.h>

#include<string.h>

void fun(int a[3][5]) //行参接受的是地址不能直接计算他的长度

{

int *p=&a[0][0];

int i=sizeof(a); //使用strlen(a)是计算的是地址的长度为4

int j=strlen(p); //间接使用指针来计算他的长度 值为15

int (*p)[5]=a;

int k=strlen(p); //间接使用指针来计算他的长度 值为15

printf("%d",i);

}

int main()

{

int b[3][5];

fun(b);

return 0;

}

/********9*********/

#include<stdio.h>

int main()

{

int x[2][3]={1,2,3,4,5,6};

int (*p1)[3]=x;

printf("%d\n",*p1);

p1++;

printf("%d",*p1);

return 0;

}

/*******10*****/

#include<stdio.h>

int main()

{

int x=20;

short y=28;

void *p1;

void *p2;

p1=&x;

p2=&y; //强制类型转化

printf("%d\n",*(short *)p1); //对与空类型指针只能由高到低转化 20

printf("%d\n",*(int *)p2); //不能由低向高转化 -2076180452

printf("%d\n",*(int *)p1); //20

printf("%d\n",*(short *)p2); //28

return 0;

}

/******11******/

#include<stdio.h>

int main()

{

int x=22;

short y=33;

int p1=(short)y; //22

short p2=(int)x; //33

printf("%d\n",p1);

printf("%d\n",p2);

char a[]="1234\0567";

char b[]="1234567\0";

char c[]="1234567";

int i=sizeof(a);

int j=sizeof(b);

int k=sizeof(c);

printf("a=%d\n",i); //7

printf("b=%d\n",j); //9

printf("c=%d\n",k); //8

return 0;

}

/******12*******/

#include<stdio.h>

int main()

{

int n=3,a
; //linux下数组内存分配是连续的,不会出现段错误

a[10]=10;

printf("%p\n",a); //0xbff06330

printf("%d\n",a[10]); //10

printf("%p\n",&a[10]); //0xbff06358

return 0;

}

/******13******/

#include<stdio.h>

void fun(int a[][3])

{

char (*b)[3]=a;

b[0][0]=4;

}

int main()

{

int a[3][3]={0};

int i,j;

fun(a);

for(i=0;i<3;i++)

for(j=0;j<3;j++)

printf("%d ",a[i][j]);

return 0;

}

/*******14**********/

#include<stdio.h>

int main()

{

int x[2][3]={1,2,3,4,5,6};

int (*p1)[3]=x;

printf("%d\n",*p1);

p1++;

printf("%d",*p1);

return 0;

}

/*****15****/

#include<stdio.h>

int main()

{

int i=0,k=0;

for(i=0;i<22;i++)

{

int j;

for(j=2;j<5;j++)

printf("j=%d ",j);

}

for(k=0;k<5;k++)

{

int x;

for(x=0;x<5;x++) //局部变量的作用域从定义的括号到大括号结束

{

while(j--) //error"‘j’ undeclared (first use in this function)"

printf("j===%d \n",j);

}

}

return 0;

}

/*******16******/

#include<stdio.h>

int i=3;

int main()

{

int i=4;

int j=::i; //不能这样使用全局变量 ::i 这是针对c++的

printf("%d",j);

return 0;

}

/******17******/

#include<stdio.h>

int main()

{

for(;1;) //相当与while(1)

printf("------\n");

return 0;

}

/******18*****/

#include<stdio.h>

static int x=0;

int fun()

{

//int x=0

x++;

return x;

}

int main()

{

int sum=0;

int j=0;

for(;j<2;j++)

sum+=fun();

printf("%d",sum); //3 //2

return 0;

}

/******19******/

#include<stdio.h>

typedef union {

long i;

int k[5]; //sizeof(s1)=20

char c;

}s1;

typedef struct date{

int cat;

s1 cow; //sizeof(s2)=32

double c;

}s2;

typedef struct data{

int cat;

s2 cow; //sizeof(s3)=44

double c;

}s3;

int main()

{

int i=sizeof(s3);

printf("%d",i);

return 0;

}

/*******20******/

#include<stdio.h>

#include<string.h>

#include<malloc.h>

int main()

{

char *src="hello,world";

char *dest=NULL;

int len=strlen(src);

dest=(char *)malloc(len+1);

char *d=dest; //

printf("%p\n",dest);

printf("%p\n",d);

char *s=&src[len-1];

while(len--!=0)

*d++=*s--;

*d=0; //尾部要加0

//printf("%s\n",d); //d指针指向末尾

printf("%s",dest);

free(dest);

return 0;

}

/*******21********/

/*创建单个节点*/

#include<stdio.h> /*二叉树*/

#include<malloc.h>

typedef struct tree

{

int data;

struct tree *lchild;

struct tree *rchild;

}tree;

tree *p=NULL;

void initTree(tree **t)

{

*t=(tree*)malloc(sizeof(tree));

(*t)->data=10;

(*t)->lchild=NULL;

(*t)->rchild=NULL;

}

tree * createTree(tree *t)

{

tree *t1;

int data;

initTree(&t);

t1=t;

puts("请输入节点的内容:");

scanf("%d",&data);

while(1)

{

if(t1->data==0)

break;

else if(data<t1->data)

{

if(t1->lchild==NULL)

{

initTree(&t1->lchild);

t1=t1->lchild;

t1->data=data;

break;

}

else

{

t1=t1->lchild;

}

}

else if(data>t1->data)

{

if(t1->rchild==NULL)

{

initTree(&t1->rchild);

t1=t1->rchild;

t1->data=data;

break;

}

else

{

t1=t1->rchild;

}

}

}

printf("%d\n",t1->data);

return t1;

}

void printTree(tree *t)

{

printf("%d",t->data);

}

int main()

{

tree *head;

head=createTree(p);

printTree(head);

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: