字符串数组在C没有的malloc [英] Array of strings with no malloc in c

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

问题描述

谁能告诉我什么是错与code?,我不能使用malloc,因为我还没有了解它在class.I意味着我可以做字符串的二维数组没有malloc的,如果是我如何我应该当我想改变它写一个元素/打印/提前扫描it.Thanks

  INT的main(){    为size_t X,Y;
    字符*一个[50] [7];    为(X = 0; X&下; = SIZEX; X ++)
    {
        的printf(\\ n请输入新用户\\ n的名称);
        scanf的(%S,一[X] [0]);        的printf(请输入新的用户\\ n的姓);
        scanf的(%S,一[X] [1]);        的printf(请输入新的用户\\ n的识别码);
        scanf的(%S,一[X] [2]);        的printf(请输入新用户的\\ n出生年份);
        scanf的(%S,一[X] [3]);        的printf(请输入新的用户\\ n的用户名);
        scanf的(%S,一[X] [4]);    }    返回0;
}


解决方案

所以,你需要字符串(字符数组)的二维数组。实现这一点的一种方法是将分配炭的3d阵列

 字符X [50] [7] [MAX_LENGTH];

您可以把具有阵的矩阵开始(的指针),然后,另一个层面给予深度的基质(为您的字符串即存储空间)。

您的做法也未尝不可,只要你愿意使用的malloc 或类似的存储空间,为您的字符串手动分配。

Can someone tell me whats wrong with that code?And i cant use malloc because i havent learn it in class.I mean can i make a 2d array of strings without malloc and if yes how i am i supposed to write an element when i want to change it/print it/scan it.Thanks in advance

int main() {

    size_t x,y;
    char *a[50][7];

    for(x=0;x<=SIZEX;x++)
    {
        printf("\nPlease enter the name of the new user\n");
        scanf(" %s",a[x][0]);

        printf("Please enter the surname of the new user\n");
        scanf(" %s",a[x][1]);

        printf("Please enter the Identity Number of the new user\n");
        scanf(" %s",a[x][2]);

        printf("Please enter the year of birth of the new user\n");
        scanf(" %s",a[x][3]);

        printf("Please enter the username of the new user\n");
        scanf(" %s",a[x][4]);

    }

    return 0;
}

解决方案

So, you need a 2d array of strings (char arrays). One way to achieve this would be to allocate a 3d array of char as:

char x[50][7][MAX_LENGTH];

You can think as having a matrix of array start (of pointers) and then, another dimension to give depth to your matrix (i.e. storage space for your string).

Your approach is also fine, as long as you are willing to allocate manually using malloc or similar storage space for your strings.

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

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