dominant wrote:
I have the following code.
What is wrong ? Let's see...
1) did you read the FAQ at http://www.eskimo.com/~scs/c-faq/top.html ?
If not, shame one you. Read it. It helps.
int *a;
a is a pointer to an int. Note that it does not point particularly anywhere.
for(int i=0;i<10;i++) {
Ah. You must have a C99 compliant compiler. In C89 (which is still the
most implemented version of the C standard), you could not declare i in
the for statement. No matter. i is an int, and will range from 0 to 9.
So far so good.
a=i;
Now this is a problem. a is a pointer. i is an int. You can't really
assign i to a and get meaningful, reliable and consistent results. Your
compiler should have produced a diagnostic on this line. If not, try to
pump up the warning level.
}
printf("%d", a[5]);
And now that's it. You are trying to access the 6th element of without
having ever prepared storage for it, *and* without even having assigned
a value to it (notwithstanding calling printf without a proper prototype
in scope... note that you should *always* post a compilable snippet of
code demonstrating you problem).
Could we have arrays with unknown length (when we define them)?
Yes we can. Let's tackle one problem at a time here. First of all, given
an array whose length you know, let's fill it out:
#include <stdio.h> /* for printf */
#include <stdlib.h>
int main(void)
{
int a[10]; /* a is an array of 10 ints */
size_t i; /* a size_t is more adapted than
an int for an array index. */
for (i = 0; i < 10; i++)
/* note that the following is also correct, a being an array
and avoids the issue of repeating the size of a:
for (i = 0; i < sizeof(a) / sizeof(*a); i++)
*/
{
a[i] = i * 2; /* a[0] = 0, a[1] = 2, a[2] = 4, ... */
}
printf("%d\n", a[5]); /* note that the \n is necessary to
*guarantee* that the printf will
actually output something *now* */
return EXIT_SUCCESS; /* portable values are EXIT_SUCCESS,
EXIT_FAILURE and 0 */
}
As you can see, there is a _big_ difference inside the for loop with
your code. Now, let's say you do not know the size of you array at
compile time. You need to use a pointer int *a, and you need to allocate
storage for the array it is going to point to, with malloc:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int *a;
size_t i;
size_t size_of_a;
int status = EXIT_SUCCESS;
size_of_a = 10;
a = malloc(size_of_a * sizeof *a); /* this means that we allocate
enough room for size_of_a times
the size of the type pointer to
by a (sizeof *a), in that case
int. */
if (NULL == a)
{
/* malloc can fail. If it does, you need to handle the failure
one way or another... */
puts("Could not allocate storage for a");
status = EXIT_FAILURE;
}
else
{
/* now a points to somewhere valid... */
for (i = 0; i < size_of_a; i++)
{
a[i] = 2 * i + 1;
}
printf("%d\n", a[5]);
}
return status;
}
Voila ! Now, you need to get a good C book (like Kernighan & Ritchie 2nd
edition), read the FAQ, and study some more...
--
Bertrand Mollinier Toublet
Currently looking for employment in the San Francisco Bay Area
http://www.bmt.dnsalias.org/employment