Search code examples
c++stdfillcomplex-numbersmemset

Can one use memset to fill an array of std::complex<float>s?


Specifically, I'm wondering if this line:

memset(cjzyp,(0,0),size_cjzy*sizeof(std::complex<float>));

will fill cjzyp, an array of complex<float>s, with the complex zero value ((0,0)).


Solution

  • std::memset takes as second parameter an int converted to an unsigned char, it won't work. Use std::fill instead

    http://www.cplusplus.com/reference/algorithm/fill/

    cjzyp = new std::complex<float>[100]
    std::fill(cjzyp, cjzyp + 100, std::complex<float>(-1.0, 0.0));
    delete [] cjzyp;