如何将char **中的个别字符复制到另一个char **中?

时间:2017-06-01 11:41:31

标签: c arrays

我正在尝试编写一个程序,如果以特定格式输入,则将单词分解成段/音节。我想以格式输入一个单词:

[d-i-s][c-o-#][v-er-#]

并将其存储为以下格式:

syllable[0]=dis
syllable[1]=co
syllable[2]=ver

到目前为止,我已设法使用分隔符']'将其分解为音节,并将其保存为以下格式的char **:

syllable[0]=[d-i-s
syllable[1]=[c-o-#
syllable[2]=[v-er-#

所以现在我只想清理它并删除不必要的角色!我以为我会创建一个新数组并复制旧数组中的字母,只要它们不是[ - #。但是对于我的生活,我无法弄清楚如何将正确的字母复制到另一个阵列中!!

我知道我不能这样做:

cleanArray[i][j] = dirtyArray[i][k] 

因为cleanArray [i]是char *而我无法编辑它?但可以我做什么?

我已经阅读了很多类似的问题,这些问题建议使用strncpy和snprintf(how to copy char array to another char array in C?strcpy and printf a multidimensional char array C),但我已尝试过这些问题但我无法让它们发挥作用。我甚至尝试将cleanArray放入3个维度,希望我能够将单个字母保存为cleanArray [i] [j]作为char * s,这可能是完全错误的。

正确的方法是什么?对不起,如果它很明显,但我花了几个小时,现在非常,非常,困惑......我真的很感激你能给出的任何建议!

这是我的代码:

char** cleanStrings (char**dirtyList, int arrayLength)
{
    int i, j, k;
    char** cleanList = (char**)calloc(arrayLength, CHARLEN);

    for (i=0; i<arrayLength; i++)
    {
        k= 0;
        cleanList[i] = (char*)calloc(10,CHARLEN);

        for (j=0; j<strlen(dirtyList[i]+1);j++)
        {

            if (dirtyList[i][j] == '[') continue;
            else if (dirtyList[i][j] == '#') continue;
            else if (dirtyList[i][j] == '-') continue;
            else 
                //i know this is wrong, but what is the right way of doing it?
                cleanList[i][k] = dirtyList[i][j];

                k++;    
        }
    }   
    return cleanList;
}

修改

感谢您的所有评论,我现在已经开始工作了!与我的想法相反,正如Barmar指出的那样,没有任何问题:

cleanArray [i] [j] = dirtyArray [i] [k]

我的代码无效,因为我犯了很多其他错误,例如: -casting calloc的返回值 - 不为calloc正确分配内存 - 不正确的括号

我还在头文件中有代码,我认为它包含了自己的问题。

2 个答案:

答案 0 :(得分:3)

假设您使用的是calloc size参数错误。其中一个char** cleanList = (char**)calloc(arrayLength, CHARLEN);cleanList[i] = (char*)calloc(10,CHARLEN);是错误的。您也不应该转换malloc() / calloc()的返回值。出于易读性和代码流的目的,我还替换了if语句。您还写了for (j=0; j<strlen(dirtyList[i]+1);j++)而不是for (j=0; j<strlen(dirtyList[i])+1;j++),因为strlen()计算的字符串长度没有\0。这是代码几乎没有变化。

char** cleanStrings (char**dirtyList, int arrayLength)
{
    int i, j, k;
    char **cleanList = calloc(arrayLength,sizeof * cleanList);

    for (i=0; i<arrayLength; i++)
    {
        k= 0;
        cleanList[i] = calloc(10,sizeof * cleanList[i]);

        for (j=0; j<strlen(dirtyList[i])+1;j++)
        {

            if ((dirtyList[i][j] != '[') && (dirtyList[i][j] != '#') && (dirtyList[i][j] != '-') ){
                cleanList[i][k] = dirtyList[i][j];
                k++;
            }

        }
    }   
    return cleanList;
}

答案 1 :(得分:2)

您没有为CHARLEN分配足够的内存。我假设sizeof(char)cleanList,这是1个字节。但是char*的元素是char **cleanList = calloc(arrayLength, sizeof(char *)); ,它是4或8个字节,分配太小了。它应该是:

malloc

使用callocsizeof (T)时的一般规则是,乘数始终为T,其中*是目标类型,其中少于char **。因此,如果您要分配给sizeof(char *),那就是{ "name": "ionic-hello-world", "author": "Ionic Framework", "homepage": "http://ionicframework.com/", "private": true, "scripts": { "clean": "ionic-app-scripts clean", "build": "ionic-app-scripts build", "ionic:build": "ionic-app-scripts build", "ionic:serve": "ionic-app-scripts serve" }, "dependencies": { "@angular/animations": "4.0.0", "@angular/common": "4.0.0", "@angular/compiler": "4.0.0", "@angular/compiler-cli": "4.0.0", "@angular/core": "4.0.0", "@angular/forms": "4.0.0", "@angular/http": "4.0.0", "@angular/platform-browser": "4.0.0", "@angular/platform-browser-dynamic": "4.0.0", "@angular/platform-server": "4.0.0", "@ionic-native/call-number": "3.8.0", "@ionic-native/core": "^3.10.3", "@ionic-native/device": "3.8.0", "@ionic-native/facebook": "^3.10.3", "@ionic-native/geolocation": "3.8.0", "@ionic-native/google-plus": "^3.10.3", "@ionic-native/in-app-browser": "^3.8.0", "@ionic-native/social-sharing": "3.8.0", "@ionic-native/splash-screen": "^3.9.2", "@ionic-native/status-bar": "^3.10.3", "@ionic/cloud-angular": "^0.12.0", "@ionic/storage": "^2.0.1", "font-awesome": "4.7.0", "ionic-angular": "3.0.1", "ionicons": "3.0.0", "jssha": "2.2.0", "rxjs": "5.1.1", "sw-toolbox": "3.4.0", "zone.js": "^0.8.4" }, "devDependencies": { "@ionic/app-scripts": "1.3.0", "@types/jssha": "0.0.29", "@types/node": "7.0.13", "typescript": "~2.2.1" }, "cordovaPlugins": [ "cordova-plugin-whitelist", "cordova-plugin-console", "cordova-plugin-statusbar", "cordova-plugin-device", "cordova-plugin-splashscreen", "ionic-plugin-keyboard" ], "description": "App desc" }