0

I have two double pointer array, which I want to print

but somehow I am not able to do it..

double *noise_feature = new double[5];
double *basic_feature = new double[39];
noise_feature_extraction(sig, len, noise_feature);
basic_feature_extraction(sig, len, basic_feature);

cout << "\n";
printf("Noice features are");
for (auto i = noise_feature.begin(); i != noise_feature.end(); ++i) 
    cout << *i << " "; 
cout << "\n";

printf("Basic features are");
for (auto i = basic_feature.begin(); i != basic_feature.end(); ++i) 
    cout << *i << " "; 
cout << "\n";

This gives error like this

Pan_Tompkins.cpp:992:29: error: member reference base type 'double *' is not a structure or union
        for (auto i = noise_feature.begin(); i != noise_feature.end(); ++i) 
                      ~~~~~~~~~~~~~^~~~~~
Pan_Tompkins.cpp:992:57: error: member reference base type 'double *' is not a structure or union
        for (auto i = noise_feature.begin(); i != noise_feature.end(); ++i) 

I tried printing this way

printf("%g",noise_feature);
printf("%g",basic_feature);

This does not give error but also does not print anything.

How can I print this two double array to see their value?

1
  • You should use a std::vector<double> instead of allocating a double array. Commented Sep 30, 2020 at 11:42

3 Answers 3

2

You request a raw array on the heap and discard the info how many elements it has. Recall that

double *noise_feature = new double[5];

declares nothing but a pointer to double. The fact that you know it's a contiguous array of length 5 can be used in different way. Either you keep that magic number literal in your code;

for (auto value = noise_feature; value != noise_feature + 5; ++value) 
    //       not maintainable, but works: ^^^^^^^^^^^^^^^^^
    cout << *value << " ";

Or you go with a raw array on the stack. Here, the length is baked into the type and hence not lost. You could use a range-based for loop to iterate over it, for example.

double noise_features[5];

// ...

for (double value : noise_features)
    std::cout << value << ' ';

However, the preferred solution is using either std::vector if the size of your sequence is only known at runtime, or std::array if it's a fixed-length sequence.

Sign up to request clarification or add additional context in comments.

2 Comments

perhaps also worth mentioning that with the array one can use std::begin(noise_feature) and std::end(noise_features) (which is basically what the range based loop does under the hood)
Not basically, but exactly!
2

You declared two pointers

double *noise_feature = new double[5];
double *basic_feature = new double[39];

Pointers are scalar objects that do not have the member functions begin and end.

So you have to use the magic numbers 5 and 39 to output the allocated arrays pointed to by the pointers.

For example

cout << "\n";
printf("Noice features are");
for ( size_t i = 0; i < 5; ++i ) 
    cout << noise_feature[i] << " "; 
cout << "\n";

printf("Basic features are");
for ( size_t i = 0; i < 39; ++i ) 
    cout << basic_feature[i] << " "; 
cout << "\n";

The same can be done using pointers as for example

cout << "\n";
printf("Noice features are");
for ( auto p = noise_feature; p != noise_feature + 5; ++p ) 
    cout << *p << " "; 
cout << "\n";

printf("Basic features are");
for ( auto p = basic_feature; p != basic_feature + 39; ++p ) 
    cout << *p << " "; 
cout << "\n";

Pay attention to that instead of "manually" allocating dynamically arrays you could use the standard container std::vector as for example

#include <vector>

//...

std::vector<double> noise_feature( 5 );

//...

cout << "\n";
printf("Noice features are");
for ( const auto &item : noise_feature ) 
    cout << item << " "; 
cout << "\n";

//...

Comments

0

Arrays defined with statements like array_name[element_count] are not objects of any class!

Arrays are actually pointers to continuous memory. So, they don't have any methods and member functions. So your code will fail to compile. So instead of this:

for (auto i = noise_feature.begin(); i != noise_feature.end(); ++i)

Use this:

for (auto i = noise_feature; i != noise_feature + 5; ++i)

Or this:

for (auto i = std::begin(noise_feature); i != std::end(noise_feature); ++i)

Or you can store it in a std::vector<double> object. Also, why you used printf when C++ provides std::cout ?

2 Comments

"Arrays defined with statements like array_name[element_count] are not objects! Arrays are actually pointers to continuous memory" These statements are entirely wrong.
@VladfromMoscow Why? Yes, they are objects but not objects of a class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.