Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Sun, 29 Apr 2012 08:24:16 -0500 Message-ID: <4F9D4101.128@mindspring.com> Date: Sun, 29 Apr 2012 09:24:17 -0400 From: pete Reply-To: pfiland@mindspring.com Organization: PF X-Mailer: Mozilla 3.04Gold (WinNT; I) MIME-Version: 1.0 Newsgroups: comp.lang.c Subject: Re: The question regarding type of pointers References: <39cgp71somr8bjmg3rufk6ins863nhdcco@4ax.com> <96bb54c9-5ea3-49ff-851b-56dfbb8eac54@qg3g2000pbc.googlegroups.com> <4F9C3E13.41E3@mindspring.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 97 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 4.154.222.63 X-Trace: sv3-zdIVSfBmGaAr40sM1f77pC8Y5SJa2Lh22hmKgEOyMFyaXUZlIKzC5lSGHfnYt8MkuNnZ03aiuUEJgpC!Pe6PNY23gxjySIyFYrtOFLMCqy62iSOaKxN4csQuQ2SGiZhmIbsHhHXgesOCJNzUarXwg5nTdbyx!hp9HRJmg X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 4317 Xref: csiph.com comp.lang.c:20049 somenath wrote: > > On Apr 28, 11:59 pm, pete wrote: > > somenath wrote: > > > Now let us focus on the two dimensional array. > > > ========================================================== > > > Suppose we have the following declaration > > > int ar[3][3]; > > > Question=> What is sizeof( ar[3])? > > > > > So what is ar[3]? ar[3] contains the pointer > > > > No, ar[3] doesn't contain any pointer. > > > > ar[3] is an expression of type array of three int. > > I did not understand this. According to my understanding C does not > have array value instead Instead, the "value" of the array is a > pointer to the first element of that array. > > So for if we have int ar[3]; > ar will have address of ar[0]. Essentially it is pointer to the first > element of a row of 3 int. > So now if we have int ar[3][3]; > > ar[0] will have the address of the first element of the the first row > of 3 int. > similarly ar[3] will have the address of the first element of the > third row of 3 int. > So the type of ar[3] will be int(*)[3] in value context. Where I am > going wrong? Please correct my understanding. > > > > > /* BEGIN new.c */ > > > > #include > > > > int > > main(void) > > { > > char ar[3][7]; > > > > printf("sizeof ar[3] is %u\n.", (unsigned)sizeof ar[3]); > > return 0; > > > > } > > So sizeof operator needs ar[3] in object context. So ar[3] in the > above is in object context not in value context. Now ar[3] is an array > object of 7 char. So the printf will print the result as sizeof > (char ) *7 not sizeof(char (*)[7]) > > Now if we think about the value of ar[3] then it will be the address > of the first element of the the third row of size 7. Please correct > me if I am wrong. There is no ar[3], so ar[3] has no value. This declaration {char ar[3][7];} declares an array of 3 arrays of 7 char. The 3 arrarys are ar[0], ar[1], and ar[2]. The size of each of those 3 arrays is 7 bytes. It doesn't matter that there is no ar[3] for the expression (sizeof ar[3]) because the operand of sizeof is not evaluated. The type of the expression of the operand is all that matters to the sizeof operator. printf("sizeof ar[3000] is %u\n.", (unsigned)sizeof ar[3000]); would have outputed "sizeof ar[3000] is 7" Expressions of array type are converted to pointers in all but 3 contexts: 1 operand of sizeof operator 2 operand of address operator 3 string literal initializer When you have a declaration like char ar[3][7]; without any more context, then I will describe the type of the expression (ar), as an array type. The type of the expression (ar + 0), is a pointer type. You can consider the entire contents of an array as the value of the array. But, there is no operator in C which can assign the value of an array to another array, and there is no operator in C which can compare the value of one array with another. -- pete