Advertisement
baldengineer

using pointers with 2d array

Apr 2nd, 2020
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.26 KB | None | 0 0
  1. // based on the examples from:
  2. // https://www.geeksforgeeks.org/pointer-array-array-pointer/
  3. // Read the explanation and then search for "C program to print elements of a 2-D array"
  4.  
  5. int Red1;
  6. int Green1;
  7. int Blue1;
  8.  
  9. // You need to fix your dimensions. You have them backward in your code
  10. int zombies[6][3] = {
  11.   {101, 52, 150},
  12.   {123, 82, 174},
  13.   {116, 182, 82},
  14.   {148, 199, 115},
  15.   {86, 148, 30},
  16.   {71, 24, 113}
  17. };
  18.  
  19. int sunset[6][3] = {
  20.   {250, 162, 117},
  21.   {255, 140, 97},
  22.   {206, 106, 133},
  23.   {152, 82, 119},
  24.   {92, 55, 76},
  25.   {92, 55, 76}
  26. };
  27.  
  28. void setup() {
  29.   // put your setup code here, to run once:
  30.  Serial.begin(9600);
  31.  
  32.  // a variable to select which array to load
  33.   byte array_select = 0;
  34.  
  35.  // create a pointer variable to the inner-dimension
  36.  int (*array_ptr)[3];
  37.  
  38.  // select which array to load
  39.  if (array_select == 0)
  40.   // point the pointer to the array
  41.   array_ptr = zombies;
  42.  
  43.  if (array_select == 1)
  44.   array_ptr = sunset;
  45.  
  46.   // load the values from the array
  47.   Red1   = array_ptr[0][0];
  48.   Green1 = array_ptr[0][1];
  49.   Blue1  = array_ptr[0][2];
  50.  
  51.   Serial.println(F("---"));
  52.   Serial.println(Red1);
  53.   Serial.println(Green1);
  54.   Serial.println(Blue1);
  55. }
  56.  
  57. void loop() {
  58.   // put your main code here, to run repeatedly:
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement