整理笔记 C语言

1.C库与系统调用的区别

库函数是高层的,完全运行在用户空间,为程序员提供调用真正的在幕后完成实际事务的系统调用的更方便的接口。系统调用在内核态运行并且由内核自己提供。标准C库函数printf()可以被看做是一个通用的输出语句,但它实际做的是将数据转化为符合格式的字符串并且调用系统调用 write()输出这些字符串。      

2. 常见编译错误

(1)missing terminating " character   缺少表示终止的"字符”

(2)stray ‘\’ in progrch                 该错误是指源程序中有非法字符

(3)undefined reference to          该函数未定义      

(4)too few arguments to function   函数参数不对,少了

3.数组与指针

参数传递时都转换为指向数组起始地址的指针,而其他的参数均采用传值调用

函数的返回值绝不能是一个函数数组,而只能是指向数组或函数的指针

 

声明一个10×20的多维字符数组 

char carrot[10][20];

或者声明一种看上去更像“数组的数组”形式:

    typedef char vegetable[20];

    vegetable carrot[10];

不论哪种情况,访问单个字符都是通过carrot[i][j]的形式,

编译器在编译时会把它解析为*(*(carrot+i)+j)的形式

 

只有字符串常量才可以初始化指针数组

 

向函数传递一个多维数组

my_function(int my_array[10][20]);

Func(a); 

my_function(int my_array[][20])

Func(a);

4 指针

绝对不能间接引用一个空指针

 

如果两个指针向同一个数组,它们就可以相减,其为结果为两个指针之间的元素数目

 

定义:指针函数

指带指针的函数,即本质是一个函数

返回类型标识符 * 返回名称(形式参数表) { 函数体 }

 

定义:函数指针

指向函数的指针变量

数据类型标志符 (*指针变量名)(参数)

5.链表操作

 

头插

         struct node *head=NULL,*p=NULL;

         int n;

         scanf("%d",&n);

         while(n!=0&&n!=32767)

         {

            p=(struct node*)malloc(sizeof(struct node));

            p->data=n;

            p->next=NULL;

            p->next=head;

            head=p;

            scanf("%d",&n);

 

         }

         return head;

     }

尾插

struct node *createlist()

      {

       struct node *head=NULL,*p=NULL,*h=NULL;

       int m;

       scanf("%d",&m);

       while(m!=0&&m!=32767)

       {

            p=(struct node*)malloc(sizeof(struct node));

            p->data=m;

            p->next=NULL;

            if(head==NULL)

            {

               head=p;

               h=p;

            }

            else

            {

               h->next=p;

                     h=p;

            }

            scanf("%d",&m);

       }

       return head;

}

显示

void print(struct node *p)

     {

           while(p->next!=NULL)

           {

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

             p=p->next;

           }

           if(p->next==NULL)

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

     }  */

查找

int find(struct node*p, int i)

     {    int j=1;struct node *q=NULL;

          while  (p!=NULL&&j<i)

          {       p=p->next; j++;  }

          if(j!=i)  return 0;

          else      return p->data;

 

     }

 

 

删除

struct node *delete(struct node *p,int i)

     {    int j=1;

          struct node *q,*head;

          q=head=p;

          while(p!=NULL&&j<i)

          { 

              p=q; q=q->next;j++;

          }

              if(j!=i) printf("i is too big");

              else

             {

              p->next=q->next;

              free(q);

             }

 

           printf("**************\n");

           print(head);

     }

插入

struct node *insert(struct node *p,int i,int m)

     {

         int j=1;struct node *q=NULL,*head=NULL;

         head=p;

      //   p=p->next;

         while(p!=NULL&&j<i)

         {     p=p->next;j++;}

         if(j!=i)printf("i is too big or i is 0\n");

          else

         {   q=(struct node*)malloc(sizeof(struct node));

             q->data=m;

             q->next=p->next;

             p->next=q;

         }

         print(head);

 

     }

 

两个链表的插入,插入后仍然有序;

#include"stdio.h"

    void swap(int *a,int *b)

    {

        int t;

        t=*a;*a=*b;*b=t;

    }

    void f(int a[],int n)

    {   int i;

        for(n=6;n>1;n--)

        {

           for(i=n/2;i>=1;i--)

           {

              if((2*i+1<=n)&&a[i]>=a[2*i+1]) swap(&a[i],&a[2*i+1]);

              if(a[i]>=a[2*i])              swap(&a[i],&a[2*i]);

           }

          swap(&a[n],&a[1]);

        }

        //swap(&a[n],&a[1]);

 

   }

    int main()

    {   int i;

        int a[7];

        for(i=1;i<7;i++)

        scanf("%d",&a[i]);

        f(a,6);

        for(i=1;i<7;i++)

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

 

    }

 

6.快速排序

Int Quick_sort(int a[],int low,int high)

