Working with self-made dynamic array

Hello! I am trying to create a dynamic vector (as is in here; it's from an assignment - it has to respect this format. Bear in mind that names in here are not reserved words as this is translated from spanish).

There's a routine/function that's called vector_obtain, and must save in "value" the value from vector's position "pos". I am not told what data type does vector contain. vector_get_size does what it says on the tin.
Although it compiles, it doesn't work. This routine runs after "vector_save_on", a function that saves certain values on the vector I've to work with. What am I doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
bool vector_obtain (vector_t* vector, size_t pos, int* value)
{
    if ((pos>=0)&&(pos<vector_get_size(vector)))
    {
        value=(vector[pos].data);
        return true;
    }
    else return false;

bool vector_save_on(vector_t* vector, size_t pos, int value)
{
    if ((pos>=0)&&(pos<vector_get_size(vector)))
    {
        vector[sizeof(vector->data)*pos].data=value;
        return true;
    }
    else
       return false;
}


}


On the .h file, vector is defined as:

1
2
3
4
typedef struct vector {
    size_t size;
    int* data;
} vector_t;
value=(vector[pos].data);

perhaps it should read: *value=vector->data[pos];

vector[sizeof(vector->data)*pos].data=value;

should read: vector->data[pos] = value;
it's because the indexing operator (i.e. subscript operator) automatically takes account of the site of the data being pointed to.

Last edited on
Thank you very much! That did it!
Topic archived. No new replies allowed.