哎呦魏

博客园 首页 新随笔 联系 订阅 管理

2021

************************3.9**************************

scanf("a=%d,b=%d",&a,&b);    //输入的时候要a=1,b=2    ,不能写成1,2     编译不出来,输入的要和双引号的一样

 


 

2021-03-10

1、一个指针函数实现多态显示

 

#include <stdio.h>


void main()
{
int max(int, int);        /函数定义,没写这个函数
int min(int, int);
int add(int, int);

 

void polymorphic(int a, int b, int(*p)());

 

int a,b;

 

printf("endter a and b: ");
scanf("%d,%d",&a,&b);
printf("max= ");
polymorphic(a,b,max);

 

printf("endter a and b: ");
scanf("%d,%d",&a,&b);
printf("min= ");
polymorphic(a,b,min);

 

printf("endter a and b: ");
scanf("%d,%d",&a,&b);
printf("add= ");
polymorphic(a,b,add);
}
}

二、

#include <stdio.h>
 
void main()
{
    double score[][4]={ {1,2,3,4},{4.6,2,8,1},{5,67,24,67},{45,87,12,98}};
    double *search(double(*poiter)[4], int n );
    double *p;
    int i, m;

    printf("Please wnter the numder of student: ");
    scanf("%d",&m);

    printf("The scores of No.%d are: \n", m);

    p = search(score, m);

    for(i=0; i<4; i++ );
    {
        printf("%5.2f\t",*(p+i ));
    }

    printf("\n\n\n");
}

double *search(double (*pointer)[4], int n)    //数组指针这不太懂
{
    double *pt;

    pt = *(pointer + n);

    return pt;
}

数组指针忘了是咋回事?

1 //一维数组
2 int a[4],* p=a;      //p指针指向a数组的首地址了
3 
4 inv(a, 10);         //引用
5 inv( int *x, int n)   //子函数
6 {        }
7 
8 int (*p)[4];    //定义指向包含4个元素的一维数组指针变量

 


2021-03-11    11:08:38

汇编中ebp是在栈中的变量

**p    是可以用p指向指针的


2021-03-14    08:17:48

void 指针      可以指向任何类型的数据

const指针    是不能被改变的指针

 

函数    memcpy 

         void  *memcpy(void *dest, const void *src ,size_t count)    (复制后的地址 , 复制内容的地址 , )

sizeof ( 变量 )    ??取占字节大小

 


 




预处理

#define       开始

#undef        结束

宏定义名用大写  , 以便于区分变量的区别

#include < stdio.h >

#define  PIN1  char*
typedef  char*  PIN2;           

void main()
{
    PIN1 x , y;        //相当于char* x,y
    PIN2 a , b;        //相当于char* a,char* b

    printf("%d    %d\n",sizeof( x ),sizeof( y ));
    printf("%d    %d\n",sizeof( a ),sizeof( b ));
}

 


一、带参数的宏定义

宏定义的字符一般带括号,减少错误

   #define  M( y )   y*y+3*y     //宏定义格式      M和(y)中间不能有空格

    k = M(5);                 //宏调用 是实参代替形参,不是值传递

函数:

#include <string.h >

strcat( a, b );             //把b复制给a的后面        可以实现连接   字符串连接     

 

二、高级注释方法、

#if  0

注释内容

#endif      注释结束

 




 

结构体与共用体

一、一般形式

struct  结构名            //结构名可以没有

{

  成员列表                         //类型说明符    成员列表

};

 

#include <stdio.h>

void main()
{
    struct student         //定义结构
    {
        int num;
        char *name;
        char sex;
        float score;
    }boy1, boy2 = {182, "Jane", 'M', 98.5};     //定义,初始化

    boy1 = boy2;

    printf("Nunber = %d\nName = %s\nscore = %d \n",boy1.num, boy1.name, boy1.score);
    printf("\n\n");
    printf("Nunber = %d\nName = %s\nscore = %d \n",boy2.num, boy2.name, boy2.score);
}

 

 

                                            存储号码小程序

#include <stdio.h>

#define NUM 3

struct person         //定义结构
{
    char name[20];
    char phone[10];
};

void main()
{
    struct person man[NUM];              //定义
    int i;
    for(i=0; i< NUM; i++)
    {
        printf("input name:\n");
        gets(man[i].name);
        printf("input phone:\n");
        gets(man[i].phone);
    }

    printf("\tname\t\t\t\tphone\n\n");

    for(i=0; i < NUM; i++)
    {
        printf("%20s\t\t\t%20s\n",man[i].name,man[i].phone);
    }

    system("pause");
}

 

 

                                                                    三种表达方式

#include <stdio.h>

struct stu         //定义结构
{
    int num;
    char *name;
    char sex;
    float score;
}boy1 = {102, "fish.c",'M',78.5};

void main()
{
    struct stu *pstu;
    pstu = &boy1;

    printf("Number = %d\nName = %s\n",boy1.num,boy1.name);
    printf("Sex = %c\nScore = %f\n\n",boy1.sex,boy1.score);

    printf("Number = %d\nName = %s\n",(*pstu).num,(*pstu).name);
    printf("Sex = %c\nScore = %f\n\n",(*pstu).sex,(*pstu).score);

    printf("Number = %d\nName = %s\n",pstu->num,pstu->name);
    printf("Sex = %c\nScore = %f\n\n",pstu->sex,pstu->score);
}

 



 

                                                             #include <string.h>    //中的函数

strcpy(a,b)          //字符串的复制   

 



 

/* 结构体和函数使用 */
struct student
{
      char name[20];  //第一种
      char *name;      //第二种
      ........     
};

void print(struct student *);

void main()
{
       struct student stu;
       strpcy(struct.name,"Fishc.com!");//第一种
       name = "Fishc.com"                    //第二种
       print(&stu);  
}

void print( *p )
{
  .......  
}                  

 

 

 

                      动态储存分配

 一、常见的内存管理函数:

1.分配内存空间 : malloc 、 calloc

2、释放内存空间 : free

 

malloc           void *malloc( unsigned int size )

 

calloc           void *calloc( unsigned int size )

 

calloc           void  free(void *p )                                               释放p指向的内存区,是这部分被其他变量使用

                                                 p是最近一次调用calloc或malloc的返回值

        链表

组成:头指针:存放一个地址,地址指向第一个元素

           结点:  用的实际数据和链接结点的指针

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

                                                                                                                                  ———学习小甲鱼c语言视频

posted on 2021-03-09 13:30  哎呦魏  阅读(34)  评论(0编辑  收藏  举报