kg120 发表于 2022-9-17 15:22:43

请问一下各位,怎样通过改变“指向结构体的指针”从而改变结构体里面的变量?

使用的软件是vs,Visual Studio 2019,语言是c。
很典型的作业,建立一个结构体student,包含年龄age和姓名name。建立学生s1和指向s1的*p。

源代码如下:
#include <stdio.h>
#include <iostream>

struct STU {
        int age;
        char name;
};
struct STU s1;
struct STU* p = { &s1 };

void input() {
        int j = 0;
        char c;
        scanf_s("%d",(*p).age,10);        //输入年龄
        getchar();
        for (j = 0, c = 0; (c = getchar()) != '\n'; j++) {        //输入姓名
                (*p).name = c;
        }
        (*p).name = '\n';
}

int main() {
        int i = 0;
        char h;
        input();
        printf("%d\n", (*p).age);
        for (i = 0, h = 0; ( h = (*p).name ); i++){
                putchar(h);
        }

        system("pause");
        return 0;
}

想法就是在自定义的input函数里改变指针从而改变结构体里的数据。“重新生成解决方案”的时候没有报错,不过有几个警告,看不太懂,请大佬教教:


然后我抱着侥幸的心理想着警告也不影响运行,结果就发现运行不了:


CSDN逛了一下听说是“由于数据超出内存所致”或者“文件调试的时候关闭导致损坏”,但我还是没找到究竟是哪里出问题了。。。

请各位大佬救救孩子{:9_222:}

jackz007 发表于 2022-9-17 15:37:08

      你这个代码中哪些是不能变的,或者说,这个代码完全是由你自己写的?

jhq999 发表于 2022-9-17 22:23:17

scanf_s("%d",&((*p).age),10);

kg120 发表于 2022-9-18 12:50:09

jackz007 发表于 2022-9-17 15:37
你这个代码中哪些是不能变的,或者说,这个代码完全是由你自己写的?

全都是我自己写的,请务必随便改{:10_333:}

kg120 发表于 2022-9-18 12:55:35

jhq999 发表于 2022-9-17 22:23


成功了!谢谢大佬{:10_298:}

jackz007 发表于 2022-9-18 13:18:12

本帖最后由 jackz007 于 2022-9-18 13:19 编辑

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

struct STU {
      int age                                          ;
      char name                                    ;
      struct STU * next                                  ;
}                                                          ;

struct STU * input(struct STU * x)
{
      struct STU * p                                     ;
      if((p = (struct STU *) malloc(sizeof(struct STU)))) {
                printf("input name please : ")             ;
                scanf_s("%s", & p -> name , 20)            ;
                printf("input ageplease : ")             ;      
                scanf_s("%d", & p -> age)                  ;
                p -> next = x                              ;
      } else {
                fprintf(stderr , "failure of malloc():\n") ;
      }
      return p                                           ;
}

void disp(struct STU * x)
{
      for(; x ; x = x -> next) printf("name : %20s , age : %2d\n" , x -> name , x -> age) ;
}

void destroy(struct STU * x)
{
      struct STU * p      ;
      for(; x ;) {
                p = x         ;
                x = p -> next ;
                free(p)       ;
      }
}

int main(void)
{
      struct STU * L = NULL                              ;
      int i , n                                          ;
      printf("input the number of people please : ")   ;
      scanf_s("%d" , & n)                              ;
      for(i = 0 ; i < n ; i ++) L = input(L)             ;
      disp(L)                                          ;
      destroy(L)                                       ;
      system("pause")                                    ;
}
      编译、运行实况:
D:\\C>g++ -o x x.c

D:\\C>x
input the number of people please : 5
input name please : abc
input ageplease : 18
input name please : def
input ageplease : 19
input name please : efg
input ageplease : 20
input name please : fgh
input ageplease : 21
input name please : ghi
input ageplease : 22
name :                  ghi , age : 22
name :                  fgh , age : 21
name :                  efg , age : 20
name :                  def , age : 19
name :                  abc , age : 18
请按任意键继续. . .

D:\\C>

kg120 发表于 2022-9-18 14:12:26

jackz007 发表于 2022-9-18 13:18
编译、运行实况:

谢谢大佬{:10_298:}就是我还是学得太少,头次见到像递归这样的在结构体里定义结构体的指针,malloc函数也是第一次见{:10_266:}我慢慢学慢慢看吧,感谢!

kg120 发表于 2022-9-23 11:23:49

kg120 发表于 2022-9-18 14:12
谢谢大佬就是我还是学得太少,头次见到像递归这样的在结构体里定义结构体的指针,malloc函数也 ...

我靠,主函数在调用input函数的时候这个“p->next=x”看了好久才懂:把L2的next指向L1,3指向2...,后面调用output函数的时候,循环会从L5开始,L5的next是L4依次往回推。
6楼大佬牛B{:10_250:}
页: [1]
查看完整版本: 请问一下各位,怎样通过改变“指向结构体的指针”从而改变结构体里面的变量?