beginner in C: void pointer refers to an array of double. How do I read the data?

This is a discussion on beginner in C: void pointer refers to an array of double. How do I read the data? within the C Programming forums, part of the General Programming Boards category; Hi I am using code from the hardware manufacturer of this chip in robotics I bought, but I'm a beginner ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    2

    beginner in C: void pointer refers to an array of double. How do I read the data?

    Hi

    I am using code from the hardware manufacturer of this chip in robotics I bought, but I'm a beginner in C.

    The code is event driven. After x millisecond an eventhandler is called. This event handler receives a "void *userptr" variable for the user to do whatever they want with it.

    In the main() I have declared "double dataset1[10]" and I pass the address on to the eventhandler.

    In the eventhandler I need to access the data in dataset1 but I can't get the code right. I believe a typecasting is required here but can't get it right. Can someone tell me how I can access the data in the dataset1 array using the void *usertpr?

    for example how do I retrieve the value of dataset1[1] ?

    Thanks

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,429
    In C, a void pointer is implicitly convertible to any other pointer type, so all you should need in the function is
    Code:
      double *data = userptr;
         /*  data[i] can now be accessed for i = 0 to 9 */
    If you insist on an explicit type conversion (aka typecast) then the syntax is
    Code:
       double *data = (double *)userptr;
    If you actually need such a explicit conversion to stop you compiler complaining, then you are actually using a C++ compiler. Note that such an explicit conversion is considered poor form in C.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    2
    works great. Thx.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 02-10-2012, 04:42 AM
  2. Read in Fortran output double precision data
    By kingfish in forum C Programming
    Replies: 4
    Last Post: 01-06-2011, 09:31 AM
  3. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  4. Read size of data array when reading .txt data
    By Taquito in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 01:52 AM
  5. read from file and put into double array ...
    By rox in forum C Programming
    Replies: 5
    Last Post: 12-02-2003, 01:32 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21