"Kiran" <Ki*********@gmail.comwrites:
int main() {
int* nums;
int i;
for (i=0; i < 5; i++) {
printf("%d", nums[i]);
}
You should return a value from main(), probably 0.
}
void modify( int* nums ) {
int i;
nums = (int*)malloc( 5*sizeof(int));
I don't recommend casting the return value of malloc():
* The cast is not required in ANSI C.
* Casting its return value can mask a failure to #include
<stdlib.h>, which leads to undefined behavior.
* If you cast to the wrong type by accident, odd failures can
result.
In unusual circumstances it may make sense to cast the return value of
malloc(). P. J. Plauger, for example, has good reasons to want his
code to compile as both C and C++, and C++ requires the cast, as he
explained in article <9s*****************@nwrddc01.gnilink.net>.
However, Plauger's case is rare indeed. Most programmers should write
their code as either C or C++, not in the intersection of the two.
When calling malloc(), I recommend using the sizeof operator on
the object you are allocating, not on the type. For instance,
*don't* write this:
int *x = malloc (128 * sizeof (int)); /* Don't do this! */
Instead, write it this way:
int *x = malloc (128 * sizeof *x);
There's a few reasons to do it this way:
* If you ever change the type that `x' points to, it's not
necessary to change the malloc() call as well.
This is more of a problem in a large program, but it's still
convenient in a small one.
* Taking the size of an object makes writing the statement
less error-prone. You can verify that the sizeof syntax is
correct without having to look at the declaration.
for (i=0; i < 5; i++) {
nums[i] = i;
}
}
Your real problem is in the FAQ:
4.8: I have a function which accepts, and is supposed to initialize,
a pointer:
void f(int *ip)
{
static int dummy = 5;
ip = &dummy;
}
But when I call it like this:
int *ip;
f(ip);
the pointer in the caller remains unchanged.
A: Are you sure the function initialized what you thought it did?
Remember that arguments in C are passed by value. The called
function altered only the passed copy of the pointer. You'll
either want to pass the address of the pointer (the function
will end up accepting a pointer-to-a-pointer), or have the
function return the pointer.
See also questions 4.9 and 4.11.
--
"Large amounts of money tend to quench any scruples I might be having."
-- Stephan Wilms