Jump to content

A bug, pointers to arrays

- - - - -

  • Please log in to reply
5 replies to this topic

#1
AdvMutant

AdvMutant

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 438 posts
I'm trying to learn about pointers to multi-dimensional arrays, but there's


#include  <stdio.h>


#define MAX1 4

#define MAX2 3


int x[MAX1] [MAX2], ctr, ctr2, ctrVal, *ptr;

signed int vals[MAX1 * MAX2];


int main()

{

	int (*y)[MAX2];


	ctrVal = 0;

	for(ctr2 = 0; ctr2 < MAX2; ctr2++)

	{

		ptr = &x[ctr][0];


		for(ctr = 0; ctr < MAX1; ctr++)

		{

			x[ctr] [ctr2] = ctr + 10;

			printf("Element [%d] [%d], on adress %d - %d\n", ctr, ctr2, ptr, x[ctr] [ctr2]);

			vals[ctrVal++] = (int)ptr++;

		}


		putchar('\n');

	}


	ptr = &x[0] [0];

	printf("\n\nptr = %d\n", &x[0] [0]);

	for(ctr = 0; ctr < (MAX1 * MAX2); ctr++)

	{

		printf("Value on adress %d - %d\n", ptr, *ptr);

		

		if(ptr == vals[ctr])

			printf("The adress matches vals[%d].\n\n", ctr);

		

		ptr++;

	}


	return 0;

}


I expect the values printed by the last loop to be the same as the first loop, but for some reason they're different. Can you please point me to the bug?

Thanks, Mike.

Posted Image
There is no problem that cannot be solved by the use of high explosives.


#2
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

I guess thats the place where you have done something wrong



for(ctr2 = 0; ctr2 < MAX2; ctr2++)

	{

		ptr = &x[ctr][0];


		for(ctr = 0; ctr < MAX1; ctr++)

		{

			x[ctr] [ctr2] = ctr + 10;

			printf("Element [%d] [%d], on adress %d - %d\n", ctr, ctr2, ptr, x[ctr] [ctr2]);

	[COLOR="red"]		vals[ctrVal++] = (int)ptr++;

	[/COLOR]	}


		putchar('\n');

	}



It should have been like


for(ctr2 = 0; ctr2 < MAX2; ctr2++)

	{

		ptr = &x[ctr][0];


		for(ctr = 0; ctr < MAX1; ctr++)

		{

			x[ctr] [ctr2] = ctr + 10;

			printf("Element [%d] [%d], on adress %d - %d\n", ctr, ctr2, ptr, x[ctr] [ctr2]);

			[COLOR="red"]vals[ctrVal++] = (int)*ptr;

                        ptr++;

		[/COLOR]}


		putchar('\n');

	}



I believe this should fix the issue.

Thanks a lot!

Munir

#3
AdvMutant

AdvMutant

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 438 posts
:c-lol:
Ironically, I need to have memory addresses at the vals[] array. What's weird is that the values printed at the first loop are 10, 11, 12 and 13. Three times - 10, 11...13. Then at the second loop, it prints different addresses. The numbers are 10, 10, 10. 11, 11, 11...13, 13, 13. I don't understand why.

Posted Image
There is no problem that cannot be solved by the use of high explosives.


#4
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

You have been doing some mistake in the loop you were setting the x array: The modified and working code is


#include  <stdio.h>


#define MAX1 4

#define MAX2 3


int x[MAX1] [MAX2], ctr, ctr2, ctrVal, *ptr;

unsigned int vals[MAX1 * MAX2];


int main()

{

	int (*y)[MAX2];


	ctrVal = 0;

	for(ctr2 = 0; ctr2 < MAX1; ctr2++)

	{

		ptr = &x[ctr2][0];


		for(ctr = 0; ctr < MAX2; ctr++)

		{

			x[ctr2] [ctr] = ctr + 10;

			printf("Element [%d] [%d], on adress %d - %d\n", ctr, ctr2, ptr, x[ctr2] [ctr]);

			vals[ctrVal++] = (int)ptr++;

		}


		putchar('\n');

	}


	ptr = &x[0] [0];

	printf("\n\nptr = %d\n", &x[0] [0]);

	for(ctr = 0; ctr < (MAX1 * MAX2); ctr++)

	{

		printf("Value on adress %d - %d\n", ptr, *ptr);

		

		if(ptr == (int *)vals[ctr])

			printf("The adress matches vals[%d].\n\n", ctr);

		

		ptr++;

	}


	return 0;

}


I hope this fixes your issues.

Munir

#5
AdvMutant

AdvMutant

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 438 posts
Thanks, it works! I've wasted 3-4 hours on trying to figure it out myself, I don't understand why I didn't discover the problem.

So what did you fix? Did you just swap MAX1 and MAX2 in the for loops?

Posted Image
There is no problem that cannot be solved by the use of high explosives.


#6
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

Yes, because we need to store memory address in same sequence as you're storing values to array:

swaping max1, max2 ensured that we are filling array in correct sequence that is
[0][0] [0][1] [0][2]
[1][0] [1][1] [1][2]
------------

and we have been storing address in the same sequences: so swaping max1, max2 makes the sequence right!

Munir




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users