Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C Question

  • 02-11-2005 10:26am
    #1
    Closed Accounts Posts: 1,541 ✭✭✭


    Hi,

    New to C. Trying to understand someone's elses code for transition to a another language.

    Can anyone answer this:

    float * data; is defined in a header file.

    In another part :
    for (i = 0; i < n + DATAPADDING_MSECS  * (Fs / 1000); i++) {
            align_filtered [i] = info-> data [i];
        }
    

    How can they rad from an 'array'data like that. Can anyone explain this.?

    Data is defined in a structer and info is a pointer to data.

    Thanks for any advice.


Comments

  • Registered Users, Registered Users 2 Posts: 7,278 ✭✭✭kenmc


    Sounds like data is a member of a structure, of which "info" is a variable of that structure... i.e.
    typedef some_Struct
    {
    float* data;
    int some_other_stuff;
    };

    some_Struct *info = (some_Struct*)malloc(sizeof(some_Struct));

    then you can get at the data via a pointer ; info->data


  • Closed Accounts Posts: 1,541 ✭✭✭finnpark


    Thanks for info.

    Should he not allocate memory to data? Why not?

    Does the following line allow Data to be accessed as an array?:
    sinfo-> data =(float *) safe_malloc( (sinfo-> h + k  * u) * sizeof(float) );
    
    where h=62347,k=320, u=8
    


    If so what size is data?

    Also, does the following line allow read_ptr to store information in data?:
    read_ptr = sinfo-> data;
    

    Thanks for any help and sorry for my ignorance - Im reasonably new to C.


  • Registered Users, Registered Users 2 Posts: 7,278 ✭✭✭kenmc


    Yes he should, unless somewhere else he assigns it - e.g.
    float MyArray[100];
    then later on....
    info->data = MyArray;

    no the
    sinfo-> data =(float *) safe_malloc( (sinfo-> h + k * u) * sizeof(float) );
    line allocates the memory for data. the brackets are a bit crap, but I suspect that he;s trying to do
    (h+k)*u rather than h + (k*u). so you can work out the size of the array as either 501336 or 64907 elements (of size float which is system dependent - could be 4 bytes for windows - not sure - do
    printf("%d\n", sizeof(float));
    in a c file to find out.
    to find out how much he's allocated, you can do
    printf("%d\n", sizeof(sinfo->data)/sizeof(float));

    Now that it's allocated, you can access it as an array.

    read_ptr = sinfo-> data;
    this assigns a pointer to point to the first element in the data array. you could then read values from (or indeed write values to, but that goes against the variable name) this array via
    read_ptr
    or sinfo->data
    (or indeed *(read_ptr+i( or *(sinfo->data+i), since arrays are pointers anyway, but don't worry about these last two too much.
    Hope this helps a bit.
    K


  • Closed Accounts Posts: 1,541 ✭✭✭finnpark


    Yes this is helpful, thanks.

    He hasn't commented 1 line even in the code:mad: so I have to work everything out.

    Thanks Again,
    DC.


  • Registered Users, Registered Users 2 Posts: 7,278 ✭✭✭kenmc


    Don't forget the ancient philosophy :
    "If it was hard to write, it should be hard to read too" :)
    hence the lack of comments!
    glad it helped
    K


  • Advertisement
Advertisement