Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: None  |  size: 0.82 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. C pointer to pointer
  2. int **p
  3.        
  4. int *p[1]
  5.        
  6. int **p;
  7.        
  8. int *p[1];
  9.        
  10. void foo()
  11. {
  12.     int **ptr;
  13.     int *array[10];
  14.  
  15.     sizeof(ptr);   // just the size of the pointer
  16.     sizeof(array); // 10 * sizeof(int *)
  17.  
  18.     // popular idiom for getting count of elements in array:
  19.     sizeof(array)/sizeof(array[0]);
  20. }
  21.  
  22. // this would always discard the array size,
  23. // because the argument always decays to a pointer
  24. size_t my_sizeof(int *p) { return sizeof(p); }
  25.        
  26. int **p
  27.        
  28. int *p[1];
  29.        
  30. int **p
  31.        
  32. int *p[1]
  33.        
  34. int **pp;   // pointer to pointer
  35. int *ap[1]; // array of pointer
  36.        
  37. foo(ap);
  38.        
  39. pp = malloc(sizeof *pp * N); // allocate N pointers to int (type of *pp == int *)
  40. if (pp)
  41. {
  42.   size_t i;
  43.   for (i = 0; i < N; i++)
  44.     pp[i] = ...;             // set pp[i] to point to some int value
  45. }
  46.        
  47. ap = some_new_pointer_value();