由于水平原因,博客大部分内容摘抄于网络,如有错误或者侵权请指出,本人将尽快修改

结构体

 

#include<stdio.h>
#include<string.h>
struct Student{
    int sid;
    char name[200];
    int age;
};                        //分号不能省略
void main(){
    struct Student st={1000,"zhangsan",20};
    printf("%d % s %d\n",st.sid,st.name,st.age);
    st.age=99;
    //st.name="lisi";
    strcpy(st.name,"lisi");
    st.sid=10000;
    printf("%d % s %d\n",st.sid,st.name,st.age);
    //printf("%d % s %d\n",st); 

    struct Student * pst;
    pst=&st;
    pst->sid=99;    //等价于(*pst).sid

}

image

#include<stdio.h>
#include<string.h>
struct Student{
    int sid;
    char name[200];
    int age;
};
void f(struct Student * pst){
    (*pst).sid=99;
    pst->age=10;
    strcpy(pst->name,"zhangsan");
}

void g(struct Student st){
    printf("%d %s %d\n",st.sid,st.name,st.age);
}
void g1(struct Student * pst){
    printf("%d %s %d\n",pst->sid,pst->name,pst->age);    
}
void main(){
    struct Student st;
    f(&st);
    //printf("%d %s %d\n",st.sid,st.name,st.age);
    g(st);
    g1(&st);
} 

image

 

malloc函数的使用

#include<stdio.h>
#include<malloc.h>
void main(){
    int a[]={4,10,2,8,6};

    int len;
    printf("请输入你要的长度:");
    scanf("%d",&len);
    int * parr=(int *)malloc(sizeof(int)*len);
/*    *parr=4;
    parr[1]=10;
    printf("%d %d",*parr,parr[1]);
*/
    for(int i=0;i<len;i++){
        scanf("%d",&parr[i]);
    }

    for( i=0;i<len;i++){
        printf("%d \n",*(parr+i));
    }

    free(parr);
}

跨内纯使用函数:

#include<stdio.h>
#include<malloc.h>
struct Student{
    int sid;
    int age;
};
struct Student * creatStudent(){
    struct Student * pst=(struct Student *)malloc(sizeof(struct Student));
    pst->age=10;
    pst->sid=110;
    return pst;
}
void showStudent(struct Student * pst){
    printf("%d  %d\n",pst->age,pst->sid);
}
void main(){
    struct Student st;
    struct Student * pst;
    pst=creatStudent();
    showStudent(pst);
}

typedet的使用方法:

typedef int INT;//位已用的数据类型起一个名字
typedef struct Student{
    int sid;
    char name[200];
    int age;
}ST;            //struct Student st等价于ST st

typedef struct Student{
    int sid;
    char name[200];
    int age;
} ST, * PST;//struct Student *   pst等价于 PST pst;
posted @ 2015-12-28 23:03  小纸条  阅读(176)  评论(0)    收藏  举报