0

My code is given below. I want to declare an array of size n.

FILE *fp;
fp=fopen("myfile.b", "rb");
if(fp==NULL){ fputs("file error", stderr); exit(1); }
int* a;
fread(a, 0x1, 0x4, fp);
int n=*a;
int array[n];  // here is an error 

How can I declare an array of size n in this code?

3
  • Why are you using C code in C++? C++ have a very nice input/output library. Commented Oct 21, 2013 at 7:43
  • An example of perfectly valid C code does not work in C++... Commented Oct 21, 2013 at 7:44
  • 1
    @JoachimPileborg "very" nice is a matter of taste. :P Commented Oct 21, 2013 at 8:36

5 Answers 5

4

That is a declaration of a variable-length array and it's not in C++ yet.

Instead I suggest you use std::vector instead:

std::vector<int> array(n);

You also have other problems, like declaring a pointer but not initializing it, and then using that pointer. When you declare a local variable (like a) then its initial value is undefined, so using that pointer (except to assign to it) leads to undefined behavior. In this case what will probably happen is that your program will crash.

Sign up to request clarification or add additional context in comments.

1 Comment

don't forget #include <vector>
0
int *array = (int*)malloc( n * sizeof(int) );
//..
//your code
//..
//..
free(array);

Comments

0

You cannot declare an array of variable size in C++, but you can allocate memory once you know how much you need:

int* a = new int[n];

//do something with your array...

//after you are done:

delete [] a;

Comments

0

Since your code looks more like C...

FILE *fp = fopen("myfile.b", "rb");

if(fp==NULL)
{ 
  fputs("file error", stderr); 
  exit(1); 
}

//fseek( fp, 0, SEEK_END ); // position at end
//long filesize = ftell(fp);// get size of file
//fseek( fp, 0, SEEK_SET ); // pos at start

int numberOfInts = 0;
fread(&numberOfInts, 1, 4, fp); // you read 4 bytes sizeof(int)=4?
int* array = malloc( numberOfInts*sizeof(int) );

4 Comments

int* array = malloc( numberOfInts*sizeof(int) ); in this line there occure an error C2440: 'initializing' : cannot convert from 'void *' to 'int *'
@azam42: That's the error you get when compiling as C++. This answer is valid C. That said, I'd suggest int array[numberOfInts] if you use C.
@MSalters: it is working in c++ by specifying the type of array from void to int like this int* array = (int )malloc( numberOfIntssizeof(int) );
when you compile that code with a C++ compiler then it is not valid to write just p = malloc(...) in C it is valid and the recommended way to do it. That is why you need the cast - I assumed a C compiler.
0

Array only accept const object or expressions, the value can be decided by the compiler during compiling, vector from c++ is more suitable from this case, otherwise we need to dynamically allocate memory for it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.