So I'm trying to create a simple array 2D inside a struct, and to read the values of some coordinates that are in a file.
At this moment I've got all inside functions and i get a segmentation fault when i try to get the coordinates on the 'get_coords' function. If I write all the exactly same code on a 'int main' instead of using functions it works.
code:
#include <stdio.h>
#include <stdlib.h>
// structure
typedef struct coordinates{
double **a;
} coord;
// counts the nmbr of points
int count(){
int i;
int a1, a2, a3;
FILE *fp;
fp = fopen("abc.txt", "r+");
while (fscanf(fp, "%d %d %d", &a1, &a2, &a3) != EOF){
i++;
if (feof(fp)){
break;
}
}
fclose(fp);
return(i);
}
// creates new structure with the right size of memory allocated
coord *newVector(size_t s){
coord *v;
int j;
v = malloc(sizeof(coord));
v->a = malloc(sizeof(double*)*3);
for (j=0; j<3; j++){
v->a[j] = malloc(sizeof(double)*s);
}
}
void get_coords(coord *points){
int i=0;
FILE *fp;
fp = fopen("abc.txt", "r+");
while (fscanf(fp, "%le %le %le", &points->a[i][0], &points->a[i][1], &points->a[i][2]) != EOF){
i++;
}
fclose(fp);
}
int main(){
int i = 0, j=0;
coord *points;
i = count();
points = newVector(i);
get_coords(points);
for (i=0; i<3; i++){
printf("%lf %lf %lf\n", points->a[i][0], points->a[i][1], points->a[i][2]);
}
}
abc.txt:
1 2 3
4 5 6
7 8 9
Thanks all for your help.
Cumps, Dylan.
PS: This is just a prototype for what i want.
In count()
, you are incrementing an uninitialized variable, invoking Undefined Behavior (UB).
Change this:
int count() {
int i;
to this:
int count() {
int i = 0;
newVector()
is not returning the dynamic allocated memory.
Change this:
coord *newVector(size_t s) {
...
}
to this:
coord *newVector(size_t s) {
...
return v;
}
After fixing these issues, you should see this output:
1.000000 2.000000 3.000000
4.000000 5.000000 6.000000
7.000000 8.000000 9.000000
Not the problem, but I would use %lf
as the format specifiers in fscanf()
, instead of %le
.
Moreover, in count()
, this return(i);
is the same as this return i;
. The parentheses are redundant.
TODO: Free the dynamically allocated memory (I assume that you skipped that part for providing the MCVE).
Pro-tip: Compile with warning flags enabled next time. They would have already found the issue for you in this case. In GCC for example, I would get this:
gsamaras@myPc:~$ gcc -Wall main.c
main.c: In function ‘main’:
main.c:51:28: warning: unused variable ‘j’ [-Wunused-variable]
int i = 0, j=0;
^
main.c: In function ‘newVector’:
main.c:37:13: warning: control reaches end of non-void function [-Wreturn-type]
}
^
where the last warning is the second point in my answer.
You forgot to return v
in newVector
function:
// creates new structure with the right size of memory allocated
coord *newVector(size_t s){
coord *v;
int j;
v = malloc(sizeof(coord));
v->a = malloc(sizeof(double*)*3);
for (j=0; j<3; j++){
v->a[j] = malloc(sizeof(double)*s);
}
return v;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.