c - Accessing an array contained in a struct -


this first post, apologize if wrong or if bad question. current project, trying access array pointed struct. here's struct:

struct dynarr {     type *data;     /* pointer data array */     int size;       /* number of elements in array */     int capacity;   /* capacity of array */ }; 

i thought correct way change , access element be:

    v->data[0]=5.0;     v->data[1]=10.0;     printf("the value %d\n",v->data[0]);     printf("the value %d\n",v->data[1]); 

what i'm instead getting is:

the value 1123954688

the value 1123954688

would mind showing me i'm doing wrong? i've looked around on here unfortunately couldn't find helped.

edit: thank replies i've received far. memory allocated elsewhere in project. chose not include code project since stands it's around 400 lines, , didn't think helpful include of it, , lot sort through. if necessary so.

edit2: here's code slimmed down. main:

#include <stdio.h> #include <stdlib.h> #include "dynamicarray.h" int main(int argc, char* argv[]) {     dynarr *dyn;     dyn = createdynarr(2);     printf("\n\ntesting adddynarr...\n");     adddynarr(&dyn, 3);     adddynarr(&dyn, 4); } 

from implementation file:

struct dynarr {     type *data;     /* pointer data array */     int size;       /* number of elements in array */     int capacity;   /* capacity ofthe array */ };  dynarr* createdynarr(int cap) {     assert(cap > 0);     dynarr *r = (dynarr *)malloc(sizeof( dynarr));     assert(r != 0);     initdynarr(r,cap);     return r; } void initdynarr(dynarr *v, int capacity) {     assert(capacity > 0);     assert(v!= 0);     v->data = (type *) malloc(sizeof(type) * capacity);     assert(v->data != 0);     v->size = 0;     v->capacity = capacity; } void adddynarr(dynarr *v, type val) {     /* fixme: write function */     v->data[0]=5.0;     v->data[1]=10.0;     printf("the value %d\n",v->data[0]);     printf("the value %d\n",v->data[1]);  } 

from header file:

#include<math.h>  #ifndef dynamic_array_included #define dynamic_array_included 1   # ifndef type # define type     double  # define type_size sizeof(double) # endif  # ifndef eq # define eq(a, b) (fabs(a - b) < 10e-7) # endif  typedef struct dynarr dynarr;  dynarr *createdynarr(int cap); void adddynarr(dynarr *v, type val); 

i believe that's of it. know values i'm passing function not values being assigned, i'm trying figure out how access array though stuct , pointer

thank time , help. issue ended being using %d instead of %f in printf() statements.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -