鱼C论坛

 找回密码
 立即注册
查看: 1653|回复: 7

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

[复制链接]
发表于 2022-9-17 15:22:43 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

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

struct STU {
        int age;
        char name[20];
};
struct STU s1;
struct STU* p[1] = { &s1 };

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

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

        system("pause");
        return 0;
}

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

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

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

请各位大佬救救孩子
最佳答案
2022-9-18 13:18:12
本帖最后由 jackz007 于 2022-9-18 13:19 编辑
#include <stdio.h>
#include <stdlib.h>

struct STU {
        int age                                            ;
        char name[20]                                      ;
        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 age  please : ")             ;        
                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:\[00.Exerciese.2022]\C>g++ -o x x.c

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

D:\[00.Exerciese.2022]\C>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-9-17 15:37:08 | 显示全部楼层
        你这个代码中哪些是不能变的,或者说,这个代码完全是由你自己写的?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-9-17 22:23:17 | 显示全部楼层
 scanf_s("%d",&((*p[0]).age),10);  

评分

参与人数 1荣誉 +3 鱼币 +5 贡献 +3 收起 理由
kg120 + 3 + 5 + 3 感谢

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2022-9-18 12:50:09 | 显示全部楼层
jackz007 发表于 2022-9-17 15:37
你这个代码中哪些是不能变的,或者说,这个代码完全是由你自己写的?

全都是我自己写的,请务必随便改
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-9-18 12:55:35 | 显示全部楼层

成功了!谢谢大佬
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-9-18 13:18:12 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2022-9-18 13:19 编辑
#include <stdio.h>
#include <stdlib.h>

struct STU {
        int age                                            ;
        char name[20]                                      ;
        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 age  please : ")             ;        
                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:\[00.Exerciese.2022]\C>g++ -o x x.c

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

D:\[00.Exerciese.2022]\C>

评分

参与人数 1荣誉 +3 鱼币 +5 贡献 +3 收起 理由
kg120 + 3 + 5 + 3 感谢大佬

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2022-9-18 14:12:26 | 显示全部楼层
jackz007 发表于 2022-9-18 13:18
编译、运行实况:

谢谢大佬就是我还是学得太少,头次见到像递归这样的在结构体里定义结构体的指针,malloc函数也是第一次见我慢慢学慢慢看吧,感谢!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-27 22:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表