c - For "int demo[4][2]",why are all these same in magnitude: &demo[1],demo[1],demo+1,*(demo+1) ?What about type? -


just when had relaxed thinking have fair understanding of pointers in context of arrays,i have fallen face down again on following program.i had understood how array arr,arr , &arr both same in magnitude,but different in type,but fail solid grip on following program's output.i try visualize succeed partially.i appreciate if can give rigorous , detailed explanation thing guys me can done confusion good.

in following program,i have used "2d" array demo[][2].i know demo[] array of arrays of size 2.i know demo used alone of type (*)[2].still @ loss following :

1) why &demo[1] same demo[1]?isn't demo[1] supposed address of second array?what on earth &demo[1] , why same address of second array?

2) know second printf() , fourth same,as demo[1] nothing else *(demo+1).but i've used illustrate point.how can equal third printf(),ie,how can demo+1 equal *(demo+1)? demo[1] being same *(demo+1) well-known,but how can demo+1 equal *(demo+1)? how can "something" equal value @ "something"?

3) , since proved not smart,i should stop guessing game , ask conclusive answer types following :

&demo[1]
demo[1]
demo+1

#include<stdio.h>  int main(void) {     int demo[4][2]= {{13,45},{83,34},{4,8},{234,934}};     printf("%p\n",&demo[1]);     printf("%p\n",demo[1]);  //should have cast (void*),but works still     printf("%p\n",demo+1);     printf("%p\n",*(demo+1)); } 

output:

0023ff28 0023ff28 0023ff28 0023ff28 

demo[1] second member of array demo, , array itself. other array, when it's not subject of & or sizeof operator, evaluates pointer first element - is, demo[1] evaluates same thing &demo[1][0], address of first int in array demo[1].

&demo[1] address of array demo[1], , because address of array , address of first member of array same location, &demo[1] equal &demo[1][0], equal bare demo[1]. key insight - first element of array located @ same place in memory array itself, first member of struct located @ same place in memory struct itself. when print &demo[1] , demo[1], you're not printing pointer array , array; you're printing pointer array , pointer first member of array.

demo+1 address of second member of demo. *(demo+1) member (it's array demo[1]), because member array, evaluates pointer first member. above, first member collocated array itself. it's not "something" equal value @ "something" - because when use array in expression that, doesn't evaluate array itself.

  • &demo[1] pointer demo[1], array of 2 int. type int (*)[2].
  • demo[1] array of 2 int. type int [2]. however, when used in expression not subject of type & or sizeof operators, evaluate pointer first member, value type int *.
  • demo+1 pointer demo[1], , type int (*)[2].

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -