Guest User

Untitled

a guest
Jul 16th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. char* names[]={"A", "B", "C"};
  2.  
  3. num = sizeof(names) / sizeof(names[0]);
  4.  
  5. #include<stdio.h>
  6.  
  7. int main(void)
  8. {
  9. char* names1[]={"A", "B", "C"}; // Three elements
  10. char* names2[]={"A", "", "C"}; // Three elements
  11. char* names3[]={"", "A", "C", ""}; // Four elements
  12. char* names4[]={"John", "Paul", "George", "Ringo"}; // Four elements
  13. char* names5[]={"", "B", NULL, NULL, "E"}; // Five elements
  14.  
  15. printf("len 1 = %zun",sizeof(names1)/sizeof(names1[0]));
  16. printf("len 2 = %zun",sizeof(names2)/sizeof(names2[0]));
  17. printf("len 3 = %zun",sizeof(names3)/sizeof(names3[0]));
  18. printf("len 4 = %zun",sizeof(names4)/sizeof(names4[0]));
  19. printf("len 5 = %zun",sizeof(names5)/sizeof(names5[0]));
  20. }
  21.  
  22. len 1 = 3
  23. len 2 = 3
  24. len 3 = 4
  25. len 4 = 4
  26. len 5 = 5
  27.  
  28. int namesLen = sizeof(names) / sizeof(char);
  29.  
  30. char **names = { "A", "B", "C" };
  31.  
  32. int namesLen = 3;
  33.  
  34. char **names = { "A", "B", "C", NULL };
  35.  
  36. int namesLen = -1;
  37. while (names[++namesLen] != NULL) { /* do nothing */}
  38.  
  39. // namesLen is now the length of your array
  40.  
  41. struct Array {
  42. void **values;
  43. int length;
  44. };
  45.  
  46. #define array(elements...) ({ void *values[] = { elements }; array_make(values, sizeof(values) / sizeof(void *)); })
  47. #define destroy(arr) ({ free(arr.values); })
  48.  
  49. struct Array array_make(void **elements, int count)
  50. {
  51. struct Array ret;
  52. ret.values = malloc(sizeof(void *) * count);
  53. ret.length = count;
  54.  
  55. for (int i = 0; i < count; i++) {
  56. ret.values[i] = elements[i];
  57. }
  58.  
  59. return ret;
  60. }
  61.  
  62. struct Array myArray = array("Hi", "There", "This", "Is", "A", "Test");
  63. // use myArray
  64.  
  65. printf("%i", myArray.length);
  66. destroy(myArray);
  67.  
  68. size_t size = sizeof(names)/sizeof(char*);
  69.  
  70. int count = sizeof(names)/sizeof(*names);
  71.  
  72. #include<stdio.h>
  73.  
  74. int main(void){
  75. size_t size, i=0;
  76. int count=0;
  77. char* names[]={"A", "B", "C"};
  78.  
  79. size = sizeof(names)/sizeof(char*);
  80.  
  81. for(i=0;i<size;i++){
  82. printf("%d - %sn",count+1, *(names+i));
  83. count++;
  84. }
  85.  
  86. printf("n");
  87. printf("The number of strings found are %dn",count);
  88. return 0;
  89. }
  90.  
  91. 1 - A
  92. 2 - B
  93. 3 - C
  94.  
  95. The number of strings found are 3
  96.  
  97. #include<stdio.h>
  98.  
  99. int main(void){
  100. size_t size, i=0;
  101. int count=0;
  102. char *names[]={"Jimmy", "Tom", "Michael", "Maria", "Sandra", "Madonna"};
  103.  
  104. size = sizeof(names)/sizeof(char*);
  105.  
  106. for(i=0;i<size;i++){
  107. printf("%d - %sn",count+1, *(names+i));
  108. count++;
  109. }
  110.  
  111. printf("n");
  112. printf("The number of strings found are %dn",count);
  113. return 0;
  114. }
  115.  
  116. 1 - Jimmy
  117. 2 - Tom
  118. 3 - Michael
  119. 4 - Maria
  120. 5 - Sandra
  121. 6 - Madonna
  122.  
  123. The number of strings found are 6
  124.  
  125. #include<stdio.h>
  126.  
  127. size_t arrLength(size_t len, char **arr){
  128. size_t i=0;
  129. size_t count=0;
  130.  
  131. for(i=0;i<len;i++){
  132. printf("%zu - %sn",count+1, *(arr+i));
  133. count++;
  134. }
  135.  
  136. return count;
  137. }
  138.  
  139. int main(void){
  140. char *names[]={"Jimmy", "Tom", "Michael", "Maria", "Sandra", "Madonna"};
  141. size_t length,size;
  142.  
  143. size = sizeof(names)/sizeof(char*);
  144. length = arrLength(size, names);
  145.  
  146. printf("n");
  147. printf("The number of strings found are %zun",length);
  148. return 0;
  149. }
  150.  
  151. 1 - Jimmy
  152. 2 - Tom
  153. 3 - Michael
  154. 4 - Maria
  155. 5 - Sandra
  156. 6 - Madonna
  157.  
  158. The number of strings found are 6
  159.  
  160. #include <stdio.h>
  161.  
  162. int main()
  163. {
  164. char* names[] = {"John", "Paul", "George", "Ringo", ''};
  165.  
  166. int size = 0;
  167. while(names[++size]!='');
  168.  
  169. printf("Size: %dn", size); // Prints "4"
  170.  
  171. return 0;
  172. }
  173.  
  174. // For size_t
  175. #include <stddef.h>
  176.  
  177. char* names[] = { "A", "B", "C" };
  178. size_t number_of_strings = sizeof names / sizeof names[0];
  179.  
  180. printf("Number of strings %zun", number_of_strings);
  181.  
  182. char* names[] = {"An", "Bed", "Cards"};
  183.  
  184. int arrayIndex, counter;
  185. for (arrayIndex = 0; arrayIndex < (sizeof(names)/sizeof(char*)); arrayIndex++) {
  186. //counting also chars in each string (extra, not necessarily the question)
  187. char* stringAtIndex = names[arrayIndex];
  188. char c;
  189. int i = 0;
  190. do {
  191. c = *(stringAtIndex + i++);
  192. } while (c != '');
  193. counter += (i-1); //exclude terminating char ''
  194. }
  195. printf("Number of array elements: %d", arrayIndex+1); //number of strings
  196. printf("Total chars %dn", counter); // Prints 10 which is number of chars excluding ''
  197.  
  198. #include<stdio.h>
  199. char* names[]={"A", "B", "C"};
  200.  
  201. int main(){
  202. int nameSize = 0;
  203. while(names[++nameSize]!='');
  204. }
  205.  
  206. for(i=0;i<nameSize;i++){
  207. printf("%sn", names[i]);
  208. }
  209.  
  210. return 0;
  211. }
  212.  
  213. A
  214. B
  215. C
Add Comment
Please, Sign In to add comment