int *a, indicator fault

D

dominant

I have the following code.



int *a;



for(int i=0;i<10;i++) {



a=i;



}





printf("%d", a[5]);







It occurs an error, What is wrong?



Could we have arrays with unknown length (when we define them)?
 
M

Mark A. Odell

I have the following code.



int *a;
for(int i=0;i<10;i++) {
This would be a = &i; But this won't work because...
}

printf("%d", a[5]);

a[5] is at address of 'i' + 5 more int sizes further off in memory. You
don't own that memory.
It occurs an error, What is wrong?

Why not *point* 'a' at something appropriate!!!!???? You point to 'i'
which is a single int, not an array of 6 (yes 6) ints.
Could we have arrays with unknown length (when we define them)?

Look up malloc(), and don't cast its return value, just check for NULL
before using it.
 
M

Martin Dickopp

dominant said:
I have the following code.

int *a;

for(int i=0;i<10;i++) {
a=i;
}
printf("%d", a[5]);

Please don't put a large number of empty lines between each non-empty
line in Usenet posts. (I have corrected this in the quotation.)
It occurs an error, What is wrong?

First of all, you are trying to assign an integer value (the value of `i')
to a pointer (`a'). This is not allowed in C; it's a constraint violation
and the compiler must emit a diagnostic.

Secondly, `a' is never initalized, i.e. it doesn't point to anything.
The expression `a[5]' therefore causes undefined behavior.
Could we have arrays with unknown length (when we define them)?

No, but you can use a pointer instead of an array for that. Before you
access the memory the pointer points to, you have to allocate said memory:


#include <stdlib.h>
#include <stdio.h>

int main (void)
{
int *a, i;

a = malloc (10 * sizeof *a);
if (a == 0)
{
fputs ("Cannot allocate memory.\n", stderr);
return EXIT_FAILURE;
}

for (i = 0; i < 10; ++i)
{
a = i;
}

printf ("a [5] = %d\n", a [5]);

free (a);
return EXIT_SUCCESS;
}


Martin
 
B

Bertrand Mollinier Toublet

dominant said:
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.
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.
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 * 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 = 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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,573
Members
47,205
Latest member
ElwoodDurh

Latest Threads

Top