Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. void by_value(const T* array) // const T array[] means the same
  2. void by_pointer(const T (*array)[U])
  3. void by_reference(const T (&array)[U])
  4.  
  5. const int a[] = { 2, 3, 5, 7, 11 };
  6.  
  7. const int* p = a;
  8.  
  9. assert( sizeof(p) != sizeof(a) ); // sizes are not equal
  10.  
  11. char a[80];
  12. strcpy(a, "This is a test");
  13.  
  14. char *strcpy(char *dest, const char *src);
  15.  
  16. char a[80];
  17. char *ptr_to_first_element = a;
  18. char (*ptr_to_array)[80] = &a;
  19.  
  20. a[i] == ptr_to_first_element[i] == (*ptr_to_array)[i] != *ptr_to_array[i] != ptr_to_array[i]
  21.  
  22. sizeof a == sizeof *ptr_to_array == 80
  23.  
  24. sizeof *ptr_to_first_element == sizeof (char) == 1
  25. sizeof ptr_to_first_element == sizeof (char *) == whatever the pointer size
  26. is on your platform
  27.  
  28. void foo(int arr[]);
  29.  
  30. int arr[5];
  31. int *ip = &(arr[1]);
  32. if (arr == ip) { /* something; */ }
  33.  
  34. arr[42];
  35. /* same as *(arr + 42); */
  36. /* same as *(&(arr[0]) + 42); */
  37.  
  38. void do_something(int *array) {
  39. // We don't know how big array is here, because it's decayed to a pointer.
  40. printf("%in", sizeof(array)); // always prints 4 on a 32-bit machine
  41. }
  42.  
  43. int main (int argc, char **argv) {
  44. int a[10];
  45. int b[20];
  46. int *c;
  47. printf("%zun", sizeof(a)); //prints 40 on a 32-bit machine
  48. printf("%zun", sizeof(b)); //prints 80 on a 32-bit machine
  49. printf("%zun", sizeof(c)); //prints 4 on a 32-bit machine
  50. do_something(a);
  51. do_something(b);
  52. do_something(c);
  53. }
  54.  
  55. void do_something(int array[][10])
  56. {
  57. // We don't know how big the first dimension is.
  58. }
  59.  
  60. int main(int argc, char *argv[]) {
  61. int a[5][10];
  62. int b[20][10];
  63. do_something(a);
  64. do_something(b);
  65. return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement