3-dim Arrays und Pointer



  • Wie gesagt: Der Tipp ist eine for-Schleife. Da du die Länge der einzelnen Arrays kennst, ist ein "sizeof()" sowieso überflüssig. Die Werte dann mittels Zeigerarithmetik statt Operatoraufruf auszugeben ist ein Leichtes, wenn du weißt, dass der Name des Arrays ein Zeiger auf das erste Element ist. Allerdings ist mir nicht klar, wieso die übliche Ausgabemethode mittels des []-Operators nicht erlaubt sein sollte.



  • Einfach drei ineinander geschachtelte for-Schleifen mit einem Zeiger auf das erste Element.



  • p[1] ist äquivalent zu *(p + 1), also wäre p[1][2] z.b. äquivalent zu ((p + 1) + 2)

    von hier gehts hoffentlich von allein 😉



  • Bei jedem durchlauf einfach den Pointer um 1 erhöhen.



  • // Program: threedim_array.C
    // iterate over a multidimensional array
    
    #include <iostream>
    
    int main() 
    {
      int a[4][2][3] = 
        {              // the 4 elements of a:
          {            // the 2 elements of a[0]:
    	{2, 4, 5}, // the three elements of a[0][0]
    	{4, 6, 7}  // the three elements of a[0][1]
          }, 
          {            // the 2 elements of a[1]:
    	{1, 5, 9}, // the three elements of a[1][0]
    	{4, 6, 1}  // the three elements of a[1][1]
          }, 
          {            // the 2 elements of a[2]:
    	{5, 9, 0}, // the three elements of a[2][0]
    	{1, 5, 3}  // the three elements of a[2][1]
          }, 
          {            // the 2 elements of a[3]:
    	{6, 7, 7}, // the three elements of a[3][0]
    	{7, 8, 5}  // the three elements of a[3][1]
          } 
        };
    
      int (*p)[2][3]=a; 
    
      for (int i=0,j,k; i<4; i++) {
           std::cout<< "\n";
        for(j=0;j<2;j++) {
    
          for(k=0;k<3;++k){
    	std::cout<< a[i][j][k] << " ";
    
    	}
         }
       } 
    
      return 0;
    
    }
    

    Danke für eure tipps - natürlich macht ihr meine Hausaufgaben nicht, habs ja selber versucht, brauchte einfach ein paar inputs...

    Ok, das obige Programm funktioniert und ich kriege den Output:

    2 4 5 4 6 7
    1 5 9 4 6 1
    5 9 0 1 5 3
    6 7 7 7 8 5

    , was mir gefällt.

    Zwei Sachen sind mir jetzt aber noch unklar, hoffe, jemand kann mir das erklären.

    1. Habe ich eigentlich in meinem Code die gewünschte Zeigerarithmetik verwendet - mir ist leider immernoch nicht so klar, was das überhaupt wirklich ist, obschon ich nochmals das Skript durchgelesen und mir die Power-Pointpräsentation aus der Vorlesung noch einmal sorgfältig angeschaut habe 😞
    2. Der Pointer selbst: Es compiliert ja mit
    int (*p)[2][3]=a;
    

    Ich begreife aber - und das hängt mit 1) zusammen - nicht ganz, wieso der so dastehen muss. Zuerst habe ich versucht: int (*p)[4]=a; Dann kam eine Meldung, dass die Konversion nicht klappt, dann machte ich mal int (*p)[4][2][3]=a; dieselbe Meldung, dann probierte ich einfach mal das obige und das klappte.

    Mit grossem Dank im Voraus auf alle Erklärungen,

    Damian



  • Warum schreibst du nicht oben

    using namespace std;
    

    Damit könntest du dir das std:: sparen!



  • D schrieb:

    Warum schreibst du nicht oben

    using namespace std;
    

    Damit könntest du dir das std:: sparen!

    Mach es nicht!



  • Warum denn nicht?



  • Schau dir einfach mal ein paar alte Threads an. In ca. jedem 20sten steht warum.



  • Bin zu faul zum suchen...



  • Tja dann wirst du auch keine Erklärung bekommen. Aber mal als Anregung: Wofür gibt es Namensräume? Was ist ihr Sinn & Nutzen? ...



  • Halte ich zwar für übertrieben bei so 'nem simplen Beispiel, aber prinzipiell haben die Verfechter des sauberen Codes schon Recht. Benutze also lieber die using-Direktive mit std::cout.



  • So nach 2 Stunden pröbeln etc. habe ich die Lösung - so glaube ich - gefunden..

    wäre froh, wenn jemand bestätigen könnte, dass dies so gemeint war....

    // Program: threedim_array.C
    // iterate over a multidimensional array
    
    #include <iostream>
    
    int main()
    {
      int a[4][2][3] =
        {              // the 4 elements of a:
          {            // the 2 elements of a[0]:
    	{2, 4, 5}, // the three elements of a[0][0]
    	{4, 6, 7}  // the three elements of a[0][1]
          },
          {            // the 2 elements of a[1]:
    	{1, 5, 9}, // the three elements of a[1][0]
    	{4, 6, 1}  // the three elements of a[1][1]
          },
          {            // the 2 elements of a[2]:
    	{5, 9, 0}, // the three elements of a[2][0]
    	{1, 5, 3}  // the three elements of a[2][1]
          },
          {            // the 2 elements of a[3]:
    	{6, 7, 7}, // the three elements of a[3][0]
    	{7, 8, 5}  // the three elements of a[3][1]
          }
        };
    
    // Output: The above array using (damn) pointer arithmetic
    
    	// Firstly, we initalize a pointer which points 
             // to the first element of a. (That is a to say)
    
            int (*p)[2][3] = a;
    
    	// Secondly, we initialize a pointer which points
             // to the first element of the first element of a.
    	// In random acces we would talk \1: a[0][0]
    	int (*q)[3] = *p;
    
    	// Now it is getting difficult
             // Finally we initialize a pointer of the first element of
             // the element of the element of a.
    	// These is equal to the random access' a[0][0][0]
    
    	int (*r) = *q;
    
      for (int i=0,j,k; i<4; i++) {
        (p)++;  
           for(j=0;j<2;j++) {
            (q)++;  
             for(k=0;k<3;++k){
    	std::cout<<  *r << " ";              // Outputs every number one by one on one line (if only this std::cout is used)
            (r)++;
    		}
         }
       }
    
    	return 0;
    
    }
    

    😃

    Damian


Anmelden zum Antworten