【问题标题】:Problems with dynamic array and pointer in CC中动态数组和指针的问题
【发布时间】:2013-04-23 15:45:44
【问题描述】:

我想从 STDIN 中读取以下行并将值保存在 c 中:

A:2
B:3
C:AAAA1
C:AASC2
C:aade3
D:1
D:199

这是我的 c 程序:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <sys/time.h>

int main(int argc, char **argv)
{

    char buf[BUFSIZ];
    short a = 0;
    short b = 0;
    short anzb=0;
    short anza=0;
    char *c
    short *d;

    short i;


    while (fgets(buf, BUFSIZ, stdin) != NULL)
    {
        if (buf[strlen(buf)-1] == '\n') {
            char *isa = strstr(buf, "A:");
            char *isb = strstr(buf, "B:");
            char *isc = strstr(buf, "C:");
            char *isd = strstr(buf, "D:");
            if(isa){
                char *sep = substring(isa,3,strlen(isa));
                a = atoi(sep);
                d = malloc(a * sizeof(short));
            }else if(isb){
                char *sep = substring(isb,3,strlen(isb));
                b = atoi(sep);
                c = malloc(b * sizeof(char));
            }else if(isc){
                char *sep = substring(isc,3,strlen(isc));
                c[anzc] = sep;
                anzc++;
            }else if(isd){
                char *sep = substring(isd,3,strlen(isd));
                d[anzd] = sep;
                anzd++;
            }
        }
    }

    printf("%i\n", a);
    printf("%i\n", b);

    for(i=0; i<=anzc-1;i++){
        printf("%c", c[i]);
    }

    return 0;
}

我是c新手,所以我对指针和数组了解不多,所以我希望你能帮助我。

在值 A: 和 B: 被读取并存储在 a 和 b 中之后,我可以为 c 和 d 的行创建我的数组。我认为这是我的问题。我不知道此时如何在我的程序中创建一个数组。我尝试了 malloc 和其他东西,但我的知识很少。

如果我已经读取了 c 和 d(A 和 B)的值(大小),我只想创建一个数组。

然后我想将值保存在数组中。

我希望你能帮助我修复我的代码。这一天我尝试了很多,但没有任何效果,我现在很无助。

编辑:

我遇到分段错误的新尝试 11:

     else if(isb){
        char *sep = substring(isb,8,strlen(isb));
        b = atoi(sep);
        c = malloc(b * sizeof(char*));
        int i;
        for (i = 0; i < subst; i++)
        {
          c[i] = malloc(13);
        }
    }else if(isc){
        char *sep = substring(isc,8,strlen(isc));
        strcpy(c[anzc], &buf[3]);
        anzc++;
    }

【问题讨论】:

    标签: c arrays pointers malloc


    【解决方案1】:

    您的分配或多或少是正确的,但是您忽略了一些细节。 B 为您提供值 3,即 C 的条目数,而不是每个条目的长度。然后,当您实际上需要一个类型为char* 的二维数组时,您分配了一个数组,该数组将指向其他 3 个数组,这些数组将包含 C 行上的每个值。所以;

    这行c = malloc(b * sizeof(char)); 需要是c = malloc(b * sizeof(char*)); 那你需要做;

     int i;
     for (i = 0; i < b; i++)
     {
         c[i] = malloc(length); // where length is some arbitrary buffer length
         // because you have no way of knowing the length of the individual strings.
     }
    

    在此之后,您可以使用strcpy 将这些行复制到您在 for 循环中分配的每个 char 数组中。

    所以要完成复制,你需要做这样的事情;

             int iC = 0;
             // outside of the while loop we need a control var track the index
             // of the c array. it needs to work independent of the normal iteration.
             //inside the while loop
             else if(isc){
                strcpy(c[iC], &buf[3]) 
                iC++;
            }
    

    【讨论】:

    • 也许你可以告诉我strlen()。他使用strlen()-1
    • 需要我复制数据吗?我只调整一次数组的大小。当时如果我知道有多少条目。
    • @bladepit 将数组想象成一个表格,就像在 excel 中一样。该代码仅创建表。数据,即 x 其中 x 是 C:x 没有被复制到其中。您需要使用 strcpy 来完成此操作。应该慷慨地设置缓冲区,以免溢出。或者您可以使用指定数据长度的安全字符串复制方法之一。
    • 我很困惑。用 char* c 创建一个二维数组?我想保存在我的数组 [0] = AAAA1; [1] = AASC2 等等。条目的长度始终相同。只有在 d 中它是动态的。一开始c是空的,然后我从stdin读取b,然后我创建c。之后,如果我从标准输入读取 c,我将字符串放入数组中。那是我的需求,我很困惑,我不知道我现在应该做什么......:-(
    • @bladepit 是的,您对二维数组是正确的。分配后,您需要将每个 C 值复制到索引 0-2 中。我会更新一些逻辑来指导你。
    猜你喜欢
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-29
    相关资源
    最近更新 更多