-4

For the following code fragment:

 unsigned int *ptr[10];
 int a[10]={0,1,2,3,4,5,6,7,8,9};
 *ptr=a;
 printf("%u %u",ptr,a);

i checked on codepad.org and ideone.com.On both compilers its showing different values of ptr and a

Ankur Singh
  • 25
  • 1
  • 4

4 Answers4

1

With warnings on:

pointer targets in assignment differ in signedness
format ‘%u’ expects type ‘unsigned int’, but argument 2 has type ‘unsigned int **’
format ‘%u’ expects type ‘unsigned int’, but argument 3 has type ‘int *’
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
0

When used in pointer context, ptr points to the beginning of ptr array, while a points to the beginning of a array. These are two different arrays that occupy completely different places in memory. Why would they be the same?

Of course, printing pointer values with %u is a crime. Use %p. That's what %pis for.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
0

This is a array of pointers

*ptr[10]

If you want to assign a to this go:

(*ptr)[10]
Theolodis
  • 4,977
  • 3
  • 34
  • 53
0

uint *ptr[10] is equivalent to uint **ptr and assignment *ptr = a is the same as ptr[0] = a which assign a to first offset inside ptr array, it doesn't touch value of ptr itself...

You maybe wanted to used one of these:

ptr = &a;

// Or
printf("%u %u",ptr[0],a);

// Or
unsigned int *ptr;
ptr = a;
Vyktor
  • 20,559
  • 6
  • 64
  • 96