understanding array declaration

Hello,
I was hoping I can declare a variable sized array but the debugger gives an error which I can't understand how to fix...
 
  int vindex[vn];

the variable vn is already declared and given a value.


I recently started coding in c++ and I am self taught in c++ and "AutoIt3" languages and in autoit declaring such array is acceptable.
Could you post the error (word by word) from the debugger? or post your code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main()
{
    int list[] = { 1,2,3,4,5,6,7,8,9,10 }; //Declare and initialize the array.

    for (auto value : list) // Use the range for (C++11) to iterate thru the array.
	   std::cout << value << "\t";
 
    std::cout << std::endl;

    for (int count = 0; count < 10; count++) // Or use the for loop to iterate thru the array.
	   std::cout << list[count] << "\t";

    return 0;
}
Last edited on
C++ doesn't have variable-length arrays. You can use std::vector instead.

 
std::vector<int> vindex(vn);

http://www.cplusplus.com/reference/vector/vector/
The idea is to read a binary file and depending on how many integers there are (count is mentioned in the file's header) to have an array with that size
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <cstdlib>
#include <iostream>
#include <array>

using namespace std;


int main(int argc, char *argv[])
{
    FILE *model,*skin,*output;
    float pos[3];
    unsigned int vn=0,vofs=0, triendofs=0, trin=0, triofs=0, c=0, vindexofs, namel, nameofs;
    char name;
    uint16_t tripos[3];
    
    printf("\n");
    if(argc < 4)
    {
            printf("Error. Expected 3 filepath arguments.\n");
            return 1;
    }
    output = fopen(argv[3],"r");
    if(output != NULL)
    {
           printf("Output file exists. Overwrite? Y/N:");
           while (c != 121)
           {
                 c = getchar();
                 if (c == 110)
                 {
                     printf("\nOk, (sad)\n");
                     fclose(output);
                     return 1; 
                 }
           }       
    }
    fclose(output);
    model = fopen(argv[1],"rb");
    if(model == NULL)
    {
            printf("Unable to open model file.\n");
            return 1;
    }
    skin = fopen(argv[2],"rb");
    if(skin == NULL)
    {
            printf("Unable to open skin file.\n");
            fclose(skin);
            return 1;
    }
    output = fopen(argv[3],"wt");
    if(output == NULL)
    {
           printf("Unable to create output file.\n");
           fclose(model);
           fclose(skin);
           return 1;   
    }
    if(fseek(model,0x8,SEEK_SET))
    {
           printf("Seek error: model file too short.\n");
           fclose(model);
           fclose(skin);
           fclose(output);
           return 1;  
    }
    if(!fread(&namel,4,1,model))
    {
         printf("Error: unable to read model name length.\n");
         fclose(model);
         fclose(skin);
         fclose(output);
         return 1;
    }          
    if(!fread(&nameofs,4,1,model))
    {
         printf("Error: unable to read model name offset.\n");
         fclose(model);
         fclose(skin);
         fclose(output);
         return 1;
    }
    if(fseek(model,nameofs,SEEK_SET))
    {
           printf("Seek error: model file too short.\n");
           fclose(model);
           fclose(skin);
           fclose(output);
           return 1;  
    }
    if(!fread(&name,namel,1,model))
    {
          printf("Error: unable to read name char %d.\n", namel);
          fclose(model);
          fclose(skin);
          fclose(output);
          return 1;
    }
    printf ("%s\n", name);      
    if(fseek(model,0x3C,SEEK_SET))
    {
           printf("Seek error: model file too short.\n");
           fclose(model);
           fclose(skin);
           fclose(output);
           return 1;  
    }
    if(!fread(&vn,4,1,model))
    {
         printf("Error: unable to read vert count.\n");
         fclose(model);
         fclose(skin);
         fclose(output);
         return 1;
    }
    int vindex[vn];
    vindex[0] = vn;
    //here is where I'll add the code to read the indexes.             
    if(!fread(&vofs,4,1,model))
    {
         printf("Error: unable to read vert offset.\n");
         fclose(model);
         fclose(skin);
         fclose(output);
         return 1;
    }
    
    fprintf(output, "#test file\ng default\n");
    if(fseek(model,vofs,SEEK_SET))
    {
           printf("Seek error: model file too short.\n");
           fclose(model);
           fclose(skin);
           fclose(output);
           return 1;  
    }
    while(vn -- )
    {
           if(fread(&pos,4,3,model) == 3)
           {
                 fprintf(output,"v %f %f %f\n",pos[0],pos[1],pos[2]);    
           }
           else 
           {
                 printf("Error: unable to read/write vert position number %d.\n", vn);
                 break;
           }
           if(fseek(model,36,SEEK_CUR))
           {
                 printf("EOF or error.\n");
                 break;
           }
    }
    vn = vindex[0];
    
    if(fseek(skin,vindexofs,SEEK_SET))
    {
           printf("Seek error: model file too short.\n");
           fclose(model);
           fclose(skin);
           fclose(output);
           return 1;  
    }
    while(vn -- )
    {
           if(fread(&pos,4,3,model) == 3)
           {
                 fprintf(output,"v %f %f %f\n",pos[0],pos[1],pos[2]);    
           }
           else 
           {
                 printf("Error: unable to read/write vert position number %d.\n", vn);
                 break;
           }
           if(fseek(model,36,SEEK_CUR))
           {
                 printf("EOF or error.\n");
                 break;
           }
    }
    if(fseek(skin,0x10,SEEK_SET))
    {
           printf("Seek error: skin file too short.\n");
           fclose(model);
           fclose(skin);
           fclose(output);
           return 1;  
    }
    if(!fread(&triofs,4,1,skin))
    {
           printf("Error: unable to read triangle offset.\n");
           fclose(model);
           fclose(skin);
           fclose(output);
           return 1;
    }
    if(fseek(skin,0x4,SEEK_CUR))
    {
           printf("Seek error: skin file too short.\n");
           fclose(model);
           fclose(skin);
           fclose(output);
           return 1;
    }
    if(!fread(&triendofs,4,1,skin))
    {
           printf("Error: unable to read triangle count.\n");
           fclose(model);
           fclose(skin);
           fclose(output);
           return 1;
    }
    trin=(triendofs-triofs)/6;
    //trin = trin / 3;
    fprintf(output,"g %s\n", name);
    if(fseek(skin,triofs,SEEK_SET))
    {
           printf("Seek error: skin file too short.\n");
           fclose(model);
           fclose(output);
           return 1;  
    }
    
    while(trin -- )
    {

            if(fread(&tripos,2,3,skin) == 3)
            {
                 fprintf(output,"f %d %d %d\n",tripos[0]+1,tripos[1]+1,tripos[2]+1);
            }
            else 
            {
                 printf("Error: unable to read/write triangle number %d.\n",trin);
                 break;
            }
            
    }
    fclose(model);
    fclose(skin);
    fclose(output);
    printf("\n");
    return EXIT_SUCCESS;
} 

This code somewhat works but I need to find a way to make that array to fix the output.
Edit: I can't post the error because the compiler is not saying exactly what is the problem but since this is the only thing I added after it worked it has to be the problem.
Last edited on
It looks like your initializing an array with a value of 0 elements so it can not hold anything. Change your variable vn to 1 and then you are able to initialize the array with 1 element.
1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;

int main() {
    int vn = 1;
    int someArray[vn];
    someArray[0] = 1; //Index 0 is still the first starting point.
}

I think that might be the problem.
Actually the value is later changed from 0 to
fread(&vn,4,1,model)
which in the example file I'm using is 9156
Also Peter87 said that I can't declare an array with variable that way.
Oh then just allocate the memory to the array then. I brain farted but if you want a run time array you need to to do this. Although I swear you could initialize an array to a value in a variable as long as it never changed in value and happened at compile time, although I could be wrong.
1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;

int main() {
    int vn;
    cin >> vn;
    int *someArray = new int[vn]; //Makes a run time array by allocating memory
    delete[] someArray; //Deletes the array to free up memory/prevent mem leak
}

Last edited on
Thank you rabster,
this works like a charm and I hope I won't need any more help to finish this script.
Topic archived. No new replies allowed.