Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

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,299 ✭✭✭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,299 ✭✭✭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,299 ✭✭✭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