2016-08-08 133 views
2

我需要通過串的預分配的數組作爲函數參數,和strcpy()到每個串陣列內的串,如在這個例子中:Ç - 傳遞一個字符串數組作爲函數參數

static void string_copy(char * pointer[]) { 

    strcpy(pointer[0], "Hello "); 

    strcpy(pointer[1], "world"); 

} 

int main(int argc, const char * argv[]) { 

    char my_array[10][100]; 

    string_copy(my_array); 

    printf("%s%s\n", my_array[0], my_array[1]); 

} 

最終的打印字符串將是'Hello world'。

如何傳遞一個預分配的字符串數組並填充函數中的每個字符串,如上所示?

+1

使用字符**和一個for循環 –

+0

正如在「靜態無效string_array(字符**指針[])」?這沒有編譯。 –

回答

3

當你在做string_copy(my_array)時,你傳遞一個char (*)[100],即pointer to char[100] array到你的函數。但是你的函數期待char *[],即array of char pointers,因爲你已經用這種方式定義了你的函數。

您可以通過進行更改以使您的功能(string_copy())預計爲char (*)[100]而不是char *[]來解決此問題。

對於這一點,你可以改變你的函數定義爲:

/* Your my_array gets converted to pointer to char[100] 
    so, you need to change your function parameter 
    from `char *pointer[]` to `char (*pointer)[100]` 
*/ 
/* static void string_copy(char *pointer []) */ 
static void string_copy(char (*pointer) [100]) 
{ 
    strcpy(pointer[0], "Hello "); 
    strcpy(pointer[1], "world"); 
} 

*替代解決方案*

不同的設計/解決方案將是您的main()功能,從而改變你是實際上通過char *[],它衰變爲char ** - 這很好 - 到string_copy()。這樣你就不必更改你的string_copy()函數。

int main(int argc, const char * argv[]) { 

    char my_array[10][100]; 
    int tot_char_arrs, i; 
    char *char_arr_ptr[10]; 

    /* Get total number of char arrays in my_array */ 
    tot_char_arrs = sizeof(my_array)/sizeof(my_array[0]); 

    // Store all char * 
    for (i = 0; i < tot_char_arrs; i++) 
      char_arr_ptr[i] = my_array[i]; 

    /* Actually passing a char *[]. 
     it will decay into char **, which is fine 
    */ 
    string_copy(char_arr_ptr); 

    printf("%s%s\n", my_array[0], my_array[1]); 
} 
+0

哦,我明白了。你有兩個版本,對不起。 – 2501

+0

@ 2501我編輯了這篇文章,並強調有兩個版本,這樣就不會混淆未來的讀者。我想我以前的做法可能會讓人們感到困惑。 – sps

1

你的函數可以簡單地接受矩陣尺寸,並將一個const char *存儲文字的陣列(預分配)字符串:

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

#define STRINGS_LENGTH 100 

static void string_copy(size_t n, size_t m, char pointer[n][m], const char *strings_to_copy[]) 
{ 
    for (size_t i=0; i< n; i++) 
    { 
     strcpy(pointer[i], strings_to_copy[i]); 
    } 
} 

int main(void) 
{ 
    const char *strings[] = { "hello", "World" }; 
    char my_array[sizeof(strings)/sizeof(strings[0])][STRINGS_LENGTH]; 

    string_copy(sizeof(strings)/sizeof(strings[0]), STRINGS_LENGTH, my_array, strings); 

    printf("%s %s\n", my_array[0], my_array[1]); 
} 

您也可以使用動態分配的改變你的代碼的結構爲您的輸出陣列,如:

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

static bool string_copy(char *pointer[], const char *strings_to_copy[], size_t strings) 
{ 

    for (size_t i=0; i< strings; i++) 
    { 
     pointer[i] = malloc(strlen(strings_to_copy[i])+1); 

     if (pointer[i] != NULL) 
      strcpy(pointer[i], strings_to_copy[i]); 
     else 
      return false; 
    } 

    return true; 
} 

int main(void) 
{ 
    const char *strings[] = { "hello", "World" }; 
    char *my_array[sizeof(strings)/sizeof(strings[0])] = {0}; 

    if (string_copy(my_array, strings, sizeof(strings)/sizeof(strings[0]))) 
    { 
     printf("%s %s\n", my_array[0], my_array[1]); 
    } 

    for (size_t i = 0; i<sizeof(strings)/sizeof(strings[0]); i++) 
     free (my_array[i]); 
} 
3

您需要使用指向該數組的指針。這裏是用1名維陣列的例子:

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

static void string_copy(char **pointer) { 

    strcpy(pointer[0], "Hello "); 

} 

int main(int argc, const char * argv[]) { 

    char my_array[10]; 
    char * p_array = my_array; 

    string_copy(&p_array); 

    printf("%s\n", my_array); 

} 
相關問題