{

   Int pivo;

   Int oldlow=low,oldhigh=high;

   Pivo=a[low];

   High=a+high;

   While(low<high)

   {

        If(*high<pivo)

         {

              *low=*high;

               Low++;

               *high=*low;

         }

         Else high--;

   }*low=pivo;

   Quick_sort(a,oldlow,low-1);

   Quick_sort(a,low+1,oldhigh);

}

把递归变成非递归要用堆栈;

Void f_quick_sort(int a[],int n)

{

     Int stack[100],top=0;

     Stack[top++]=0;

     Stack[top]=n;

     While(top!=0)

     {

           High=stack[top--];

           Low=stack[top];

          // todo one_quick_sort code;

        If(low>0)

        {

            Stack[top++]=0;

            Stack[top]=low-1;

        }

        If(low+1<n)

        {  stack[top++]=low+1;stack[top]=n;

             

        }

       

     }

}

7.通用栈

1 #include <stdio.h>

  2 #include<stdlib.h>

  3 struct list

  4 {

  5         struct list *prev,*next;

  6 };

  7 struct stack

  8 {

  9         struct list *top;

 10 };

 11 struct stack *stack_init(struct stack *stack)

 12 {

 13         stack->top=NULL;

 14         return stack;

 15 }

 16 struct stack *stack_creat()

 17 {

 18         struct stack *st;

 19         st=(struct stack*)malloc(sizeof(struct stack));

 20         return stack_init(st);

 21 }

 22 int stack_push(struct stack *st,struct list *list)

 23 {

24         if(list==NULL)

 25                 return 0;

 26         list->next=st->top;

 27         st->top=list;

 28         return 1;

 29 }

 30 int stack_pop(struct stack *st,struct list **list)

 31 {

 32         if(st->top==NULL)

 33                 return 0;

 34         *list=st->top;

 35         st->top=st->top->next;

 36         return 1;

 37 }

 38 #define list_entry(ptr,type,member)\

 39          ((type*)((char *)(ptr)-(char *)(&((type *)0)->member)))

 40 struct data

 41 {

 42         int s;

43         int i;

 44         struct list list;

 45 };

 46 int main()

 47 {

 48         struct stack *st=stack_creat();

 49         struct data *p,*n=(struct data*)malloc(sizeof(struct data));

 50         n->i=10;

 51         n->s=8;

 52         struct list *l;

 53         stack_push(st,&n->list);

 54         stack_pop(st,&l);

 55         p=list_entry(l,struct data,list);

 56         printf("%d %d\n",p->s,p->i);

 57         free(p);

 58         free(st);

 59         return 1;

 60 }

8.二叉树

#include <stdio.h>
#include <conio.h>          
#include <stdlib.h>

typedef struct BitNode
{
 char data;
 struct BitNode *lchild,*rchild;
}BitNode,*BiTree;

void CreateBiTree(BiTree &);   
void FirstOrder(BiTree);        
void MiddleOrder(BiTree);       
void LastOrder(BiTree);         


void main()
{
 BiTree  T;
 int flag=1;
 char j;
 printf("tree works:\n");
 printf("ge:ABC  DE G  F   ,set up tree:\n");
 printf("           A      \n");
 printf("          /       \n");
 printf("         B        \n");
 printf("        / \\       \n");
 printf("       C   D      \n");
 printf("          / \\     \n");
 printf("         E   F    \n");
 printf("          \\       \n");
 printf("           G      \n");
 CreateBiTree(T);    
 getchar();
 while(flag)
    {
  printf("please choose: \n");
  printf("1.first\n");
  printf("2.second\n");
  printf("3.last\n");
  printf("4.exit\n");
  scanf(" %c",&j);
  switch(j)
  {
  case '1': if(T)
      {
       printf("%c",T->data);            
       FirstOrder(T->lchild);           
       FirstOrder(T->rchild);            
      }
   else printf("the tree is empty!\n");
   break;
  case '2': if(T)
      {
       MiddleOrder(T->lchild);           
       printf("%c",T->data);             
       MiddleOrder(T->rchild);           
      }
   else printf("the tree is empty!\n");
   break;  
  case '3': if(T)
      {
       LastOrder(T->lchild);             
       LastOrder(T->rchild);             
       printf("%c",T->data);              
      }
   else printf("the tree  is empty !\n");
   break;       
  default: flag=0; printf(" any key exit!\n"); exit(0);
  }
    }
 getcher();
}

void CreateBiTree(BiTree &T)
{
 char ch;
 scanf("%c",&ch);     
 if(ch==' ') T=NULL;
 else
 {
  T=(BitNode *)malloc(sizeof(BiTree));    
  T->data=ch;
  CreateBiTree(T->lchild);                
  CreateBiTree(T->rchild);                 }
}
void FirstOrder(BiTree T)
{
 if(T)
 {
  printf("%c",T->data);            
  FirstOrder(T->lchild);            
  FirstOrder(T->rchild);            

 }
}
void MiddleOrder(BiTree T)
{
 if(T)
 {
  MiddleOrder(T->lchild);           
  printf("%c",T->data);              
  MiddleOrder(T->rchild);            
 }
}
void LastOrder(BiTree T)
{
 if(T)
 {
  LastOrder(T->lchild);            
  LastOrder(T->rchild);           
  printf("%c",T->data);            
 }
}
9   const

Const  type 变量名

Const int a;是对a的限制

Const int *a;不是,a可以变,但地址不能变

Const int *const a;对*a确定

Eg: int const *p1,p2;*p1and p2 is const 修饰,但是不对p1

Int const(*const p1),p2;p1 and p2 and *p1 all 修饰by const

Int  (*const p1),p2; p2 and const has no relaction;

Const *p1=&m;

P1=&y;可以

&p1=66;不可以

int *const p1=&x;

p1=&y;不可以

*p1=88;可以

Const int *const p1=&x;可以

C中只是定义不初始化是可以的,而c++不行

10  文件

    读取文本文件的内容,并加上行号显示

    Int main(int argc,char *argv[])

    {

    char ch[SIZE];

        int c,line;

        FILE *fp;

        if(argc!=2)

        {

        printf("you forgot to enter a filemane\n");

        exit(0);

        }

        if((fp=fopen(argv[1],"r"))==NULL)

        {

        printf("File %s cannot open\n",argv[1]);

        exit(0);

        }

        line=1;

        while(fgets(ch,SIZE,fp)!=NULL)

        printf("%4d\t%s\n",line++,ch);

        fclose(fp);

     }

从键盘输入4个学生数据,把他们转存到磁盘文件中去。

#include <stdio.h>

#define SIZE 4

struct student_type

{    char name[10];

     int num;

     int age;

     char addr[15];

}stud[SIZE];

main()

{

     int i;

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

                         scanf("%s%d%d%s",stud[i].name,&stud[i].num,

                                    &stud[i].age,stud[i].addr);

     save();

     display();

}

void save()

{   FILE *fp;

     int  i;

     if((fp=fopen(“c:\\stu_list","wb"))==NULL)

     {    printf("cannot open file\n");

                           exit(0);

     }

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

         if(fwrite(&stud[i],sizeof(struct student_type),1,fp)!=1)

                              printf("file write error\n");

     fclose(fp);

}

 

void display()

{   FILE *fp;

     int  i;

     if((fp=fopen(“c:\\stu_list","rb"))==NULL)

     {    printf("cannot open file\n");

                           exit(0);

     }

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

     {   fread(&stud[i],sizeof(struct student_type),1,fp);

         printf("%-10s %4d %4d %-15s\n",stud[i].name,

                 stud[i].num,stud[i].age,stud[i].addr);

     }

     fclose(fp);

}

11 简单总结

(1

读写函数总结

1 int  fread(void  *buffer,int num_bytes, int  count,FILE   *fp);

2 int  fwrite(void  *buffer,int  num_bytes, int  count,FILE   *fp)

 读/写数据块fread与fwrite 一般用于二进制文件的输入/输出。

3 int  fscanf(FILE  *fp, char  *format, arg_list)

4 int  fprintf(FILE  *fp,const char  *format, arg_list)

  Txt文本可以看到输入数据

5fscanf(stdin,“%s%d”,s,&a);  /*键盘输入*/

6fprintf(stdout,“%s %d”,c,b);/*屏幕显示*/

7int  fputc(int ch, FILE *fp)输入一个字符到文件

8int  fgetc(FILE *fp)      输出一个字符

9ssize_t read(int fd,char *buf,size_t n);(#include <unistd.h>

10ssize_t write(int fd,char *buf,size_t n);

(2

ipcs   –m   查看虚拟内存

%s/a/b/g     用b替换a

[root@localhost ~]# cd /mnt/hgfs/    查找共享文件

通过return语句,函数可以带回一个或一个以上的返回值。(   )结构体,形参

链表不支持随机访问,只能通过前一个元素的指针域得知后一个元素的地址,因此只能从头指针开始顺序访问各节点。

一个数组就是一个地址

一个指针就是一个地址的地址

Typedef 是用来定义变量的,不能用变量定义变量

函数的定义和函数的调用都可以嵌套

Continue 只结束本次循环跳过下面为执行的语句,进行下一次循环判定;

break结束整个循环

p = a; p[i];        p = a;*(p+i);       p = a+i;*p;

数组作为sizeof()的操作数一显然此时需要的是整个数组的大小,而不是指针所指向的第一个元素的大小。

使用&操作符取数组的地址。

如果有随机值,可能是有定义但未初始化的变量。

Malloc 在堆中定义要free

避免指针声明不初始化

Free(p)释放后马上那个置空

不要忘记为数组和动态内存赋初值

不要用return语句指向栈内空间

双层循环最长循环放内层,最短放外层;

posted on 2013-08-28 11:54  寻找心的巨人  阅读(617)  评论(0编辑  收藏  举报

导航