分配内存的字符串数组 [英] allocating memory to an array of string

查看:196
本文介绍了分配内存的字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用填充两个函数的字符串数组:第一,如果我有N个字符串来分配,将分配N个存储空间;第二个将分配内存读取每串

I would like to fill an array of strings using two functions: the first, if I have n strings to allocate, will allocate n memory spaces; the second will allocate memory for each string read

这是第一个功能:

char** allocate(int n)
{
    char** t;
    t=(char**)malloc(n*sizeof(char*));
    if(!t) exit(-1);
    return t;
}

下面是第二个

void fill(char*** t,int n)
    {
        int i;
        char* help=" ";
        for(i=0;i<n;i++)
        {
            printf("\n saisir la chaine n %d :",i+1);
                scanf("%s",help);
                *t[i]=(char*)malloc((strlen(help)+1)*sizeof(char));
                strcpy(*t[i],help);
        }
    }

我没有忘记调用这样的第二个:填写(amp; T公司,N);

问题是,我读的第一个字符串,程序结束后得到一个错误。

The problem is that I get an error after reading the first string and program ends.

推荐答案

的问题是,你不帮忙变量分配内存。

The problem is that you do not allocate the memory for help variable.

修改的char *帮助=; 字符帮助[512] =;

这种方式帮助指向一个字符串(常数存储在存储器块,这是不允许被改变。

this way help points to a string literal (constant stored in memory block, which is not allowed to be changed.

这篇关于分配内存的字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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