I don't see a problem in the code you are posting; I would guess that your problem is in the code that you aren't posting. Is there some minimal bit you could post that would give this its necessary context?- What is atom_name? An array of char? A pointer? An int?
- What is atom_type?
- Where and how are you changing i?
- What does the loop look like?
- Are you checking malloc's return value?
Here is an example.
Code:
#include <stdio.h>
#include <stdlib.h>
struct sAtom
{
char name[32];
char type[16];
};
int main(void)
{
int i, count = 100;
struct sAtom *atom = malloc(count * sizeof(*atom));
if ( atom != NULL )
{
for ( i = 0; i < count; ++i )
{
sprintf(atom[i].name, "Name%02d", i);
sprintf(atom[i].type, "Type%02d", i);
printf("i = %2d, name = \"%s\", type = \"%s\"\n", i,
atom[i].name, atom[i].type);
}
printf("i = %d\n", i);
free(atom);
}
return 0;
}