简体   繁体   English

如何将字符串数组指针传递给新的字符数组? 在 C

[英]How to pass a string array pointer to a new character array? in C

I have an array pointer and I try to pass strings of this pointer to a new array.我有一个数组指针,我尝试将这个指针的字符串传递给一个新数组。 However, I can not print out the new array.但是,我无法打印出新数组。 My code is:我的代码是:

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

int main() {
    char *array[5] = {"may", "june", "july", "august", "september"};
    char buffer[5];
    for (int i=0; i<5; i++) {
        buffer[i] = *array[i];
        printf(%s , buffer[i]);     
    }
}

I can not compile the program because it got error of %s.我无法编译该程序,因为它出现了 %s 错误。 How can I print out the buffer array like:如何打印出缓冲区数组,如:

may june july august september五月七月八月九月

without changing it format char buffer[5] which means I do not want to change it to *buffer[5] or anything else.不更改它的格式 char buffer[5] 这意味着我不想将其更改为 *buffer[5] 或其他任何内容。 Thank you.谢谢你。

Not sure exactly what you're wanting to do.不确定你到底想要做什么。 You can use strcpy() to copy the contents of one array to another.您可以使用 strcpy() 将一个数组的内容复制到另一个数组。

char* array[5] = {"may", "june", "july", "august", "september"};
char arr[10];

for(i = 0; i < 5; ++i) {
    strcpy(arr, array[i]);
    printf("%s\n", arr);
}

Or if you want to do it like this instead with a 2d char array.或者,如果您想使用 2d 字符数组来做这样的事情。

char* array[5] = {"may", "june", "july", "august", "september"};
char arr2d[5][10];

for(i = 0; i < 5; ++i) {
    strcpy(arr2d[i], array[i]);
    printf("%s\n", arr2d[i]);
}

I can not compile the program because it got error of %s.我无法编译该程序,因为它出现了 %s 错误。 How can I print out the buffer array like:如何打印出缓冲区数组,如:

 may june july august september

Quite simply print out each index of array , eg很简单地打印出array每个索引,例如

#include <stdio.h>

int main() {

    char *array[] = {"may", "june", "july", "august", "september"};
    int nmembers = sizeof array / sizeof *array;

    for (int i = 0; i < nmembers; i++)
        printf(" %s", array[i]); 
    putchar ('\n');     /* tidy up with newline */
}

note: do not use magic numbers in your code.注意:不要在代码中使用幻数 There is no need.没有必要。 You can compute the number of elements in array using sizeof (only when array is declared within the current scope)您可以使用sizeof计算array的元素数(仅当在当前范围内声明array时)

Example Use/Output示例使用/输出

$ ./bin/parraymess
 may june july august september

Why does it work that way?为什么它会这样工作?

char *array[] = {"...", ...}; declares an array of pointers to char and each pointer making up the array is initialized to point to the beginning of each string-literal used within the initializer., eg声明一个指向char的指针数组,并且组成该数组的每个指针都被初始化为指向初始化程序中使用的每个字符串文字的开头。例如

array[0] = "may";
array[1] = "june";
...

Since you have an array of pointers to char , each element is a pointer to char , eg char* .由于您有一个指向char的指针数组,因此每个元素都是指向char指针,例如char* When you access each individual member of an array using name[..] the [..] acts as a dereference .当您使用name[..]访问数组的每个单独成员时, [..]充当取消引用 So simply looping over each element for (int i = 0; i < nmembers; i++) and then accessing array[i] references a pointer (eg char* ) to the beginning of each string that can be used with the "%s" format specifier in printf .所以简单地循环遍历每个元素for (int i = 0; i < nmembers; i++)然后访问array[i]引用一个指向每个字符串开头的指针(例如char* ),可以与"%s" printf格式说明符。

Look things over and let me know if you have further questions.仔细检查一下,如果您还有其他问题,请告诉我。

You cannot store a string in space of a char .您不能在char空间中存储字符串。 You can either have an array of char * for a string (and allocate memory dynalically) or you can store the strings in a two-dimensional char array eg:您可以为字符串使用char *数组(并动态分配内存),也可以将字符串存储在二维字符数组中,例如:

char buffer[5][MAX_LEN];

and then you may copy strings from array to buffer using either strcpy or by writing your own function.然后您可以使用strcpy或编写您自己的函数将字符串从array复制到buffer

How about this:这个怎么样:

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

int main(void) {
    char *array[5] = {"may", "june", "july", "august", "september"};
    char *buffer[5];
    int i;
    for (i=0; i<5; i++) {
        buffer[i] = strdup(array[i]);
        printf("%s ", buffer[i]);
    }
    return 0;
}

You need buffer to be an array of 5 pointers to char.您需要buffer是指向 char 的5指针的数组。 You have declared buffer as a char array of size 5 , this will not be large enough to hold strings larger than 4 chars (mind the trailing \\0 ).您已将buffer声明为大小为5char数组,这将不足以容纳大于4字符的字符串(请注意尾随\\0 )。

strdup is a function which takes a c-string, and writes that string to some space it allocates by calling malloc and returns a pointer to that new space. strdup是一个函数,它接受一个 c 字符串,然后将该字符串写入它通过调用malloc分配的某个空间,并返回一个指向该新空间的指针。

If you really need buffer be a char array, you should make sure it can hold the longest string you want to write to it and a trailing \\0 .如果你真的需要buffer是一个char数组,你应该确保它可以保存你想要写入的最长字符串一个尾随\\0 Then you could use the strcpy function to write data to buffer .然后您可以使用strcpy函数将数据写入buffer Like so:像这样:

int main(void) {
    char *array[5] = {"may", "june", "july", "august", "september"};
    char buffer[12];
    int i;
    for (i=0; i<5; i++) {
        strcpy(buffer, array[i]);
        printf("%s ", buffer);
    }
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM