0

In my below code,why value and address of ptr[0] is same. Same behavior for ptr[1] and ptr[2].

#include<stdio.h>

int main()
{
    int var=100;
    int(*ptr)[5];//array of pointer.

    ptr=&var;
    printf("Value of var is  : %d\n",var);
    printf("Address of var : %u\n",&var);
    printf("Value inside ptr after ptr=&var : %u\n",ptr[0]);
    printf("Value of ptr[0] is  : %d\n", ptr[0]);
    printf("Adress of ptr[0] is  : %u\n",&ptr[0]);

    printf("Value of ptr[1] is  : %d\n",ptr[1]);
    printf("Adress of ptr[1] is  : %u\n",&ptr[1]);

    printf("Value of ptr[2] is  : %d\n",ptr[2]);
    printf("Adress of ptr[2] is  : %u\n",&ptr[2]);
    return 0; 
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294

3 Answers3

4

In your case

  int(*ptr)[5];//array of pointer.

is not exactly "array of pointer.". Rather, it is a pointer to an array of 5 ints.

So,

 ptr=&var;

is wrong because, &var is not a pointer to an array.

Then, accessing ptr[1] is absolutely wrong because of out-of-bound access. ptr here, is not an array all by itself.

You may want to rewrite

  int(*ptr)[5];

as

   int *ptr[5];

That said,

  1. int main() should be int main(void), as per the standard.
  2. Always use %p to print the address. Also, cast the pointer to (void *) before passing as argument, as %p expects a void *.
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • `int f(void)` is very unreadable than `int f()` and also involves lesser keystrokes. Bjanre called the former an "abomination". One might argue that his comment was for C++ and not C, but in this context it applies to both. Also the standard just gives a guideline there not a rigid rule: _It shall be defined with a return type of `int` and with no parameters […] or equivalent; or in some other implementation-defined manner._ – legends2k Jul 10 '15 at 07:00
  • 4
    @legends2k Don't listen to Bjarne, he is an abomination. In **C**, unlike C++, there is a distinct difference between `(void)` and `()`, namely that the latter, when lacking a proper function prototype means "accept any parameter" and may invoke an abomination called "the default argument promotions". Also, programmers measuring good programming practice in number of keystrokes need to find another career. Programming involves a whole lot of typing, see. – Lundin Jul 10 '15 at 07:00
  • I thought engineers apply thought and work without rotely following some rule. In this context, if you prefer `int main(void)` we'll end this here. I've no comments further :) – legends2k Jul 10 '15 at 07:01
  • @legends2k: Have you actually looked at the standard in detail? The 2 valid definitions given are `int main (int argc, char *argv[])` and equivalent. The _"equivalent"_ is explained in a footnote, saing that `int main (int argc, char **argv);` is valid, because it's compatible with `char *argv[]`. `int main()` is **not** equivalent, because it's compatible with `int main(struct foo *bar, double zar, const char **something);`, which is not valid according to the standard – Elias Van Ootegem Jul 10 '15 at 07:04
  • @EliasVanOotegem He is likely referring to the text "or otherwise in some implementation-defined manner" from the standard. See [this answer](http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c/31263079#31263079) where this part of the standard is discussed below "C99 hosted environment". – Lundin Jul 10 '15 at 07:39
  • @legends2k Indeed. The good software engineer would in this context would know the standard and then use their brain and be concerned about reduced type safety and reduced portability that potentially comes with `int main()`, compared to `int main(void)`. – Lundin Jul 10 '15 at 07:42
  • @Lundin Agree. Applying thought in a design approach is a good idea. Applying thought in re-defining standard in order to justify the wrong usage is most probably a wastage of time, IMHO. :-) – Sourav Ghosh Jul 10 '15 at 07:45
  • @Lundin FWIW: [the standard clearly states that in a function definition an empty set of parameters has a well-defined behaviour: that the function does not take any parameters](http://stackoverflow.com/a/12225214/183120). The keystrokes was just an additional argument, really, the key point was readability. If I came across as brash, I didn't mean to. – legends2k Jul 10 '15 at 08:02
  • @legends2k That answer is bad an contains incorrect statements. It is true that (6.7.6.3) "An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters.". However, this is bad style and flagged as obsolete, see C11 future language extensions 6.11.6 "The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature.". So if you write code as `int main()` your code might not compile in the next version of the standard. – Lundin Jul 10 '15 at 08:19
  • Hopefully cleared this up by adding [another answer](http://stackoverflow.com/a/31336367/584518) to that other thread linked. – Lundin Jul 10 '15 at 08:55
  • @Lundin Nice one, sir, :-) This link is a very useful link that will be used frequently, I assume. :-) – Sourav Ghosh Jul 10 '15 at 09:02
0
int(*ptr)[5];//array of pointer.
ptr=&var;

wouldn't compile error: assigning to 'int (*)[5]' from incompatible type 'int *'. ptr is not an int pointer but a pointer to an array of 5 integers, hence they're incompatible for assignment.

Even if you coerce the compiler with an explicit cast, this

printf("Value inside ptr after ptr=&var : %u\n",ptr[1]);

is clearly undefined behaviour since you'd be accessing beyond what is allowed.

why value and address of ptr[0] is same

Once you get undefined behaviour, all bets are off! Please read about undefined behaviour.

Community
  • 1
  • 1
legends2k
  • 31,634
  • 25
  • 118
  • 222
0

The value of a pointer might be determined by printing.

*ptr[0]

Since this is the proper syntax. Also if you print the address of prt[0] using

&ptr[0]

it will give the same address as of the var variable because it will automatically assign the 0'th index in the array of integer with the var. To check results, you might use the modified code below.

#include <stdio.h>

int main()
{
    int var=100;
    int(*ptr)[5]; 

    ptr=&var;
    printf("Value of var is  : %d\n",var); // it will print 100.
    printf("Address of var : %u\n",&var);  // it will print an address.
    printf("Value inside ptr after ptr=&var : %u\n",*ptr[0]); // it will print 100.
    printf("Value of ptr[0] is  : %d\n", *ptr[0]); // it will print 100.
    printf("Address of ptr[0] is  : %u\n",&ptr[0]); // it will print the address same as var.

    printf("Value of ptr[1] is  : %d\n",*ptr[1]); // it will print garbage value.
    printf("Address of ptr[1] is  : %u\n",&ptr[1]); // Address.

    printf("Value of ptr[2] is  : %d\n",*ptr[2]); // garbage value
    printf("Address of ptr[2] is  : %u\n",&ptr[2]); // Address.
    return 0;
}

Here's my output.

Value of var is  : 100
Address of var : 1386442756
Value inside ptr after ptr=&var : 100
Value of ptr[0] is  : 100
Address of ptr[0] is  : 1386442756
Value of ptr[1] is  : -87802171
Address of ptr[1] is  : 1386442776
Value of ptr[2] is  : 32767
Address of ptr[2] is  : 1386442796
phraniiac
  • 384
  • 1
  • 3
  • 16