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. void pointer?

void pointer?

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structures
10 Posts 3 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.
  • S Offline
    S Offline
    sam_psycho
    wrote on last edited by
    #1

    How can I store address of integer array to void pointer and print array using void pointer?

    M 1 Reply Last reply
    0
    • S sam_psycho

      How can I store address of integer array to void pointer and print array using void pointer?

      M Offline
      M Offline
      molesworth
      wrote on last edited by
      #2

      sam_psycho wrote:

      How can I store address of integer array to void pointer

      int anArray\[1234\];
      
      void\* pointyThing = (void\*)(anArray);
      

      and print array using void pointer?

      I don't know of any built-in way to print arrays from a void*, but presumably you can write a function to do it...

      MyPrintArrayFunction(pointyThing);
      

      There are three kinds of people in the world - those who can count and those who can't...

      S 1 Reply Last reply
      0
      • M molesworth

        sam_psycho wrote:

        How can I store address of integer array to void pointer

        int anArray\[1234\];
        
        void\* pointyThing = (void\*)(anArray);
        

        and print array using void pointer?

        I don't know of any built-in way to print arrays from a void*, but presumably you can write a function to do it...

        MyPrintArrayFunction(pointyThing);
        

        There are three kinds of people in the world - those who can count and those who can't...

        S Offline
        S Offline
        sam_psycho
        wrote on last edited by
        #3

        To print array compiler giving error: I tried following code but It is not working,

        int arr[]={10,20,3};
        void *ptr=(void *)arr;
        printf("%d",*ptr); // getting error on this line

        So, please any suggestion to print array using void pointer, And most imp why compiler not allowing above code .. :confused:

        J 1 Reply Last reply
        0
        • S sam_psycho

          To print array compiler giving error: I tried following code but It is not working,

          int arr[]={10,20,3};
          void *ptr=(void *)arr;
          printf("%d",*ptr); // getting error on this line

          So, please any suggestion to print array using void pointer, And most imp why compiler not allowing above code .. :confused:

          J Offline
          J Offline
          Joe Woodbury
          wrote on last edited by
          #4

          printf expects you to pass a value as an argument. By definition you can't resolve a void pointer. Be happy; in the past the compiler wouldn't stop you from being stupid. (I actually slightly misspoke--the *ptr is simply an error. That said, newer compilers do some rudimentary "sanity" checking of printf arguments. I've never seen how far the compiler goes, but have seen it complain on occasion when I was being dumb.)

          modified on Thursday, July 30, 2009 2:35 PM

          S 1 Reply Last reply
          0
          • J Joe Woodbury

            printf expects you to pass a value as an argument. By definition you can't resolve a void pointer. Be happy; in the past the compiler wouldn't stop you from being stupid. (I actually slightly misspoke--the *ptr is simply an error. That said, newer compilers do some rudimentary "sanity" checking of printf arguments. I've never seen how far the compiler goes, but have seen it complain on occasion when I was being dumb.)

            modified on Thursday, July 30, 2009 2:35 PM

            S Offline
            S Offline
            sam_psycho
            wrote on last edited by
            #5

            Joe Woodbury wrote:

            printf expects you to pass a value as an argument. By definition you can't resolve a void pointer.

            Agree with you..:thumbsup:

            Joe Woodbury wrote:

            Be happy;

            Here too...:thumbsup: BUT,

            Joe Woodbury wrote:

            in the past the compiler wouldn't stop you from being stupid.

            Is this solution to the problem I asked? :sigh:

            J 1 Reply Last reply
            0
            • S sam_psycho

              Joe Woodbury wrote:

              printf expects you to pass a value as an argument. By definition you can't resolve a void pointer.

              Agree with you..:thumbsup:

              Joe Woodbury wrote:

              Be happy;

              Here too...:thumbsup: BUT,

              Joe Woodbury wrote:

              in the past the compiler wouldn't stop you from being stupid.

              Is this solution to the problem I asked? :sigh:

              J Offline
              J Offline
              Joe Woodbury
              wrote on last edited by
              #6

              There is no solution except to case the pointer to something that resolves to an integer or long value (which is dangerous of the void* isn't actually pointing to an integer or long.) The point is that you could do *(int*)ptr. (I actually slightly misspoke--the *ptr is simply an error. That said, newer compilers do some rudimentary "sanity" checking of printf arguments. I've never seen how far the compiler goes, but have seen it complain on occasion when I was being dumb.)

              S 1 Reply Last reply
              0
              • J Joe Woodbury

                There is no solution except to case the pointer to something that resolves to an integer or long value (which is dangerous of the void* isn't actually pointing to an integer or long.) The point is that you could do *(int*)ptr. (I actually slightly misspoke--the *ptr is simply an error. That said, newer compilers do some rudimentary "sanity" checking of printf arguments. I've never seen how far the compiler goes, but have seen it complain on occasion when I was being dumb.)

                S Offline
                S Offline
                sam_psycho
                wrote on last edited by
                #7

                Joe Woodbury wrote:

                The point is that you could do *(int*)ptr.

                is working fine, But I want to print array so I tried following code which is not working, So can you please make it workable.

                int arr[]={10,20,3,6};
                void *ptr=(void *)arr;
                int i=3;
                while(i--)
                {
                printf(" %d",(*(int *)ptr));
                (int *)ptr+=2;
                }

                J 1 Reply Last reply
                0
                • S sam_psycho

                  Joe Woodbury wrote:

                  The point is that you could do *(int*)ptr.

                  is working fine, But I want to print array so I tried following code which is not working, So can you please make it workable.

                  int arr[]={10,20,3,6};
                  void *ptr=(void *)arr;
                  int i=3;
                  while(i--)
                  {
                  printf(" %d",(*(int *)ptr));
                  (int *)ptr+=2;
                  }

                  J Offline
                  J Offline
                  Joe Woodbury
                  wrote on last edited by
                  #8

                  I don't understand why you are using a void*. It has no concept of size of what it's pointing to. You are removing type safety for no reason. That said, when you increment do arithmetic on a pointer, that arithmetic is done according to the size of the object pointed to by that pointer. In this case you are casting ptr to an (int*). Therefore, you only need to increment ptr by 1 to take you to the next position. (As an aside, (int*)ptr += 1; will not compile on many compilers--you would have to have ptr = (int*)ptr + 1; Two other points: You don't need to cast arr to a void*, that cast is automatic. You have an off by one error; you have four items in your array, but you only go through the loop three times. You simply shouldn't be using a void* in a case like this. It's an extremely bad idea. It would be better to use arr[] directly or if you must use a pointer, use an int*.

                  S 1 Reply Last reply
                  0
                  • J Joe Woodbury

                    I don't understand why you are using a void*. It has no concept of size of what it's pointing to. You are removing type safety for no reason. That said, when you increment do arithmetic on a pointer, that arithmetic is done according to the size of the object pointed to by that pointer. In this case you are casting ptr to an (int*). Therefore, you only need to increment ptr by 1 to take you to the next position. (As an aside, (int*)ptr += 1; will not compile on many compilers--you would have to have ptr = (int*)ptr + 1; Two other points: You don't need to cast arr to a void*, that cast is automatic. You have an off by one error; you have four items in your array, but you only go through the loop three times. You simply shouldn't be using a void* in a case like this. It's an extremely bad idea. It would be better to use arr[] directly or if you must use a pointer, use an int*.

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

                    Joe Woodbury wrote:

                    I don't understand why you are using a void*

                    Just I wanted to check that is is possible to print array using void*, You helped me a lot and I Could, thanks. Last one Q.:

                    ptr = (int*)ptr + 1;

                    In this case,I don't understand why you incremented by 1, size of int is 2, ptr is void* but we are converting it into int so suppose to be incremented by 2; but it fails and how above code works fine ?

                    J 1 Reply Last reply
                    0
                    • S sam_psycho

                      Joe Woodbury wrote:

                      I don't understand why you are using a void*

                      Just I wanted to check that is is possible to print array using void*, You helped me a lot and I Could, thanks. Last one Q.:

                      ptr = (int*)ptr + 1;

                      In this case,I don't understand why you incremented by 1, size of int is 2, ptr is void* but we are converting it into int so suppose to be incremented by 2; but it fails and how above code works fine ?

                      J Offline
                      J Offline
                      Joe Woodbury
                      wrote on last edited by
                      #10

                      The size of an int varies by compiler and platform. On 16-bit operating systems, it is 16 bits or 2 bytes long. On 32-bit operating systems, it is 32 bits or 4 bytes long. Incrementing a pointer will cause it the memory reference to increase by the size of the object it points to. One way to look at pointers is to understand that int* pArray is functionally equivalent to int array[4]. Likewise, assuming that pArray points to array, pArray[0] is the same as array[0]. Likewise pArray[1] is the same as array[1]. pArray[1] is the same as *(pArray + 1).

                      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