指针与结构体的初步了解

常用知识点总结

指针的定义

1.指针就是地址,而地址就是内存单元的编号,是从0开始的非负整数
2.指针变量就存放地址的变量,但是指针和指针变量是两个概念

为什么需要指针

1.表示一些复杂的数据结构
2.快速的传递数据,减少内存的耗用
3.能够使函数返回多个值
4.可以直接访问硬件
5.能够方便的处理字符串
6.了解了指针能够更好的理解面向对象语言中的引用

指针的注意事项
int *p;
scanf("%d",p);

这是错误的所有的指针在使用前都要赋初值

int a=0;
int *p=&a;
scanf("%d",&p);

这是错误的所有的指针在使用scanf函数时不用加&

与指针有关的应用——动态分配内存(malloc函数和realloc函数的用法)

1.malloc函数
语法:指针名=(数据类型)malloc(申请分配的字节数);
malloc函数的返回值是void
类型,在C语言中不用强制转换,但在·C++中必须转化

#include<stdio.h>
#include<malloc.h>
int main(void)
{
    int i=2;
    int * p = (int *)malloc(4);
    /*
       1.要使用malloc函数就必须添加 malloc.h头文件 
       2.malloc只有一个形参。并且形参是整形 
       3.4表示请求系统为本程序分配4个字节的内存
       4.malloc函数只能返回第一个字节的地址,(int *)是为了让程序明白该地址是整型变量的地址 
    
    */
    
    return 0;
}

2.realloc函数
语法:指针名=(数据类型*)realloc(要改变内存大小的指针名,新的大小);
新的大小一定要大于原来的大小,不然的话会导致数据丢失!

#include<stdio.h>

#include<stdlib.h>

int main()

{

int i;

int *pn=(int *)malloc((5*sizeof(int));

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

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

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

pn=(int *)realloc(pn,10*sizeof(int));

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

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

printf("%3d",pn[i]);

printf("\n");

free(pn);

return 0;

}

什么叫结构体

把一些基本类型数据结合在一起形成的一种新的复合数据类型

为什么需要结构体

为了表示一些现实中的复杂事物。而普通的数据类型无法满足基本要求

结构体的注意事项

在定义的同时可以整体赋值,定义后只能单个赋值
赋值的特殊情况

Include<stdio.h>
Include<string.h>
Struct Student
{
Char a[10];
};

Struct Student A;//
//如果是这样赋值
A.a[10]=”ghhh”;//会报错
//此时可以用一个字符串复制函数来赋值
Strcpy(A.a,”ghhh”);//把ghhh字符串复制到A.a所指向的内存空间里
结构体所占内存的计算

叙述不便详情请点击下方链接
https://blog.csdn.net/onlyanyz/article/details/8159240

结构体应用——简略的学生信息系统
#include<stdio.h>
#include<malloc.h>

struct student
{
    char sex[10];
    char name[10];
    int chengji;
    
};

void fuzhi(struct student **,int);
void paixu(struct student *,int);
//void shuchu(struct student *,int,struct student *);

int main(void) 
{
    int len;
    printf("请输入学生的个数:\n");
    scanf("%d",&len);
    struct student  *pArr;
    pArr =(struct student *)malloc(len*sizeof(struct student));//动态构造了一个一维数组 
    

    fuzhi(&pArr,len);
    paixu(pArr,len);
//  shuchu(pArr,len,pArr);
    
    return 0;
}

void fuzhi(struct student ** pArr,int len)
{
    
            int i; 
    for(i= 0;i<len;++i) 
    {
        printf ("请输入第%d个学生的信息:\n",i+1);
        
        //char sex1;
        printf("sex=");
        scanf("%s",&(*pArr+i)->sex);
    //  pArr->sex = 'h';
    //  printf("%c",pArr->sex);
    //  printf("\n");
        
        printf("name=");
        scanf("%s",(*pArr+i)->name); //p[i].name的前面不需要加 & 因为结构体中 name这个对象是数组名即是数组首地址 
    //  printf ("\n"); 
        
        printf("chenji=");
        scanf ("%d",&(*pArr+i)->chengji);
        //printf("\n");
    }
        
    
    
 } 
 
 void paixu(struct student * pArr,int len)
 {
    int i,j;
    
    
    for (i=1;i<len-1;++i)
    {
        for(j=0;j<len-i;++j)
        {
            struct student t;
            if((pArr+j)->chengji>(pArr+j+1)->chengji)
            {
                t=pArr[j];
                pArr[j]=pArr[j+1];
                pArr[j+1]=t;
             }
         }
     }
     for(i=0;i<len;++i) 
    {
        printf ("第%d个学生的信息:\n",1);
        printf("sex=");
        printf("%s",(pArr+i)->sex);
        printf("\n");
        
        printf("name=");
        printf("%s",(pArr+i)->name); 
        printf ("\n"); 
        
        printf("chenji=");
        printf("%d",(pArr+i)->chengji);
        printf("\n");
    }
     
 
     }
    
    
    
 
 
// void shuchu(struct student *p,int len,struct student *q)
// {
//  for(int i=0;i<len;++i) 
//  {
//      printf("%s\n",(p+i)->sex);
//      printf("%s\n",(p+i)->name);
//      printf("%d\n",(p+i)->chengji);
//   }
// }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容