I am getting a segfault when running this, somehow because I'm using double* d wrong in vector_make_from.
I need to use -> and pointers somehow I guess but don't get how.
Should vector_make and *from return a pointer to a vector nit a vector?
______________________________
#include <math.h>
#include <stdlib.h>
#include "vector.h"
#include <stdio.h>
vector vector_make(int size)
{
vector vr;// = malloc(sizeof(vector));
vr.size = size;
vr.v = malloc(size * sizeof(double));
return vr;
}
vector vector_make_from(int vsize, double* d)
{
vector vr;// = malloc(sizeof(vector));
vr.size = vsize;
int i;
for(i=0; i<vsize; i++) {
vr.v = d;
}
return vr;
}
typedef struct
{
int size;
double* v;
} vector;
vector vector_make(int size);
vector vector_make_from(int size, double* d);
#include <stdio.h>
#include "vector.h"
#include <stdlib.h>
int main(void)
{
vector a = vector_make(3);
vector b = vector_make(3);
double z[3] = {5,1,3};
vector c = vector_make_from(3,z);
printf("\nWhere is jsjsjerror?");
vector_set(c,2,4);
printf("\nz[2] is:%f\n", z[2]);
double x[3] = {1,2,3};
double y[3] = {2,3,5};
a.v = x;
b.v = y;
return 0;
}
I need to use -> and pointers somehow I guess but don't get how.
Should vector_make and *from return a pointer to a vector nit a vector?
______________________________
#include <math.h>
#include <stdlib.h>
#include "vector.h"
#include <stdio.h>
vector vector_make(int size)
{
vector vr;// = malloc(sizeof(vector));
vr.size = size;
vr.v = malloc(size * sizeof(double));
return vr;
}
vector vector_make_from(int vsize, double* d)
{
vector vr;// = malloc(sizeof(vector));
vr.size = vsize;
int i;
for(i=0; i<vsize; i++) {
vr.v = d;
}
return vr;
}
typedef struct
{
int size;
double* v;
} vector;
vector vector_make(int size);
vector vector_make_from(int size, double* d);
#include <stdio.h>
#include "vector.h"
#include <stdlib.h>
int main(void)
{
vector a = vector_make(3);
vector b = vector_make(3);
double z[3] = {5,1,3};
vector c = vector_make_from(3,z);
printf("\nWhere is jsjsjerror?");
vector_set(c,2,4);
printf("\nz[2] is:%f\n", z[2]);
double x[3] = {1,2,3};
double y[3] = {2,3,5};
a.v = x;
b.v = y;
return 0;
}