I’ve seen this question and I noticed that I get errors when I declare variables in the middle of the main() function, but I thought that creating variables dynamically wouldn’t cause an error, because it can be done anywhere and anytime during run-time (as far as I know).
However, I still get:
error C2065: 'i' : undeclared identifier
error C2065: 'z' : undeclared identifier
error C2065: 'intArr' : undeclared identifier
My code:
int main()
{
.....
.....
.....
printf("Type the array size:\t");
int *z = (int *)malloc(sizeof(int));
scanf("%d", z);
int *intArr = (int *)malloc((*z) * sizeof(int));
int *i = (int *)malloc(sizeof(int));
for (*i = 0; *i < *z; ((*i)++))
{
printf("Type a number\t");
scanf("%d", (intArr+(*i)));
}
printArr(intArr);
}
void printArr(int *arr)
{
int i;
for (i = 0; i < (sizeof(arr) / sizeof(*arr)); ++i)
printf("%d ", *(arr+i));
}
(I’m not sure why @Blood deleted his answer; it was essentially correct.)
When I compile your program using gcc, it compiles with no errors. I had to add
to the top, and delete the three
.....lines.When I compile the same program using Microsoft’s Visual C++ 2010 Express, I get a number of errors. Several of them complain about undeclared identifiers, but that’s a common side effect of syntax errors; if the compiler can’t parse your source file, it’s likely to become “confused” as it tries to recover. The most relevant error is:
on line 10:
The problem is that the 1989/1990 version of the C standard doesn’t permit mixing declarations and statements within a block; it requires all declarations to appear first, followed by all statements. The 1999 C standard changed that, but Microsoft’s C compiler has very limited support for any C standard past the 1990 one (and they’ve said they have no intention of changing that). (I expect they might permit mixed declarations and statements in a future version, since that’s a C++ feature as well.)
(I’m assuming from the form of the error messages that you’re using a Microsoft compiler.)
You can rearrange your code to satisfy the Microsoft compiler’s restrictions. In some cases, you might need to change something like
to
Another suggestion, not related to your question:
In C, you shouldn’t cast the result of
malloc(). Themalloc()function returns a value of typevoid*, which can be implicitly converted to any pointer-to-object type. (C++ doesn’t have this implicit conversion, but you probably shouldn’t be usingmalloc()in C++ anyway.)Rather than this:
you can write this:
Dropping the unnecessary cast can avoid certain errors; for example, with some compilers a cast can mask a necessary error message if you’ve forgotten the required
#include <stdlib.h>. And applyingsizeofto*zor*intArr, rather than naming the size explicitly, means that you won’t have to change the call if the type of the pointer changes. If you write, for example:then you’re allocating the wrong size, but the compiler won’t warn you about it.
Also, if you’re allocating a single
intvalue usingmalloc(), as you’re doing with youriandzpointers, you’re doing unnecessary work. Unless your purpose is to practice usingmalloc(), you might as well just makeiandzintvariables, and drop themalloc()calls. You’ll just have to pass their addresses toscanf. In other words, you can change this:to this:
One more point: your program has no error checking.
malloc()can fail if there isn’t enough memory to allocate; it returns a null pointer (NULL) when this happens.scanf()can fail if there’s an input error, or if you typehellowhen it’s expecting to read anint.scanf()returns the number of items it successfully scanned; you should verify that it did so (in this case, it returns 1 on success). For a simple program like this, aborting the program with an error message:is probably good enough.