| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 495 人关注过本帖
标题:指针异常问题怎么解决求助
只看楼主 加入收藏
今生情谊
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2022-10-12
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
指针异常问题怎么解决求助
程序代码:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

//结构体嵌套二级指针练习
//结构体设计
struct TeaCher {
    //老师姓名
    char *name;

    //老师带的学生姓名数组
    char **Studets;



};

void allocateSpace(struct TeaCher ***teacherArray) {
    
    if (teacherArray == NULL)
    {
        return;
    }
    //堆区分配内存
     struct TeaCher ** ts = malloc(sizeof(struct TeaCher *) * 3);
    //数据赋值
    
    for (int i = 0; i < 3; i++)
    {
        ts[i] = malloc(sizeof(struct TeaCher));//给老师分配内存

        ts[i]->name = malloc(sizeof(char) * 64);//给老师姓名属性分配内存
        sprintf(ts[i]->name, "TeaCher_%d", i + 1);//给老师姓名赋值
      //给老师带领学生数组分配内存
        ts[i]->name = malloc(sizeof(char *)* 4);
        //给学生姓名赋值

        for (int j = 0; j < 4; j++)
        {
            ts[i]->Studets[j] = (char)malloc(sizeof(char)* 64);

            sprintf(ts[i]->Studets[j], "%s_studet_%d", ts[i]->name, j + 1);
        }
    
    }


    //建立关系
    *teacherArray = ts;

}

//打印操作
void printTeacharArray(struct TeaCher ** teacherArray) {

    for (int i = 0; i < 3; i++)
    {
        printf("%s\n", teacherArray[i]->name);//老师姓名
        for (int j = 0; j < 4; j++)
        {
            printf("   %s\n",teacherArray[i]->Studets[j]);
        }

    }

}

void test01() {
    //老师数组创建
    struct TeaCher ** teacherArray = NULL;

    //分配内存
    allocateSpace(&teacherArray);
    
    //打印所有老师和学生的信息
    printTeacharArray(teacherArray);

}


int main() {

    test01();

    system("pause");
    return EXIT_SUCCESS;
}

搜索更多相关主题的帖子: 姓名 char TeaCher 老师 struct 
2022-10-12 03:03
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:20 
把题目贴出来,因为你使用了很多内存分配,我不知道这是题目的要求,还是你从Java等语言带来的恶习

程序代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct teacher
{
    char* name;
    char** students;
};

void teachers_alloc_3x4( struct teacher** pTeachers )
{
    *pTeachers = malloc( 3 * sizeof(struct teacher) );

    for( size_t i=0; i!=3; ++i )
    {
        (*pTeachers)[i].name = malloc( 64 );
        sprintf( (*pTeachers)[i].name, "teacher_%zu", 1+i );

        (*pTeachers)[i].students = malloc( 4 * sizeof(char*) );
        for( size_t j=0; j!=4; ++j )
        {
            (*pTeachers)[i].students[j] = malloc( 64 );
            sprintf( (*pTeachers)[i].students[j], "%s_student_%zu", (*pTeachers)[i].name, 1+j );
        }
    }
}

void teachers_free_3x4( struct teacher* pTeachers )
{
    for( size_t i=0; i!=3; ++i )
    {
        for( size_t j=0; j!=4; ++j )
            free( pTeachers[i].students[j] );

        free( pTeachers[i].students );
        free( pTeachers[i].name );
    }
    free( pTeachers );
}

void teachers_output_3x4( const struct teacher* teachers )
{
    for( size_t i=0; i!=3; ++i )
    {
        puts( teachers[i].name );

        for( size_t j=0; j!=4; ++j )
            printf( "   %s\n", teachers[i].students[j] );
    }
}

void test01( void )
{
    struct teacher* teachers;
    teachers_alloc_3x4( &teachers );
    teachers_output_3x4( teachers );
    teachers_free_3x4( teachers );
}

int main( void )
{
    test01();
}


假如不是题目的要求,那
程序代码:
#include <stdio.h>
#include <string.h>

struct teacher
{
    char name[64];
    char students[4][64];
};

void teachers_init( struct teacher teachers[], size_t n )
{
    for( size_t i=0; i!=n; ++i )
    {
        sprintf( teachers[i].name, "teacher_%zu", 1+i );

        for( size_t j=0; j!=4; ++j )
            sprintf( teachers[i].students[j], "%s_student_%zu", teachers[i].name, 1+j );
    }
}

void teachers_output( const struct teacher teachers[], size_t n )
{
    for( size_t i=0; i!=n; ++i )
    {
        puts( teachers[i].name );

        for( size_t j=0; j!=4; ++j )
            printf( "   %s\n", teachers[i].students[j] );
    }
}

void test01( void )
{
    struct teacher teachers[3];
    teachers_init( teachers, 3 );
    teachers_output( teachers, 3 );
}

int main( void )
{
    test01();
}
2022-10-12 09:26
快速回复:指针异常问题怎么解决求助
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017181 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved