Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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]);
- printf("narr[%d] = %s.", count, *p[count]);
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- int main(void)
- {
- char *s_one[] = { "Zorro", "Alex", "Celine" };
- char *s_two[] = { "Zorro1", "Alex1"};
- printf("%lun", sizeof(s_one));
- printf("%lun", sizeof(s_two));
- int numberOfEntries = (sizeof(s_one) + sizeof(s_two)) / sizeof(char*);
- char **p = (char **)malloc(numberOfEntries);
- printf("%dn", 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]);
- }
- char **p = malloc(sizeof s_one + sizeof s_two);
- 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]);
- 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]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement