Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. stuck with a trivia

stuck with a trivia

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
21 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ _Superman_

    Smith# wrote:

    *pyp2 = first row. **pyp2 = first element of the first row.

    Wrong. I have tried to explain it in my previous post.

    «_Superman_»

    S Offline
    S Offline
    Smith
    wrote on last edited by
    #9

    Now when you say *pyp2 it is taking the first value of 0 which it thinks is another address. Yes. that's the address of the first pointer. I called it as 1st row. With **pyp2, I try to access the first element. in other words, *pyp2[0]. I agree that you cannot ask it go to the next pointer since it doen't know where first one ends. But what's the problem to access the "first-most" element? We have the address, what's the problem with taking it's address? Am I still missing something?

    :beer:

    _ 1 Reply Last reply
    0
    • S Smith

      Now when you say *pyp2 it is taking the first value of 0 which it thinks is another address. Yes. that's the address of the first pointer. I called it as 1st row. With **pyp2, I try to access the first element. in other words, *pyp2[0]. I agree that you cannot ask it go to the next pointer since it doen't know where first one ends. But what's the problem to access the "first-most" element? We have the address, what's the problem with taking it's address? Am I still missing something?

      :beer:

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #10

      To access the first element you simply do *pyp2. Try it.

      «_Superman_»

      S 1 Reply Last reply
      0
      • _ _Superman_

        To access the first element you simply do *pyp2. Try it.

        «_Superman_»

        S Offline
        S Offline
        Smith
        wrote on last edited by
        #11

        That will give me the address of the first element. Not the value.

        :beer:

        _ 1 Reply Last reply
        0
        • S Smith

          argh okay. accepted. is there way I can do it? I think it can be done by allocating a memory of that dimension dynamically and assigning the pointers one by one.bullshit. I meant assigning the pointers.

          :beer:

          modified on Wednesday, February 4, 2009 4:02 AM

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #12

          Try initialising pyp2 like this:

          int p1[2] = { 1, 2 };
          int p2[2] = { 1, 2 };
          int* pyp2[2] = { p1, p2 };

          pyp2 is of type (int*)[2], which is equivalent to int **.

          S 2 Replies Last reply
          0
          • S Smith

            That will give me the address of the first element. Not the value.

            :beer:

            _ Offline
            _ Offline
            _Superman_
            wrote on last edited by
            #13

            It is giving me the value. Try to understand that this is exactly why your code is crashing.

            «_Superman_»

            S 1 Reply Last reply
            0
            • S Stuart Dootson

              Try initialising pyp2 like this:

              int p1[2] = { 1, 2 };
              int p2[2] = { 1, 2 };
              int* pyp2[2] = { p1, p2 };

              pyp2 is of type (int*)[2], which is equivalent to int **.

              S Offline
              S Offline
              Smith
              wrote on last edited by
              #14

              hmm, so we cannot simply take a pointer and navigate through.. :sigh: .. the complier wants it to be informed about these..

              :beer:

              1 Reply Last reply
              0
              • S Stuart Dootson

                Try initialising pyp2 like this:

                int p1[2] = { 1, 2 };
                int p2[2] = { 1, 2 };
                int* pyp2[2] = { p1, p2 };

                pyp2 is of type (int*)[2], which is equivalent to int **.

                S Offline
                S Offline
                Smith
                wrote on last edited by
                #15

                I think I just ripped through mr.Smith tried this : int py2[2][2] = {10,11,12,13}; int* sptr = &py2[0][0]; int** dptr = &sptr; printf("\nAtlast:%d\n",**dptr); *dptr+=1; *dptr+=2; printf("\nAtlast:%d\n",**dptr); o/p:10,13 -Anderson. :cool:.

                :beer:

                1 Reply Last reply
                0
                • _ _Superman_

                  It is giving me the value. Try to understand that this is exactly why your code is crashing.

                  «_Superman_»

                  S Offline
                  S Offline
                  Smith
                  wrote on last edited by
                  #16

                  Are you using VC6.0 Advanced Auto-Deferencing Compiler ? lol just kidding.

                  :beer:

                  1 Reply Last reply
                  0
                  • S Smith

                    //Works int py[5] = {0,1,2,3,4}; int* pyp = (int*)py;//py printf("%d",pyp[2]); //Doesn't. show "2Dpointer\n"; int py2[2][2] = {0,1,2,3}; int **pyp2 = (int**)py2; printf("py2:%d",py2); printf("pyp2:%d",pyp2); printf("**pyp2[0][0]",pyp2[0][0]);//Crash printf("**pyp2%d",**pyp2);//Crash `why? :beer: `

                    E Offline
                    E Offline
                    Emilio Garavaglia
                    wrote on last edited by
                    #17

                    The key point is that arrays and pointers are NOT the same. An array can be converted into a pointer and pointer arithmetic (and indexing) looks the same as array subscripting, but they aren't. They coincide only for linear indexing (that's the case of monodimensional arrays) int **pyp2 is a "[pointer to a [pointer to [an int]]]" The memory layout it expects is like

                    int** ----> [int*, int*, int*, ...]
                    | | |
                    | | |
                    | | V
                    | V [int,int,...]
                    V [int,int,int...]
                    [int, int, ...]

                    While int py2[2][2] is an "[array of [array of [int]]]" The memory layout it expects is like

                    [[int,int],[int,int]]

                    That's completely different.

                    2 bugs found. > recompile ... 65534 bugs found. :doh:

                    S 1 Reply Last reply
                    0
                    • E Emilio Garavaglia

                      The key point is that arrays and pointers are NOT the same. An array can be converted into a pointer and pointer arithmetic (and indexing) looks the same as array subscripting, but they aren't. They coincide only for linear indexing (that's the case of monodimensional arrays) int **pyp2 is a "[pointer to a [pointer to [an int]]]" The memory layout it expects is like

                      int** ----> [int*, int*, int*, ...]
                      | | |
                      | | |
                      | | V
                      | V [int,int,...]
                      V [int,int,int...]
                      [int, int, ...]

                      While int py2[2][2] is an "[array of [array of [int]]]" The memory layout it expects is like

                      [[int,int],[int,int]]

                      That's completely different.

                      2 bugs found. > recompile ... 65534 bugs found. :doh:

                      S Offline
                      S Offline
                      Smith
                      wrote on last edited by
                      #18

                      As you know pointers & arrays both move consecutive. Then how come it be totally different? To some extent I can agree. Because the next pointer in the **ptr can be made to point to some other *p pointing somewhere. But here that's not the case.. it's array, quite consecutive and we are making our **ptr point to it. What do you think about the below approach ? getting the address of the first row pointer & getting the address of it and then deferencing. int py2[2][2] = {10,11,12,13}; int* sptr = &py2[0][0]; int** dptr = &sptr; printf("\nAtlast:%d\n",**dptr); *dptr+=1; *dptr+=2; printf("\nAtlast:%d\n",**dptr);

                      :beer:

                      E 2 Replies Last reply
                      0
                      • S Smith

                        As you know pointers & arrays both move consecutive. Then how come it be totally different? To some extent I can agree. Because the next pointer in the **ptr can be made to point to some other *p pointing somewhere. But here that's not the case.. it's array, quite consecutive and we are making our **ptr point to it. What do you think about the below approach ? getting the address of the first row pointer & getting the address of it and then deferencing. int py2[2][2] = {10,11,12,13}; int* sptr = &py2[0][0]; int** dptr = &sptr; printf("\nAtlast:%d\n",**dptr); *dptr+=1; *dptr+=2; printf("\nAtlast:%d\n",**dptr);

                        :beer:

                        E Offline
                        E Offline
                        Emilio Garavaglia
                        wrote on last edited by
                        #19

                        It seems you're confusing two well distinct concepts (don't worry: it takes about three years to me to get away from that confusion...). Let's reconsider your points:

                        Smith# wrote:

                        As you know pointers & arrays both move consecutive

                        No. Pointers "move" (in th sense they can receive another address as their own stored value). Array just "are". Array indexes "move" (in the sense they can be used to indicate other array cells).

                        Smith# wrote:

                        how come it be totally different

                        Because they ARE totally different. They simple have a "similar" external interface (same operators).If you don't get this point may be I was not able to explain the first point, but until you don't get it you cannot move away from your trivia. So loop over and over until you get it, find other sources than me (if you find my way to explain is not clear) but there is no way to move over until this crucial point is clear.

                        Smith# wrote:

                        Because the next pointer in the **ptr can be made to point to some other *p pointing somewhere. But here that's not the case.. it's array,

                        You know, but compiler doesn't. int** for the compiler is just 4 bytes of memory containing the address of an int* that is another 4 byte of memory, somewhere else, containing the address of an int. Whatever you think about this is irrelevant to the compiler. int[] is a consecutive bounce of integers at some place in memory. This place has an address. When you do

                        int a[2] = { 0,1 };
                        int* pa = a;

                        you assign the address of the array head to the pointer. When you do

                        *pa = 5;

                        the compiler assign 5 to the memory whose address is stored in "pa". This accidentally is a[0] (but it is not something the compiler is aware of). When you do

                        pa[1] = 6;

                        the compiler assign 6 to the memory whose address is given by pa+1*sizeof(int), that accidentally is a[1] (again you know, not the compiler: there is nothing in "pa" that makes the compiler aware of the fact that it is NOW pointing to an array and sometime later on to an individual integer and some later on to an integer inside a class or struct. The design of the [] operator appllied to poitner or to array makes this operation giving a same result just because for monidimensional array, the same calculation is done. Bidimensional arra

                        S 1 Reply Last reply
                        0
                        • S Smith

                          As you know pointers & arrays both move consecutive. Then how come it be totally different? To some extent I can agree. Because the next pointer in the **ptr can be made to point to some other *p pointing somewhere. But here that's not the case.. it's array, quite consecutive and we are making our **ptr point to it. What do you think about the below approach ? getting the address of the first row pointer & getting the address of it and then deferencing. int py2[2][2] = {10,11,12,13}; int* sptr = &py2[0][0]; int** dptr = &sptr; printf("\nAtlast:%d\n",**dptr); *dptr+=1; *dptr+=2; printf("\nAtlast:%d\n",**dptr);

                          :beer:

                          E Offline
                          E Offline
                          Emilio Garavaglia
                          wrote on last edited by
                          #20

                          Now, let's move a little bit on: If you agree with my previous post, consider this:

                          int a [2][2] = { 1,2,3,4 };
                          int[2]* ppa = a;

                          Do you get it? Instead of int**, I use int[2]*: a "pointer to an array of two integers". considering the int[][] as "array of 2 arrays of two integers". The type of int** is int* (that doesn't exist in memory since we store arrays), but the value of int[2]* is int[2] that's just one row of the matrix ( in our case {{1,2},{3,4}} Now ppa[1][1] is "take the address stored in ppa, add 1*sizeof(int[2]). This is the int[2] representing the second row. Now we're anymore dealing with a pointer, but with an array, whose cell "1" has the value "4".

                          2 bugs found. > recompile ... 65534 bugs found. :doh:

                          1 Reply Last reply
                          0
                          • E Emilio Garavaglia

                            It seems you're confusing two well distinct concepts (don't worry: it takes about three years to me to get away from that confusion...). Let's reconsider your points:

                            Smith# wrote:

                            As you know pointers & arrays both move consecutive

                            No. Pointers "move" (in th sense they can receive another address as their own stored value). Array just "are". Array indexes "move" (in the sense they can be used to indicate other array cells).

                            Smith# wrote:

                            how come it be totally different

                            Because they ARE totally different. They simple have a "similar" external interface (same operators).If you don't get this point may be I was not able to explain the first point, but until you don't get it you cannot move away from your trivia. So loop over and over until you get it, find other sources than me (if you find my way to explain is not clear) but there is no way to move over until this crucial point is clear.

                            Smith# wrote:

                            Because the next pointer in the **ptr can be made to point to some other *p pointing somewhere. But here that's not the case.. it's array,

                            You know, but compiler doesn't. int** for the compiler is just 4 bytes of memory containing the address of an int* that is another 4 byte of memory, somewhere else, containing the address of an int. Whatever you think about this is irrelevant to the compiler. int[] is a consecutive bounce of integers at some place in memory. This place has an address. When you do

                            int a[2] = { 0,1 };
                            int* pa = a;

                            you assign the address of the array head to the pointer. When you do

                            *pa = 5;

                            the compiler assign 5 to the memory whose address is stored in "pa". This accidentally is a[0] (but it is not something the compiler is aware of). When you do

                            pa[1] = 6;

                            the compiler assign 6 to the memory whose address is given by pa+1*sizeof(int), that accidentally is a[1] (again you know, not the compiler: there is nothing in "pa" that makes the compiler aware of the fact that it is NOW pointing to an array and sometime later on to an individual integer and some later on to an integer inside a class or struct. The design of the [] operator appllied to poitner or to array makes this operation giving a same result just because for monidimensional array, the same calculation is done. Bidimensional arra

                            S Offline
                            S Offline
                            Smith
                            wrote on last edited by
                            #21

                            Hey Sorry man, I'm just checking your reply. Before even reading it I felt I should thank you for your helping effort. I'll read it, and if I find it reasonable, I'll give up the weapons & surrender :). Anyway thanks a lot.

                            :beer:

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups