指向C中的字符串数组的指针

时间:2012-01-11 17:45:52

标签: c string pointers arrays

C中使用指针很有趣(不是真的)。​​

我想要以简单的方式声明几个字符串数组,最好是:

arrayOfStrings1 = {"word1", "word2", etc. };
arrayOfStrings2 = {"anotherword1", "anotherword2", etc. };
arrayOfStrings3 = etc.
etc.

类似于翻译数组的东西(但不完全),所以我希望能够在运行时间间隔这些。为此,我想要一个指针pointerToArrayOfStrings,我可以交换:

pointerToArrayOfStrings = arrayOfStrings1;
doStuff();
pointerToArrayOfStrings = arrayOfStrings2;
doSomeOtherStuff();

在我对字符串数组和指向这些字符串的指针的天真理解中,这就是我所尝试的:

// Danish transforms
const unsigned char* da_DK[] = {"b","bb","c","c","cc","d","dd","e","f","ff","g","gg","h","hh","j","j","jj","k","k","kk","l","l","l","l","ll","m","mm","n","n","nn","p","pp","r","r","r","rr","s","s","s","ss","t","t","tt","v","v","vv","æ"};

// British english transforms
const unsigned char* en_GB[] = {"a","a","a","a","a","a","a","a","a","a","a","a","a","age","ai","aj","ay","b","cial","cian","cian","dj","dsj","ea","ee","ege","ei","ei","eigh","eigh","f","f","f","g","g","gs","i","i","i","j","j","k","ks","kw","l","m","n","n","o","r","s","s","sd","sdr","sion","sion","sj","sj","tial","tion","tion","tj","u","u","u","u","w","ye","ye","z"};

    // More languages....

const unsigned char** laguageStrings;

// Assign language
if (streq(language, "da-DK")){
    laguageStrings= da_DK;
}
else if (streq(language, "en-GB")){
    laguageStrings= en_GB;
}
else
         return 0;
}

语言是char *,其中包含“en-GB”,“da-DK”等语言,streq()只是一个家庭酿造(比strcmp()稍微快一点)的字符串比较功能

长话短说,取决于编译器这种方法可能有效,报告编译器警告或编译,但会给出意想不到的结果。

解决这个问题的正确方法是什么?

2 个答案:

答案 0 :(得分:21)

C中有两种处理字符数组(字符串)的方法。它们如下:

char a[ROW][COL];
char *b[ROW];

图片表示可在代码中作为内联注释使用。

根据您希望如何表示字符数组(字符串),您可以按如下方式定义指向该字符串的指针

    char (*ptr1)[COL] = a;
    char **ptr2 = b;

它们基本上是不同的类型(以微妙的方式),所以指向它们的指针也略有不同。

以下示例演示了在C中处理字符串的不同方法,我希望它可以帮助您更好地理解C中的字符数组(字符串)。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define ROW 5
#define COL 10

int main(void) 
{
    int i, j;
    char a[ROW][COL] = {"string1", "string2", "string3", "string4", "string5"};
    char *b[ROW];

    /*

    a[][]

      0   1   2   3   4   5   6     7    8   9
    +---+---+---+---+---+---+---+------+---+---+
    | s | t | r | i | n | g | 1 | '\0' |   |   |
    +---+---+---+---+---+---+---+------+---+---+
    | s | t | r | i | n | g | 2 | '\0' |   |   |
    +---+---+---+---+---+---+---+------+---+---+
    | s | t | r | i | n | g | 3 | '\0' |   |   |
    +---+---+---+---+---+---+---+------+---+---+
    | s | t | r | i | n | g | 4 | '\0' |   |   |
    +---+---+---+---+---+---+---+------+---+---+
    | s | t | r | i | n | g | 5 | '\0' |   |   |
    +---+---+---+---+---+---+---+------+---+---+

    */  

    /* Now, lets work on b */    
    for (i=0 ; i<5; i++) {
        if ((b[i] = malloc(sizeof(char) * COL)) == NULL) {
            printf("unable to allocate memory \n");
            return -1;
        }
    }

    strcpy(b[0], "string1");
    strcpy(b[1], "string2");
    strcpy(b[2], "string3");
    strcpy(b[3], "string4");
    strcpy(b[4], "string5");

    /*

       b[]              0   1   2   3   4   5   6    7     8   9
    +--------+        +---+---+---+---+---+---+---+------+---+---+
    |      --|------->| s | t | r | i | n | g | 1 | '\0' |   |   |
    +--------+        +---+---+---+---+---+---+---+------+---+---+
    |      --|------->| s | t | r | i | n | g | 2 | '\0' |   |   |
    +--------+        +---+---+---+---+---+---+---+------+---+---+
    |      --|------->| s | t | r | i | n | g | 3 | '\0' |   |   |
    +--------+        +---+---+---+---+---+---+---+------+---+---+
    |      --|------->| s | t | r | i | n | g | 4 | '\0' |   |   |
    +--------+        +---+---+---+---+---+---+---+------+---+---+
    |      --|------->| s | t | r | i | n | g | 5 | '\0' |   |   |
    +--------+        +---+---+---+---+---+---+---+------+---+---+

    */

    char (*ptr1)[COL] = a;
    printf("Contents of first array \n");
    for (i=0; i<ROW; i++)
        printf("%s \n", *ptr1++);


    char **ptr2 = b;
    printf("Contents of second array \n");
    for (i=0; i<ROW; i++)
        printf("%s \n", ptr2[i]);

    /* b should be free'd */
    for (i=0 ; i<5; i++)
        free(b[i]);

    return 0;
}

答案 1 :(得分:1)

  

解决这个问题的正确方法是什么?

嗯,正确的方法是使用专为处理多语言界面而设计的库 - 例如gettext

另一种方法,虽然更加贴切,但是在其他语言/技术中使用hash table(也称为“字典”或“哈希映射”或“关联映射”):Looking for a good hash table implementation in C

这可能不是您所寻找的答案,但您已经向正确的问题提出了错误的问题。