Concatenating two arrays of strings?

≯℡__Kan透↙ 提交于 2019-12-08 02:11:53

问题


I am new to the c world and I want to merge two arrays into one array, i have one idea how to do this, but it doesnt work :P

char *s_one[] = { "Zorro", "Alex", "Celine" };
char *s_two[] = { "Zorro1", "Alex1"};

char *p = (char*)malloc((sizeof(s_one)+sizeof(s_two))*sizeof(char));
memcpy(p, s_one, sizeof(s_one));
memcpy(p + sizeof(s_one), s_two, sizeof(s_two));

//print out
for (count = 0; count < sizeof(p); count++)
        printf("\narr[%d] = %c.", count, p[count]);

the output is just some random characters... what i am doing wrong, thanks in advance for every tipp

the output should be: Zorro Alex Celine Zorro1 Alex1


回答1:


Instead of using memcpy I would recommend to iterate over each of the two arrays and copy one string from the source array into p using strcpy. The problem of memcpy is that it should omit to copy the \0 character, hence your random characters.

You malloc instruction is fine.




回答2:


Let's see if I can get this right...

s_one is an array of char *. So, your sizeof() and memcpy() operations are likely operating on the pointers, not the strings that they point to somewhere else in memory. Then, when you go and printf() you are printing the pointers as characters rather than the strings they point to (as strings). Not sure here, but perhaps this would work?

printf("\narr[%d] = %s.", count, *p[count]);

If you don't get it fixed before I get home, I'll test it out and see.


Combine with Marvo's comment about the sizeof() calls.


It really depends on what you want, but here's my attempt since I said I would post it:

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

int main(void)
{
    char *s_one[] = { "Zorro", "Alex", "Celine" };
    char *s_two[] = { "Zorro1", "Alex1"};

    printf("%lu\n", sizeof(s_one));
    printf("%lu\n", sizeof(s_two));

    int numberOfEntries = (sizeof(s_one) + sizeof(s_two)) / sizeof(char*);
    char **p = (char **)malloc(numberOfEntries);

    printf("%d\n", numberOfEntries);

    memcpy(p, s_one, sizeof(s_one));
    memcpy(p + sizeof(s_one)/sizeof(char *), s_two, sizeof(s_two));

    //print out
    int count = 0;
    for (count = 0; count < numberOfEntries; count++)
        printf("arr[%d] = %s.\n", count, p[count]);
}



回答3:


You have two arrays of pointers to char (actually pointing to the first character of a 0-terminated char[]). So when you memcpy from s_one and s_two to p, you copy the pointers, and what you print out are parts of the pointer values.

If you declared

char **p = malloc(sizeof s_one + sizeof s_two);

you'd get an array of five char*, pointing to the respective strings.

If you want to concatenate the strings that the elements of s_one resp. s_two point to, you need to allocate enough to hold the result (plus the 0-terminator):

size_t needed = 1;
for(size_t i = 0; i < sizeof s_one / sizeof *s_one; ++i)
    needed += strlen(s_one[i]);
for((size_t i = 0; i < sizeof s_two / sizeof *s_two; ++i)
    needed += strlen(s_two[i]);
char *p = malloc(needed);
if (!p) {
    // allocation failed
    exit(EXIT_FAILURE);
}
p[0] = 0;
for(size_t i = 0; i < sizeof s_one / sizeof *s_one; ++i)
    strcat(p,s_one[i]);
for(size_t i = 0; i < sizeof s_two / sizeof *s_two; ++i)
    strcat(p,s_two[i]);



回答4:


Just two tips for a less noisy malloc:
You don't need to cast it's return value.
You don't need the sizeof(char) as a char by definition is 1 byte.




回答5:


my first solution is to iterate over each of the two arrays and copy one string from the source array into p using strcpy... thanks to Hernan Velasquez

char *s_one[] = { "Zorro", "Alex", "Celine" };
char *s_two[] = { "Zorro1", "Alex1"};

char *p[sizeof(s_one)/sizeof(char *) + sizeof(s_two)/sizeof(char *)];
memset(p, 0, sizeof(p));
for(count=0;count < sizeof(s_one)/sizeof(char *) ;count++) {
    p[count] = (char*)malloc((strlen(s_one[count])+1)*sizeof(char));
    strcpy(p[count], s_one[count]);
}
i=count;
for(count=0;count < sizeof(s_two)/sizeof(char *) ;count++) {
    p[i] = (char*)malloc((strlen(s_two[count])+1)*sizeof(char));
    strcpy(p[i], s_two[count]);
    i++;
}

//print out
for (count = 0; count < sizeof(p)/sizeof(char *); count++)
        printf("\narr[%d] = %s.", count, p[count]);

i think it is not very clean, but it works for me... tomorow i try it with the tipp from Daniel Fischer



来源:https://stackoverflow.com/questions/12903745/concatenating-two-arrays-of-strings

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!