2017-02-20 168 views
-2

我想有一个字符串数组,其中在该阵列的每个索引存在字符串的另一个阵列。字符指针指针数组用C

char Example[0] = { "array", "of", "strings"} 
Example[1] = { "array", "of", "strings2"} 
Example[...] = { ... } 

我也曾想过尝试使用字符**阵列[some_number]的方式来做到这一点,但我发现的语法不舒服。

#include <stdio.h> 

int main(){ 
    char *test[10] = {0}; 
    char *ex[3] = {0}; 

    ex[0] = "array"; 
    ex[1] = "of"; 
    ex[2] = "strings"; 

    test[3] = *ex; 

    printf("Content of test[3] = %s %s\n", test[3], ???); 

    return 0; 
} 

我有困难试图打印出来......有什么其他方式可以去实现我所提到的吗?任何资源/帮助都会很棒。

+1

它几乎* *听起来像是你试图制造指针数组的数组常量字符串。我几乎在那里使用“因为”,因为我读了你的帖子四次,我仍然无法辨别你所寻找的东西。 – WhozCraig

+1

'char * Example [] [3] = {{“array”,“of”,“strings”},{“array”,“of”,“strings2”},/*...*/};' ? – BLUEPIXY

+0

那么,我真正想做的是创建一个虚拟内存地址的哈希表。我最初的想法是,使用某种类型的散列,我将特定地址分组为“桶”。我认为如果我有一个指向数组的指针数组,我可以实现这一点。尽管我迷失了语法。 –

回答

2

尝试这种情况:

const char* example[] = { "array", "of", "strings" }; 

这就是3(隐式地)指针的数组,以炭的阵列,其不能被修改。通过example[0]阅读第一个。

+0

我需要一个更动态的方法。 –

5

我真的不知道你想要什么,但如果你想包含指向字符串数组的数组,那么你有两个选择。

首先是要记住,阵列衰减到一个指向它的第一个元素。对于字符串的像

const char *array_of_strings[3] = { "abc", "def", "ghi" }; 

则衰减(其等于&array_of_strings[0])的结果的阵列是一个指向一个指针指向一个const char,即const char**。这意味着有一个数组,你让像

const char **array_or_arrays_1[] = { array_of_strings }; 

另一种方法是有一个指向字符串数组明确,使用运营商&的地址。当你&array_of_strings你得到一个指针的三个指针数组char,型号为const char *(*)[3]。你可以使用类型别名轻松声明它:

typedef const char *(array_of_3_strings_t)[3]; 
array_of_3_strings *array_of_arrays_2[] = { &array_of_strings }; 
+0

我该如何让这更具动感? –

+0

@JeffGreene类型别名(使用'typedef')和'malloc'? –

+0

@JeffGreene更多“*动态*”是什么意思? – RoadRunner

0

我想了解你在说什么“更动态的方法”。 也许你想使列表的数组,其中每个列表(串)可以有不同的尺寸。如果这是你想要的,这个代码可以帮助:

#include<stdio.h> 
#include<string.h> 
typedef char * string; 
int main() 
{ 
    string array1[]={"apple","durian"}; 
    string array2[]={"mustard","carrot","spinach"}; 
    string array3[]={"garlic","onion","pepper", "blackpepper"}; 

    string 
     * array_of_array[]={ array1, 
          array2, 
          array3 }; 


    printf("\n%s %s", array_of_array[0][1],array_of_array[2][3]); 

    return 0; 
} 

控制台结果:

榴莲blackpepper

但是,如果你的意思是所有的资源必须动态初始化,所以这段代码可帮助...

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

typedef char * string; 
typedef string * arrayString; 
typedef arrayString * arrayList; 

string newString(char * temp); 
arrayString newArrayString(int size); 
arrayList newArrayList(int size); 

int main() 
{ 
//Example 1, static allocation, string constant 
    string array1[]={"apple","durian"}; 
    string array2[]={"mustard","carrot","spinach"}; 
    string array3[]={"garlic","onion","pepper", "blackpepper"}; 

    arrayString Example_One[3]={ array1, array2, array3 }; 

//Example 2, dynamic allocation using pointer struct 
/************************************************************/ 
    arrayList Example_Two=newArrayList(3); 
     arrayString ExTwoValue1=newArrayString(2); 
     arrayString ExTwoValue2=newArrayString(3); 
     arrayString ExTwoValue3=newArrayString(4); 

     Example_Two[0]=ExTwoValue1; 
      ExTwoValue1[0]=newString("apple2"); 
      ExTwoValue1[1]=newString("durian2"); 
     Example_Two[1]=ExTwoValue2; 
      ExTwoValue2[0]=newString("mustard2"); 
      ExTwoValue2[1]=newString("carrot2"); 
      ExTwoValue2[2]=newString("spinach2"); 
     Example_Two[2]=ExTwoValue3; 
      ExTwoValue3[0]=newString("garlic2"); 
      ExTwoValue3[1]=newString("onion2"); 
      ExTwoValue3[2]=newString("pepper2"); 
      ExTwoValue3[3]=newString("blackpepper2"); 

/************************************************************/ 

//Print Result Here...  
    printf("\n%s %s", Example_One[0][0],Example_One[2][3]); 
    printf("\n\n%s %s", Example_Two[0][0],Example_Two[2][3]); 

    return 0; 
} 

string newString(char * temp) 
{ 
    string str = (string)malloc(sizeof(char)*(strlen(temp)+2)); 
    strcpy(str,temp); 
    return str; 
} 

arrayString newArrayString(int size) 
{ 
    arrayString arrStr= (arrayString)malloc(sizeof(string)*size); 
    return arrStr; 
} 

arrayList newArrayList(int size) 
{ 
    arrayList arrLst= (arrayList)malloc(sizeof(arrayString)*size); 
    return arrLst; 
} 

控制台结果:

苹果blackpepper

apple2 blackpepper2