C语言常见问题

C语言常见问题

指针与数组

放一个最近改的代码。
代码中的output函数还是以前错的,input函数是改后的。

知识点很多,总结的可能不到位。如有错误,欢迎留言。

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

#define N 5

typedef struct student
{
    char name[20];
    int number;
    char sex[20];
    float score;
    int age;
} student;

void input(student *stu);
void output(student *stu);

int main()
{
    student stu[N];

    printf("please input five students' information you need:\n\n");
    input(stu);

    printf("That you input the five students' information is:\n\n");
    // output(stu);

    system("pause");
    return 0;
}

void input(student *stu)
{
   for (int i = 0; i < N; i++)
    {
        //这是第一种,数组的方式,理解上容易。
        //scanf("%s %d %s %d %f", stu[i].name, &(stu[i].age), stu[i].sex, &(stu[i].number), &(stu[i].score));
        //指针的方式
        scanf("%s %d %s %d %f", (stu + i)->name, &((*(stu + i)).age), stu[i].sex, &(stu[i].number), &(stu[i].score));
        //当i = 2, stu是student结构体数组的首地址,(stu + 2)就是stu[2]的首地址;根据后面的知识点5,(stu + 2)->name就是字符数组name的首地址,类型char *的数组,这里作右值。
        //(*(stu + 2))就相当于 stu[2]
    }
}

// void output(student *stu)
// {
//     for (int i = 0; i < N; i++)
//     {
//         printf("%s%d%%s%d%f", stu[i]->name, stu[i]->age, stu[i]->sex, stu[i]->number, stu[i]->score);
//     }
// }

知识点:

1、指针 既有值、又有类型;

比如
int *num 类型是int *
char *str 类型是char *

不容易理解的在二维数组的类型(验证过)
可能有一点错,(我后来看到,现在想不起来了)

 1、数组名做右值会自动转换为 指向数组 首元素 的指针。
2、  数组名作右值时,会转换成指针,(姑且用void *表示)
  1)当数组是一维的时候,int arr[10]
     这个时候数组名作右值,数组名转换成-数组的首地址,
     指针的类型是 int *
     此时void *parr = arr;可以
     如果这个时候 &arr, 类型为int(*)[10]
     
 > 2)当数组是2维的时候,int arr[4][5]
  这个时候数组名作右值,数组名也是转换成-数组的首地址
   但是这个时候类型不是int *了,而是指向数组的指针,
   也就是int (*p)[5];
   此时void * parr = arr;警告
   initialization from incompatible pointer type。
   
   >  其实也就是这个问题  指针类型不同
     int arr[10];  一维
                   数值                 类型
        arr做右值  数组arr的首地址        int *
        &arr[0]   数组arr首元素的首地址   int (*)[10] //指向数组指针
        &arr      数组arr的首地址         int (*)[10]
                   都相同 

 >  int arr[2][3];  二维
                   数值                 类型
        arr做右值  数组arr的首地址        int (*)[3]
        &arr[0]   数组arr首元素的首地址   int (*)[3] //指向数组指针
        &arr      数组arr的首地址         int (*)[2][3]
                   都相同         

>也就是说  int a[2][3]
          int (*pa) [3] = &a[0];


         int arr[2][3] = {0};
         int (*p)[2][3] = &arr;//取二维数组的地址。
         
         int (*p)[2][3] = arr;//这个是一维的。警告!!!
         int *pp = arr;   警告   是a[0]的地址。
         int (*ppp)[3] = arr;

2、结构体的 " . "和“ -> ”
这两个一个是结构体变量的指针用的,一个是结构体变量用的。
3、指针与数组。

int  a[10];
int *p = a;

1、a[2]之所以能取到数组的第三个元素,就是因为它等价于 *(a + 2)

4、scanf函数, 后面需要指针,name是结构体里面的数组,做右值自动转化为首地址指针;age是int类型,作右值不会转换为指针。
5、运算符的优先级。

()> 结构体取成员" . " / 指向结构体的指针取成员 “ -> ” / 数组下标[] > 
取地址、指针间接寻址。

问题?

1、为什么数组从0开始?
2、
int a[10];
int *p = a;
p++是什么意思?
3、数组下标负数什么意思?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include"stdio.h" #include"stdlib.h" #define NULL 0 struct student { long num; char name[20]; int score[6]; struct student *next; }; void show() { printf("\nthere is a cataloge as follow.\n"); printf("***************************************\n"); printf("* *\n"); printf("* 1. create *\n"); printf("* 2. Insert *\n"); printf("* 3. print *\n"); printf("* 4. delete *\n"); printf("* 5. modify *\n"); printf("* 6. save_to_file *\n"); printf("* 7. lode from file *\n"); printf("* 8. exit *\n"); printf("***************************************\n"); printf("please input 1--8 to choice what you want:\n"); } struct student *create() { struct student *head,*p,*last; int i; int temp2; char name[20]; p=head=(struct student *)malloc(sizeof(struct student)); head->next=NULL; last=p; while(1) { last->next=p; last=p; p=(struct student *)malloc(sizeof(struct student)); /*last->next=p; last=p;*/ p->next=NULL; printf("number:"); scanf("%ld",&p->num); if(p->num==0)break; getchar(); printf("name:"); scanf("%s",p->name); printf("score:"); for(i=0;i<6;i++) { scanf("%d",&temp2); p->score[i]=temp2; } printf("next student's information.\n"); } free(p); return head; } void Insert(struct student *head) { struct student *p,*q; int score; long num; int i; printf("\nEnter the student's information you want to insert.\n"); printf("number:"); scanf("%ld",&num); q->num=num; getchar(); printf("name:"); scanf("%s",q->name); printf("score:(chinese,math,english,biology,physics,chemistry)\n"); for(i=0;i<6;i++) { scanf("%d",&score); q->score[i]=score; } q->next=NULL; p=head; while(p->next->num<q->num&&p->next!=NULL) p=p->next; q->next=p->next; p->next=q; if(p->next==NULL) p->next=q; } void delete(struct student *head) { struct student *p,*q; long num; printf("enter the student's information you want to delete.\n"); printf("number:"); scanf("%ld",&num); getchar(); p=head; while(p->next!=NULL&&p->next->num!=num) p=p->next; q=p->next; p->next=p->next->next; free(q); } void print(struct student *head) { struct student *p; int i; p=head->next; if(p==NULL) { printf("\nthere is no information.\n"); exit(0); } printf("\nnumber\tnamme\tchinese\tmath\tenglish\tbiology\tphysics\tchemistry\n"); while(p!=NULL) { printf("\n%ld\t%s",p->num,p->name); for(i=0;i<6;i++) printf("\t%d",p->score[i]); p=p->next; } } void modify(struct student *head) { struct student *p; int choice,i; long num; char name[20]; int score[6]; printf("\nEnter what student's information you want to modify.\n"); printf("number:"); scanf("%ld",&num); getchar(); printf("\nname:"); scanf("%s",name); printf("\n"); p=head->next; while(p->num!=num&&p->name[20]!=name[20]&&p!=NULL) p=p->next; printf("\nplease choice what you want to modify:1-number 2-name 3-score.\n"); scanf("%d",&choice); switch(choice) { case 1:printf("\nEnter the true number:"); scanf("%ld",&num); p->num=num; break; case 2:printf("\nEnter the true name:"); scanf("%s",p->name); break; case 3:printf("\nEnter the right score:"); for(i=0;i<6;i++) { scanf("%d",&score[i]); p->score[i]=score[i]; } break; } } void save_in(struct student *head) { struct student *p; FILE *fp; char file_name[30]; printf("please enter the file name you want to save.\n"); scanf("%s",file_name); printf("save to file:%s",file_name); if((fp=fopen(file_name,"w"))==NULL) { printf("can't open the file.\n"); exit(0); } p=head; while(p->next!=NULL) { fwrite((void*)p->next,sizeof(struct student),1,fp); p=p->next; } fclose(fp); } struct student *load_from_file() { struct student *head,*p,*last; FILE *fp; char file_name[30]; head=(struct student *)malloc(sizeof(struct student)); last=head; head->next=NULL; printf("please enter the file name you want to save.\n"); scanf("%s",file_name); printf("save to file:%s",file_name); if((fp=fopen(file_name,"r"))==NULL) { printf("can't open the file.\n"); exit(0); } p=(struct student *)malloc(sizeof(struct student)); p->next=NULL; while(fp=fread((void *)p,sizeof(struct student),1,fp)==1) { last->next=p; last=p; p=(struct student *)malloc(sizeof(struct student)); p->next=NULL; } free(p); fclose(fp); return head; } void main() { struct student *la; int choice; /*char Yes_No;*/ la=(struct student *)malloc(sizeof(struct student)); la->next=NULL; while(1) { show(); scanf("%d",&choice); switch(choice) { case 1:la=create(); break; case 2:Insert(la); break; case 3:print(la); break; case 4:delete(la); break; case 5:modify(la); break; case 6:save_in(la); break; case 7:la=load_from_file(); break; case 8:exit(0); } } }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值