Getting the data from a void* in a struct

This is a discussion on Getting the data from a void* in a struct within the C++ Programming forums, part of the General Programming Boards category; So there's this c++ struct that has a *void variable that I want to get the data from and I'm ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    5

    Question Getting the data from a void* in a struct

    So there's this c++ struct that has a *void variable that I want to get the data from and I'm not sure how to do it. Here's the struct def (FYI its from the CoreAudio framework):


    Code:
    struct AudioBuffer
    {
        UInt32  mNumberChannels;
        UInt32  mDataByteSize;
        void*   mData;
    };
    typedef struct AudioBuffer  AudioBuffer;
    Thanks in advance!
    GW

  2. #2
    Registered User Inanna's Avatar
    Join Date
    May 2011
    Posts
    69
    You have to cast the pointer to a usable pointer type before it can be used. What does mData represent?

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    5
    It should be a float ranging from -1. to 1. I know I cant change the struct because its from the apple coreaudio framework. could I possibly create my own struct of the AudioBuffer type?

    GW

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    A float? That's not what Apple seems to think. If it really is a float, for whatever reason, then you'll have to cast it to (float*) and then dereference it.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    5
    Well its data type is variable depending on a bunch of other stuff that you setup. The way I have done so it should be a float. So here's where my knowledge of pointers lacks, here's what I think will do the job. Please correct me if I'm wrong.

    Code:
    AudioBufferList tempList;
    // code to read data into the buffer list
    
    Float32 *frame = (Float32*) &tempList.mBuffers[0].mData;
    NSLog(@"value: %f", *frame);
    I know that can't be right but its the only way the complier will build it without errors.
    Thanks a bunch
    GW

  6. #6
    Registered User Inanna's Avatar
    Join Date
    May 2011
    Posts
    69
    mData is already a pointer. The address operator makes it a double pointer, and that is wrong.
    Code:
    Float32* frame = (Float32*)tempList.mBuffers[0].mData;
    
    NSLog(@"value: %f", *frame);

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    5
    When I do that Xcode has an error:
    "Initializing 'Float32' (aka float) with an expression of incompatible type 'Float32* (aka float*)"

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That's because you forgot the * after "Float32".

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    5
    Ohhhhh..... there it is! Thanks a bunch!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 03-08-2010, 11:30 AM
  2. Array of structs -> struct to void
    By v1n1c1u5 in forum C Programming
    Replies: 1
    Last Post: 12-14-2009, 07:23 AM
  3. struct holding data inside a linked list struct
    By icestorm in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 01:49 PM
  4. What is this mean? static void *name (void *data)
    By beyonddc in forum C++ Programming
    Replies: 14
    Last Post: 05-05-2004, 04:06 PM
  5. '=' : cannot convert from 'void *' to 'struct HBRUSH__ *'
    By Bajanine in forum Windows Programming
    Replies: 9
    Last Post: 10-14-2002, 08:54 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