Passing array to function

Hi guys,
How I can pass array of one type(lets say uint8_t) to function as parameter with different type(lets say uint64_t)?
I'm trying to make function that will split array of values into bytes(with no extra bytes) no matter what type the array is. Is it possible to make one function for that job?
Last edited on
I'm trying to make a function that will split an array of values into bytes

I don't think I understand. Can you give a representative example?

Here's what I done for now.
But it gives me weird numbers from data parameter.
I know I can't just change from uint8_t to uint64_t
Ignore address parameter.
"data" is parameter for array of data, "len" is length of array and "bPD" is how much bytes holds each value.
I tought uint64_t will care of every smaller data type, but it doesn't work that way.

In another example will be
1
2
    uint32_t array[10] = { 255000, 2000, 30000, .... };
    writePage(0, (uint64_t*)array, 10, 4); // 10 "indexes" and 4 bytes per value 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void writePage(uint16_t address, uint64_t *data, uint8_t const len, uint8_t const bPD)
{
    uint8_t pageBuffer[32];
    uint8_t bytes2Write = len * bPD;

    for (uint8_t dataIndex = 0; dataIndex < len; dataIndex++)
    {
        std::cout << data[dataIndex] << std::endl;
        for (uint8_t i = 0; i < bPD; i++)
        {
            pageBuffer[i + (dataIndex * bPD)] = ((data[dataIndex] >> 8 * i) & 0xFF); // HERE IT SPLITS VALUE INTO BYTES
            std::cout << int(pageBuffer[i + (dataIndex * bPD)]) << std::endl << std::endl;
        }
    }
}

int main()
{
    uint8_t array[3] = { 255, 20, 30 };
    writePage(0, (uint64_t*)array, 3, 1);

    getch();
    return 0;
}
Last edited on
Looks like you're over-complicating the problem.

What's wrong with
1
2
3
4
void write_page(std::uint8_t const* buf, std::size_t sz) 
{
    std::copy_n(buf, sz, std::ostream_iterator<int>{std::cout, "\n"});
}


Live demo:
http://coliru.stacked-crooked.com/a/0531190568fe27dc

What you're calling bPD is generally named stride, the number of bytes between data points. It generally appears to handle interleaved data. Since the data you're accessing has no semantics (you're inspecting the data as a sequence of bytes - i.e., the data's object representation), there's no point in providing a stride. Only the total size and a pointer is required.
Last edited on
> I'm trying to make function that will split array of values into bytes(with no extra bytes)
> no matter what type the array is. Is it possible to make one function for that job?

Make it a function template.
If we also want to read the original values back, it would need to be an array of a TriviallyCopyable type.

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <type_traits>
#include <cstring>
#include <iomanip>

template< typename T >
void write_bytes( const T* data, std::size_t n, void* dest ) {

    static_assert( std::is_trivially_copyable<T>::value ) ;

    const auto nbytes = sizeof(T)*n ;

    std::memcpy( dest, data, nbytes ) ;

    const unsigned char* pb = reinterpret_cast<const unsigned char*>(data) ;
    std::cout << "bytes: " ;
    for( std::size_t i = 0 ; i < nbytes ; ++i ) std::cout << std::setw(4) << int( pb[i] ) ;
    std::cout << '\n' ;
}

template< typename T >
void read_bytes( const void* srce, T* data, std::size_t n ) {

    static_assert( std::is_trivially_copyable<T>::value ) ;

    const auto nbytes = sizeof(T)*n ;

    std::memcpy( data, srce, nbytes ) ;
}

template< typename T, std::size_t N >
void write_bytes( const T (&data)[N], void* dest ) { write_bytes( data, N, dest ) ; }

template< typename T, std::size_t N >
void read_bytes( const void* srce, T (&data)[N] ) { read_bytes( srce, data, N ) ; }

int main() {

    unsigned char buffer[1024] ;

    int array[] { 1, 2, 3, 4, 5, 6 } ;
    for( int v : array ) std::cout << v << ' ' ; // 1 2 3 4 5 6
    std::cout << '\n' ;

    static_assert( sizeof(buffer) >= sizeof(array) ) ;
    write_bytes( array, buffer ) ; // save value representation into buffer

    for( int& v : array ) v *= 23 ; // modify array
    for( int v : array ) std::cout << v << ' ' ; // 23 46 69 92 115 138
    std::cout << '\n' ;

    read_bytes( buffer, array ) ; // restore saved value
    for( int v : array ) std::cout << v << ' ' ; // 1 2 3 4 5 6
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/0fa5d1daa831c95e
Topic archived. No new replies allowed.