重新分配存储器并在C中的重新分配存储器空间增加一个串 [英] Reallocating memory and adding a string at the reallocated memory space in C

查看:259
本文介绍了重新分配存储器并在C中的重新分配存储器空间增加一个串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法加入记录在动态分配的字符串数组的结尾。重新分配更多的内存要添加的记录之前,一切工作正常,然后我基本上复制了我最初没有,但现在的realloc。而我做的输入添加的记录后,我得到一个错误,我不知道如何去添加记录。注:*本带来code是真的从原来的剥离下来。我尝试过很多事情,但不得要领,感谢所有帮助提前。

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#定义stringsize的21无效addRecords(字符**名称,INT classSize);诠释主(){
    焦炭**名称;
    INT classSize,我;    //用户将能够选择他多少条记录woudld喜欢的输入。
    的printf(请说明你要输入的记录数:\\ n);
    scanf函数(%d个,&安培; classSize);    名称=的malloc(classSize * sizeof的(字符*));    对于(i = 0; I< classSize;我++){
        名称[I] =的malloc(stringsize的*的sizeof(字符));
    }
    的printf(请学生输入记录(输入每条记录后,新行),具有以下格式:第一个名字.... \\ n);    对于(i = 0; I< classSize;我++){
        scanf函数(%S,*(姓名+ I));
    }    对于(i = 0; I< classSize;我++){
        的printf(%S,*(姓名+ I));
        的printf(\\ n \\ n);
    }addRecords(名称,classSize);
}无效addRecords(字符**名称,诠释classSize){
    INT另外,我;    的printf(你会如何对多条记录想补充\\ n吗?);
    scanf函数(%d个,&安培;另外);    名称=的realloc(名称,(classSize +加)* sizeof的(字符*));    对于(i = classSize;我≤(classSize +加);我++){
        名称[I] =的malloc(stringsize的*的sizeof(字符));
    }    的printf(请学生输入记录(输入每条记录后,新行),与followingformat:名字.... \\ n);    对于(i = classSize; I< classSize +另外,我++){
        scanf函数(%S,*(姓名+(classSize + I)));
    }
    的printf(\\ n \\ n);
    对于(i = 0; I< classSize +另外,我++){
        的printf(%S,*(姓名+ I));
    }    的printf(\\ n \\ n);
}


解决方案

您正在编写出数组的边界的:

 为(I = classSize; I< classSize +另外,我++){
    scanf函数(%S,*(姓名+(classSize + I)));

更改

 为(I = classSize; I< classSize +另外,我++){
    scanf函数(%S,*(姓名+ I));

注意名称[I] *(姓名+ I)

更具可读性

I am having trouble adding "records" at the end of a dynamically allocated string array. Before reallocating more memory for the records to be added, everything works fine, and then I basically replicate what I initially did but now with realloc. And after I am done inputting the added Records I get an error and I don't know how to go about adding records. NOTE* The posed code is really stripped down from the original. I've tried many things but to no avail, thanks for all the help in advance.

#include <stdio.h>
#include <stdlib.h>
#define STRINGSIZE 21

void addRecords( char **Names, int classSize);

int main(){
    char **Names;
    int classSize, i;

    //User will be able to choose how many records he woudld like to input.
    printf("Please indicate number of records you want to enter:\n");
    scanf("%d", &classSize);

    Names=malloc(classSize*sizeof(char*));

    for (i=0; i<classSize; i++) {
        Names[i]=malloc(STRINGSIZE*sizeof(char));
    }
    printf("Please input records of students (enter a new line after each record), with following format: first name....\n");

    for (i=0; i<classSize; i++) {
        scanf("%s", *(Names + i));
    }

    for (i=0; i<classSize; i++) {
        printf("%s ", *(Names+i));                
        printf("\n\n");
    }

addRecords(Names, classSize);
}

void addRecords(char **Names, int classSize){
    int addition, i;

    printf("How many records would you like to add?\n");
    scanf("%d", &addition);

    Names=realloc(Names, (classSize+addition)*sizeof(char*));

    for (i=classSize; i<(classSize+addition); i++) {
        Names[i]=malloc(STRINGSIZE*sizeof(char));
    }

    printf("Please input records of students (enter a new line after each record), with followingformat: first name....\n");

    for (i=classSize; i<classSize+addition; i++) {
        scanf("%s", *(Names + (classSize + i)));
    }
    printf("\n\n");
    for (i=0; i<classSize+addition; i++) {
        printf("%s ", *(Names+i));
    }

    printf("\n\n");
}

解决方案

You are writing out of the bounds of the array:

for (i=classSize; i<classSize+addition; i++) {
    scanf("%s", *(Names + (classSize + i)));

change to

for (i=classSize; i<classSize+addition; i++) {
    scanf("%s", *(Names + i));

Note that Names[i] is more readable than *(Names + i)

这篇关于重新分配存储器并在C中的重新分配存储器空间增加一个串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