473,387 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Why is sizeof(a) zero? where a is int a[0]?

$ cat VLA.c

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. void main()
  3. {
  4.         int a[0];
  5.         int *b;
  6.  
  7.         /*As we say a[0] is equivalent to *(a+0) */
  8.         printf("\n a = %d b = %d",sizeof(a),sizeof(b));
  9. }
  10.  
  11. $gcc VLA.c
  12. $ ./a.out
  13.  
  14.  a = 0 b = 4
  15.  
Why sizeof(a) is zero ?
Jan 21 '13 #1
5 1611
u have declared int a[0], which means declaring an array which contains no elements, tats y it is returning as 0.Also u cant do anything with it, it is of no use, to declare an array with 0 elements.
Jan 21 '13 #2
Thanks for reply.
How should we differentiate int a[0] and int *b ? As we say a[0] is equivalent to *(a+0), which is nothing but *a.
Jan 21 '13 #3
int *b, is a pointer to an integer and int a[n], is an array of n integers. Dont get confused by the two.


if we declare int *b; 4 bytes of memory will be allocated
for it (considering sizeof int as 4).

consider b points to memory location 1000 , so b[0]==*(b+0) points to 1000,
b[1]==*(b+1) points to 1004
and so on.

if we declare int a[0];
intially no memory gets allocated for it.
but when do some operations like a[0]=2; then 'a' will points to some memory location say '2000'
no a[1] will points 2004 and goes on.
Jan 21 '13 #4
donbock
2,426 Expert 2GB
By the way, line 2 should be
Expand|Select|Wrap|Line Numbers
  1. int main(void)
As a consequence of making this change, you will need to return a value from main.
Jan 22 '13 #5
for any pointer size is 4 Bytes because.. Pointer is used to store memory address and every memory address in Computer are integer valuse so sizeof((int*a)) is always 4 bytes...
Jan 24 '13 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
28
by: Andreas Prilop | last post by:
Jukka reports on http://www.cs.tut.fi/~jkorpela/chars/spaces.html that Internet Explorer 6 fails on the "zero width space" U+200B ​ Is this observation still valid? For which versions of MS...
15
by: I wish | last post by:
#include <string.h> int a; memset( a, 0, sizeof(a) ); Does that guarantee all bits zero? -- |
25
by: Mantorok Redgormor | last post by:
Finally, control is returned to the host environment. If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. beyond this...
53
by: Zhiqiang Ye | last post by:
Hi, All I am reading FAQ of this group. I have a question about this: http://www.eskimo.com/~scs/C-faq/q7.31.html It says: " p = malloc(m * n); memset(p, 0, m * n); The zero fill is...
25
by: pm940 | last post by:
Hello. I've been reading some past discussions on the NULL vs. zero. References are always made to systems or machienes that use values other than zero to represent the NULL pointer. Although...
10
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaac11/html/acfctNZ_HV05186465.asp "If the value of the variant argument is Null, the Nz function returns the number zero or a...
4
by: H.S. | last post by:
Hello, I am trying out a few methods with which to test of a given number is practically zero. as an example, does the following test correctly if a given number is zero within machine...
23
by: Hallvard B Furuseth | last post by:
As far as I can tell, (x & -1) is nonzero if the integer x is negative zero. So for signed types, x == 0 does not guarantee (x & foo) == 0. Is that right? (Not that I expect to ever encounter a...
1
by: mbluett | last post by:
I am having a problem with a C# application that I wrote sometime ago. It worked fine after creating it and for roughly a year after. At some point within 2007 something changed on my Windows XP...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.