study c(vc中测试过)——结构体,链表,内存分配,位操作

#include "stdafx.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#define PEOPLE struct people
typedef struct people_l peoplelink;

//链表
struct people_l{
 int age;
 char *name;
 struct people_l *next;
};

void main_struct(){
 puts("start struct......");

 struct people{
  int age;
  char *name;
 }my3={30,"weisonghe3"};

 struct people my,my2;
 my.age=28;
 my.name="weisonghe";
 my2 = my;
 my2.age=29;
 printf("age=%d,name=%s/n",my.age,my.name);
 printf("age=%d,name=%s/n",my2.age,my2.name);
 printf("age=%d,name=%s/n",my3.age,my3.name);


 //结构体指针
 struct people *my4;
 my4 = &my;
 printf("age=%d,name=%s/n",(*my4).age,(*my4).name);
 printf("age=%d,name=%s/n",my4->age,my4->name);
 //指向结构数组的指针
 struct people myarray[2]={{40,"www40"},{41,"www41"}};
 struct people *structp=myarray;
 structp=myarray;
 for(int i=0;i<2;i++){
  printf("name=%s/n",(*(structp+i)).name);
 }
 //结构指针变量作为函数参数

 //动态分配函数
 PEOPLE *dynamic=(PEOPLE*)malloc(sizeof(PEOPLE));
 //(people*)即类型*为强制转换符,和函数返回类型格式一致
 //*在类型前面表示变量,*在类型后面表示强制转换

 dynamic->age=100;
 dynamic->name="dynamicname";
 printf("age=%d,name=%s/n",(*dynamic).age,(*dynamic).name);
 free(dynamic);

 //链表
 peoplelink* create(int n);
 peoplelink *pl = create(5);
 
 for(peoplelink *temp = pl;temp!=NULL;temp = (*temp).next){
  printf("age=%d,name=%s/n",(*temp).age,(*temp).name);
 }


 //枚举
 enum week{monday,tuesday,wednesday,thursday,friday,saturday,sunday};
 enum week w1,w2;
 w1=saturday;
 w2=(enum week)2;
 printf("w1=%d,w2=%d/n",w1,w2);
}

char str[4][10];//声明全局变量,如果声明在for循环中,则结束后被释放
peoplelink* create(int n){

 peoplelink *first ;
 peoplelink *previous;
 peoplelink *current;
 
 for(int i=0;i<n;i++){
  current = (peoplelink*)malloc(sizeof(peoplelink));
  (*current).age=i;

  //sprintf(str[i],"www%d",i);
  //(*current).name=str[i];
  
  char *temp = (char*)malloc(4);//使用手工动态分配内存,手工调用free才会释放
  sprintf(temp,"www%d",i);
  (*current).name=temp;

  printf("--%d--%s/n",i,(*current).name);
  if(i==0)
   previous=first = current;
  else
   (*previous).next=current;//1、这里把previous指向的结构体变量的next变量地址指向current
  (*current).next=NULL;
  //2、指针传递的是地址,所以这里只是把previous指向的变量地址变为current的地址,而没有改变previous指向的变量的值
  previous = current;
 }

 return first;
}

 

 

 

 

 

 

 

 

 

#include "stdafx.h"

void main_bit(){
 puts("start bit main......");

 //异或运算^,同为0,异为1
 int a=9;  //00010001
 int b=a^5;//00000101
     //00010100
 printf("%d/n",b);

 //求反运算~
 int revert = ~a;
 //9取反为11101110(负数的二进制在计算机中表示为补码:原码的反码+1),因此原码为补码-1取反
 //-1=11101101减一
 //  10010010符号位不变取反即-10
 printf("%d/n",revert);

 //移位
 int positive=-10;
 printf("%d/n",positive>>1);//符号为不变,其他位右移一位,原数/2
 printf("%d/n",positive<<1);//符号为不变,其他位左移一位,原数*2
 //java中还包含>>>,用0填充前面的空位,而不是符号位

 //位域
 struct bitfield{
      unsigned a:1;
      unsigned b:3;
      unsigned c:4;

 };

 struct bitfield bf1,*bf2;
 bf1.a=1;
 bf1.b=2;
 bf1.c=4;
 bf2=&bf1;
 printf("%d,%d,%d/n",bf2->a,(*bf2).b,bf2->c);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值