C programming 2D arrays of chars and reading them in

So I have this portion of code I'm working on (non academic) and I'm having trouble reading in the 2D array of characters. It segfaults at the portion of code that reads 'temp = atof(stringArray[0][0]);' I chose 3 and 4 as just arbitrary points, I'll eventually use iterators and change it into a for loop. Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <stdio.h>
#include <stdlib.h>
#include "IONmodel.h"
#include <string.h>


   IONmodel* ION_loadModel(const char* filename, char **model){

	int size = 0;
	FILE *file = fopen(filename, "r");

	if (file == NULL)
	{
	    printf("ERROR OPENING FILE!");
		return -1; // -1 means file opening fail
	}

	fseek(file, 0, SEEK_END);
	size = ftell(file);
        printf("Size of file: %d",size);
	fseek(file, 0, SEEK_SET);


     char line[size];

    //string array of rows and columns
    char stringArray[70000][51];
    int i = 0;
    int text = 0;
	while( fgets(line, sizeof(line), file) != NULL)
	{

	   printf(line);

            for(text = 0; text <= sizeof(line); text++){
                stringArray[i][text] = line[text];

            }
            i++;
	}


	fclose(file);

//future ints to read in as the file 'seperates' into sections
    int  v[i][51];
    int vt[i][51];
    int f[i][51];

//temporary float
    float temp = 0.000000;

//iterators for the 2D array to split into sections
    int j =0;
    int k =0;



    for(j = 0; j < i; j++){
            if(strncmp (stringArray[j], "v  ",3) == 0){
                printf("Entered v portion:  ");

              //want to use sizeof for proper measurement
              //  for(k = 0; k < 3; k++ ){

              //SEGFAULT HERE
                temp = atof(stringArray[0][0]);
                    printf(" %g", temp);

           //     }

               }

            else if(strncmp (stringArray[j], "vt  ",3) == 0){
                printf("Entered vt portion:  ");

                }

            else if(strncmp (stringArray[j], "f  ",2) == 0){
                printf("Entered f portion:  ");

                }
    }

        //just return something for now
	return size;
    }


//}


int main()
{
    ION_loadModel("AK74.obj", 'model');
    return 0;
}


I understand the code is messy, it's slowly evolving and getting more efficient.
So I want to understand why I'm getting a segfault.

I know the stringArrray reads in perfectly for row and column. When I access it just by stringArray[i] it works just fine, however when I access it by stringArray[i][j] it segfaults instantly. Why is this happening?

I just put [0][0] down because I know it is reading the file in and at column 0, row 0, I know there is a number that was read in which was '2.0345' at that point, so in that test poriton with stringArray[0][0] I should be getting a 2.0345 back, but it just segfaults.
Last edited on
I just realized that if I dereference the 2D array, it works!
Now why is this.
Topic archived. No new replies allowed.