Skip to main content

[C++] One way to convert pointer of structure to vector

Of course, you can choose one method of C++ likes reinterpret which is described in below topic:
https://www.geeksforgeeks.org/reinterpret_cast-in-c-type-casting-operators/

However, it is easily to find out one big problem in here is:
reinterpret_cast is a very special and dangerous type of casting operator

In embedded system, this problem is not allowed. Then, we should convert it by manually.
The idea is, you push each structure to vector by passing number of structure to function which is used for converting.


 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
56
57
58
59
60
61
62
63
#include <iostream>
#include <list>
#include <vector>
#include <string.h>

using namespace std;

#define STR_NUM   3       //Number of structures
typedef struct
{
    char    text_id[16];
    char    name[16];
}st_A;

/* Show vector data after convert */
void vector_show( std::vector<st_A> pvIn)
{
    int size = pvIn.size();
    cout << "Number of structure in vector:" <<size << endl;
    
    for(int i = 0; i<size; i++)
    {
        cout << pvIn[i].text_id;
        cout << pvIn[i].name << endl;
    }
    
}

/* Convert from pointer of structure to vector with given number of structure */
void convert_func( st_A *psIn, int st_no)
{
    std::vector<st_A>    stTemp;
    
    for(int i = 0; i<st_no; i++)
    {
        stTemp.push_back(psIn[i]);
    }
    
    vector_show(stTemp);
}

/* It is main */
int main()
{
    //Declare structure with 10 members   
    st_A    stA_in[STR_NUM];
 
    //Set value for all member as example 
    strcpy(stA_in[0].text_id,"MrPres 0:");
    strcpy(stA_in[0].name,"Trumpy");
    
    strcpy(stA_in[1].text_id,"MrPres 1:");
    strcpy(stA_in[1].name,"Putty");
    
    strcpy(stA_in[2].text_id,"MrPres 2:");
    strcpy(stA_in[2].name,"Xiy");
    
 
 //Convert structure to vector
 convert_func(stA_in,STR_NUM);
 
 while (true);
}

Comments

Popular posts from this blog

[STM32] How to configure Timer 1, Channel 3 is PWM 1kHz, duty cycle 20% to control BLDC motor

 To configure Timer1 for a 1 kHz PWM signal with a 20% duty cycle on an STM32H7S3L8 microcontroller, follow these steps: 1. Understand the Timer Parameters PWM Frequency : 1 kHz → Period = 1 ms. Duty Cycle : 20% → ON time = 0.2 ms. The timer’s clock frequency is derived from the APB clock (e.g., TIMCLK). Let’s assume you know the APB clock frequency. The Timer prescaler and auto-reload register (ARR) define the PWM frequency. 2. Compute Timer Parameters Formula: PWM Frequency = Timer Clock (Prescaler + 1) * (ARR + 1) \text{PWM Frequency} = \frac{\text{Timer Clock}}{\text{(Prescaler + 1) * (ARR + 1)}} PWM Frequency = (Prescaler + 1) * (ARR + 1) Timer Clock ​ For 1 kHz PWM: A R R = Timer Clock PWM Frequency ∗ ( Prescaler + 1) − 1 ARR = \frac{\text{Timer Clock}}{\text{PWM Frequency} * (\text{Prescaler + 1)}} - 1 A RR = PWM Frequency ∗ ( Prescaler + 1) Timer Clock ​ −...

[CanadaLife] Example of answers for final test in Smart Service Ontario 2024

If you have failed the final exam in the first attempt. Congratulations, you are not odd. Here, take a look and grab some corrected answers for your next attempt.  Don't waste your money and time for more failure.  Good luck bros. Quick note: remember that, the system will change the bunch of questions after each attempt. Then your next questionnaire will be different compared with the first trial. ========================================== =============FINAL TEST=================== ========================================== TIP: Here is a list of questions you did not answer correctly. *Please note: You will only be able to view this list immediately following your test attempt. ========================================== Alcohol slows down the central nervous system and impacts how a person thinks, acts, and moves. This means alcohol is a: depressant ========================================== Alcohol is metabolized in the body at a set rate. For most people, that rate is: one...

[MyThought] Software design scalability, how to navigate conflicting perspectives

 Your team disagrees on software design scalability. How do you navigate conflicting perspectives? First of all, disagreements usually come from different viewpoints and personal biases. When people look at a problem, they each see it from their own perspective. To make sure everyone’s on the same page, it helps to list out the main issues before jumping into brainstorming. Remember, how scalable the software is will depend on both the project’s budget and timeline. Secondly, based on the project's budget and timeline, team members are encouraged to share their thoughts freely during the brainstorming session. Afterward, we'll gather the most outstanding ideas and compile them into a table of options. We'll then weigh these options based on several criteria, and finally, we'll select the top-ranked one. Moreover, I work in the field of embedded software. Whenever we design a new product, we must consider both the hardware and software aspects. Hardware is always the fir...