(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
#include <stdio.h> #include <stdlib.h>
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:
syntax error : missing ';' before 'type'
on line 10:
printf("Type the array size:\t"); // line 9 int *z = (int *)malloc(sizeof(int)); // 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
int *var = initial_value;
to
int *var; // ... var = initial_value;
Another suggestion, not related to your question:
In C, you shouldn't cast the result of malloc(). The malloc() function returns a value of type void*, which can be implicitly converted to any pointer-to-object type. (C++ doesn't have this implicit conversion, but you probably shouldn't be using malloc() in C++ anyway.)
Rather than this:
int *z = (int *)malloc(sizeof(int)); ... int *intArr = (int *)malloc((*z) * sizeof(int));
you can write this:
int *z = malloc(sizeof *z); ... int *intArr = malloc(*z * sizeof *intArr);
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 applying sizeof to *z or *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:
double *p = malloc(sizeof (int)); // incorrect
then you're allocating the wrong size, but the compiler won't warn you about it.
Also, if you're allocating a single int value using malloc(), as you're doing with your i and z pointers, you're doing unnecessary work. Unless your purpose is to practice using malloc(), you might as well just make i and z int variables, and drop the malloc() calls. You'll just have to pass their addresses to scanf. In other words, you can change this:
int *z = (int *)malloc(sizeof(int)); ... scanf("%d", z);
to this:
int z; ... scanf("%d", &z);
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 type hello when it's expecting to read an int. 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:
fprintf(stderr, "Call to ... failed\n"); exit(EXIT_FAILURE);
is probably good enough.
zandidynamically? They look like simple size and loop variables! 2. If you do, you should alsofreethem. 3.type *var = malloc(size * sizeof(*var));is much more maintainable as you don't repeattypeover and over again.