Advertisement
Guest User

Untitled

a guest
Jul 24th, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. char *s_one[] = { "Zorro", "Alex", "Celine" };
  2. char *s_two[] = { "Zorro1", "Alex1"};
  3.  
  4. char *p = (char*)malloc((sizeof(s_one)+sizeof(s_two))*sizeof(char));
  5. memcpy(p, s_one, sizeof(s_one));
  6. memcpy(p + sizeof(s_one), s_two, sizeof(s_two));
  7.  
  8. //print out
  9. for (count = 0; count < sizeof(p); count++)
  10. printf("narr[%d] = %c.", count, p[count]);
  11.  
  12. printf("narr[%d] = %s.", count, *p[count]);
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17.  
  18. int main(void)
  19. {
  20. char *s_one[] = { "Zorro", "Alex", "Celine" };
  21. char *s_two[] = { "Zorro1", "Alex1"};
  22.  
  23. printf("%lun", sizeof(s_one));
  24. printf("%lun", sizeof(s_two));
  25.  
  26. int numberOfEntries = (sizeof(s_one) + sizeof(s_two)) / sizeof(char*);
  27. char **p = (char **)malloc(numberOfEntries);
  28.  
  29. printf("%dn", numberOfEntries);
  30.  
  31. memcpy(p, s_one, sizeof(s_one));
  32. memcpy(p + sizeof(s_one)/sizeof(char *), s_two, sizeof(s_two));
  33.  
  34. //print out
  35. int count = 0;
  36. for (count = 0; count < numberOfEntries; count++)
  37. printf("arr[%d] = %s.n", count, p[count]);
  38. }
  39.  
  40. char **p = malloc(sizeof s_one + sizeof s_two);
  41.  
  42. size_t needed = 1;
  43. for(size_t i = 0; i < sizeof s_one / sizeof *s_one; ++i)
  44. needed += strlen(s_one[i]);
  45. for((size_t i = 0; i < sizeof s_two / sizeof *s_two; ++i)
  46. needed += strlen(s_two[i]);
  47. char *p = malloc(needed);
  48. if (!p) {
  49. // allocation failed
  50. exit(EXIT_FAILURE);
  51. }
  52. p[0] = 0;
  53. for(size_t i = 0; i < sizeof s_one / sizeof *s_one; ++i)
  54. strcat(p,s_one[i]);
  55. for(size_t i = 0; i < sizeof s_two / sizeof *s_two; ++i)
  56. strcat(p,s_two[i]);
  57.  
  58. char *s_one[] = { "Zorro", "Alex", "Celine" };
  59. char *s_two[] = { "Zorro1", "Alex1"};
  60.  
  61. char *p[sizeof(s_one)/sizeof(char *) + sizeof(s_two)/sizeof(char *)];
  62. memset(p, 0, sizeof(p));
  63. for(count=0;count < sizeof(s_one)/sizeof(char *) ;count++) {
  64. p[count] = (char*)malloc((strlen(s_one[count])+1)*sizeof(char));
  65. strcpy(p[count], s_one[count]);
  66. }
  67. i=count;
  68. for(count=0;count < sizeof(s_two)/sizeof(char *) ;count++) {
  69. p[i] = (char*)malloc((strlen(s_two[count])+1)*sizeof(char));
  70. strcpy(p[i], s_two[count]);
  71. i++;
  72. }
  73.  
  74. //print out
  75. for (count = 0; count < sizeof(p)/sizeof(char *); count++)
  76. printf("narr[%d] = %s.", count, p[count]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement