2017-02-20 167 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