Hi all,

I am having a problem printing out the elements of an array, using a for loop.

when i try to print out each individual element, it prints out the last element repeatedly.

ie will print the last element of the array, over and over again.

if you could post the cause of this problem or a solution would be greatly appreciated

thanks in advanced

techevo

Recommended Answers

All 5 Replies

Since you didn't post any code at all, I'll have to dig out my crystal ball and divine the source of your problems from thin air. Many people would call this pulling complete guesses out of my ass, but the possibility remains that I'm actually psychic.

I bet your code looks something like this:

int a[10];
int i;

for ( i = 0; i < 10; i++ )
  printf ( "%d\n", a[9] );

Where you're printing a single element every time ( a[9] ) rather than using the loop counter to index the array ( a[i] ).

If that's not it, post your code so we don't have to read your mind.

No need to be rude, you only had to ask

and no it does not look like that, i use array in the loop

that way it will start at 0 all the way till the last element.

for(i=0; i <= count-1; i++)
	{
		struct[i] = (struct *)malloc(sizeof(struct));
		
		while(struct[i]->number != 0)
		{
		printf("Processing %s, number = %ld\n", struct[i]->string, struct[i]->number);
		--struct[i]->number;
		sleep(1);
			if(struct[i]->number == 0)
			{
				printf("Process %s  Finished\n", struct[i]->string);
			}	
		}	
		free(struct[i]);
	}

>and no it does not look like that, i use array in the loop
>that way it will start at 0 all the way till the last element.
struct[i] is allocated but not initialized. Technically you're invoking undefined behavior, but from the description of your problem malloc is probably giving you the same uninitialized block of memory each time (or using debug initialization values for all blocks), which makes it look like you're processing the same array element over and over.

All in all your code is even more nonsensical than my original guess. Grats.

struct[i] is allocated but not initialized. Technically you're invoking undefined behavior, but from the description of your problem malloc is probably giving you the same uninitialized block of memory each time .

how can i solve this then? using calloc?

also im using an array of pointers to structs if that makes any difference.

please ask if you need any specific lines of code, the whole program is rather large am afraid.


thanks again


techevo

>how can i solve this then? using calloc?
You can solve it by doing something that isn't completely goofball in the first place. I would expect something more along these lines:

for ( i = 0; i < count; i++ ) {
  struct[i] = malloc ( sizeof *struct[i] );

  /* Initialize struct[i] here */
}

/* Other code, perhaps */

for ( i = 0; i < count; i++ ) {
  while ( struct[i]->number != 0 ) {
    printf ( "Processing %s, number = %ld\n", struct[i]->string, struct[i]->number );

    sleep ( 1 );

    if ( --struct[i]->number == 0 )
      printf("Process %s  Finished\n", struct[i]->string);	
  }

  /* Assuming you're done with struct[i] at this point */
  free ( struct[i] );
}

>also im using an array of pointers to structs if that makes any difference.
I kind of figured that one out on my own.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.