Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error while printing structure array contents

Below code is not printing after 1st element of array. After printing first structure content the subsequent values are containing garbage values

#include <iostream>

using namespace std;

#define SMALL_STRING_LEN 20
#define TINY_STRING_LEN 10

enum data_item_type_t {
    TYPE_FLOAT,
    TYPE_INT,
    TYPE_UINT
};

enum agent_type_t {
    LOCATION,
};

enum sensor_type_t {
    NOT_APPLICABLE,
};

typedef struct data_holder_conf {
    int16_t data_id;
    char description[SMALL_STRING_LEN];
    int16_t num_items;
    char unit[TINY_STRING_LEN];
    data_item_type_t data_type;
    agent_type_t agent;
    sensor_type_t sensor;
    /* pull frequency in milliseconds*/
    uint32_t pull_freq;
} data_holder_conf_t;

data_holder_conf_t data_holder_conf_arr[] = {
    { 101, "altitude",  1, "metres", TYPE_FLOAT, LOCATION, NOT_APPLICABLE, 100 },
    { 102, "latitude",  1, "metres", TYPE_FLOAT, LOCATION, NOT_APPLICABLE, 100 },
    { 103, "longitude", 1, "metres", TYPE_FLOAT, LOCATION, NOT_APPLICABLE, 100 },
    { 104, "velocity",  1, "kmph",   TYPE_FLOAT, LOCATION, NOT_APPLICABLE, 100 }
};

int main() {
    data_holder_conf_t *ptrLocal = (data_holder_conf_t *)malloc(4 * sizeof(data_holder_conf_t));
    memcpy(ptrLocal, data_holder_conf_arr, 4 * sizeof(data_holder_conf_t));
    cout << "..........................................\n";
    data_holder_conf_t *ptrTemp;
    for (int i = 0; i < 4; i++) {
        ptrTemp = (i * sizeof(data_holder_conf_t)) + ptrLocal;
        cout << " data_id = " << ptrTemp->data_id << endl;
        cout << " description = " << ptrTemp->description << endl;
        cout << " num_items = " << ptrTemp->num_items << endl;
        cout << " unit = " << ptrTemp->unit << endl;
        cout << " data_type =" << ptrTemp->data_type << endl;
        cout << " agent = " << ptrTemp->agent << endl;
        cout << " sensor = " << ptrTemp->sensor << endl;
        cout << " pull_freq = " << ptrTemp->pull_freq << endl;
    }
    free(ptrLocal);
}

I think there is problem while calculating the ptrTemp value.

But I am not able to figure out what is the mistake.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I think there is problem while calculating the ptrTemp value.

You presume right: your pointer arithmetic is incorrect: to get a pointer to the i-th entry, just use:

    ptrTemp = ptrLocal + i;

or

    ptrTemp = &ptrLocal[i];
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading