Start New Discussion within our Software Development Community

This is what I have so far.

I have to create array of 10 int values as local variable then iterate through the array elements using pointer, print address, and value of the each item.

But this is my output. It should print values from 0 to 10, but mine seem to print from 2 to 10 and 1 empty address.

And is pointer address supposed to be same for 0 to 10 like my output?

[IMG]http://dl.dropbox.com/u/43732846/output.jpg[/IMG]

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

int main(void) {
	int values[10] = {1,2,3,4,5,6,7,8,9,10};
	int *pointer1;
	int i;
	pointer1 = values;

	for(i = 0; i < 10; i++) { 
		pointer1++;
		printf("value, deference pointer, address \n");
		printf("pointer1 = %p, *pointer1 = %d, &pointer1 = %p\n", pointer1, *pointer1, &pointer1);
	}

	printf("\n\n Press any key to continue.");
	getchar(); getchar();
	exit(0);
}

In your for statement

for(i = 0; i < 10; i++) {
printf("value, deference pointer, address \n");
printf("pointer1 = %p, *pointer1 = %d, &pointer1 = %p\n", pointer1, *pointer1, &pointer1);
pointer1++;//this line should go last
}

Or better yet.

#include <stdio.h>

int main(void) 
{
	int values[10] = {1,2,3,4,5,6,7,8,9,10};
	int *pointer1;
	pointer1 = values;

	while ( pointer1 < &values[10] )
	{ 
	  fprintf(stdout, "address->%p, value->%d\n", (void*)pointer1, *pointer1);
	  ++pointer1;
	}

	return 0;
}
This question has already been answered. Start a new discussion instead.