Like Tree1Likes
  • 1 Post By laserlight

Inserting bytes into float array

This is a discussion on Inserting bytes into float array within the C++ Programming forums, part of the General Programming Boards category; Hi, I welcome myself to my first visit to the form - hope it will justify my expectations. Here is ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    2

    Inserting bytes into float array

    Hi,
    I welcome myself to my first visit to the form - hope it will justify my expectations.

    Here is my first question.
    I declare a float array (takes 4 bytes, 32 bits).
    Most of my array is filled with float numbers which is fine.
    I wish to fill some cells with several addressable bytes each.

    I have tried (example):

    array[x] = byte << 16; which works OK, but overwrites other bytes.

    array[x] |= byte << 16; Get lvalue error.

    tried (uint32) array[x] |= byte << 16; Get lvalue error.

    I realize the array is a pointer, so I tried even dereferencing it (&), but for no avail.

    What is the right way to load separate bytes into a floating point array cell, and be able to read them back one by one?

    Thanks
    samtal

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Welcome

    You can create a char* pointer to the array and access the bytes that way.

    Code:
    char * c_arr = array;
    c_arr[5] = 0; //writes the 6th byte, in the 2nd floating point num
    c_arr[x*4+2] = 0; // access second halfword of element x
    Beware of Endianness! -- be sure that you're writing the bytes you wanted to.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,606
    You may have to do

    char * c_arr = reinterpret_cast<char*>(array);

    And btw, an array is not a pointer and you don't dereference a pointer with & (it's *).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    2
    Thanks for your support.
    Unfortunately I could not get it to work.

    The line
    char * c_arr = (char*)array; (array is my floating point array)
    compiles OK, but when I load data into its locations

    c_arr[5] = 0xA; or *(c_arr+x) = y;

    It builds a new array c-arr[], where the data is loacted.
    This is not what I was looking for.
    I want to load byte or half word into cells in my floating point array.
    Any other idea ?
    My basic thinking was that I can simply write anything into any of the array addresses (&array+offset), but it seems as if once the array is defined as floating point, it will refuse any casting.
    I even tried casting it into a uint32 which is its basic size, but it only generates casting errors.

    Can anyone come up with a tested working solution?
    I'd appreciate it very much becasue I need it and because I need to understand how to do it and why it does not work the way I do.


    and, Elaysia you were right, I should have wrote 'referencing' (&), not 'dereferencing' (*).
    but, Array address IS a pointer.
    "array[i]" equals "*(array + i)".

    samtal
    Last edited by samtal; 11-27-2011 at 02:40 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,727
    Quote Originally Posted by samtal
    The line
    char * c_arr = (char*)array; (array is my floating point array)
    compiles OK, but when I load data into its locations

    c_arr[5] = 0xA; or *(c_arr+x) = y;

    It builds a new array c-arr[], where the data is loacted.
    No, c_arr is a pointer, not an array, so "builds a new array" is nonsense. It overwrites the data of the array named array (assuming you don't go out of bounds).

    Quote Originally Posted by samtal
    but, Array address IS a pointer.
    "array[i]" equals "*(array + i)".
    Yeah, but that does not mean that an array is a pointer.

    Why do you want to do this, anyway?
    Elysia likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fputc inserting extra bytes
    By dylan.scott in forum C Programming
    Replies: 2
    Last Post: 09-05-2009, 04:52 PM
  2. Float, Double, to bytes?
    By Florian in forum C++ Programming
    Replies: 5
    Last Post: 05-25-2009, 10:15 AM
  3. Printing a float's bytes...
    By Mr_Miguel in forum C Programming
    Replies: 3
    Last Post: 01-07-2008, 08:06 AM
  4. inserting values in an array
    By fairyjerry14 in forum C Programming
    Replies: 1
    Last Post: 10-12-2007, 01:15 AM
  5. Inserting elements into array
    By sean in forum C++ Programming
    Replies: 4
    Last Post: 07-07-2003, 04:56 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