I'm doing a sample test question in C and can't explain the 'right answer'.  The question is what is the output of:  #include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; struct st{     int a;     char b; };  int main() {     struct st *st_ptr;     st_ptr = malloc(sizeof(struct st));     printf("%d\n",sizeof(struct st));     return 0; }   And possible answers  a) 8 b) 5 c) 0 d) none of the mentioned   I figured the answer was B (4 bytes + 1 byte) but they say the right answer is A (8 bytes).  Their explanation is: Maximum size of the data type is 4 byte(int) in the structure.  Can someone perhaps explain the right answer better?  Or is their right answer actually wrong.
1450
Why does the sizeof operator return a size larger for a structure than the total sizes of the structure's members?
46005
If I have a source.c file with a struct:  struct a {      int i;     struct b {         int j;     } };   How can this struct be used in another file (i.e. func.c)?  Should I create a new header file, declare the struct there and include that header in func.c?  Or should I define the whole struct in a header file and include that in both source.c and func.c?  How can the struct be declared extern in both files?  Should I typedef it? If so, how?
80355
I have this struct:  struct Map {     public int Size;      public Map ( int size )     {         this.Size = size;     }      public override string ToString ( )     {         return String.Format ( "Size: {0}", this.Size );     } }   When using array, it works:  Map [ ] arr = new Map [ 4 ] {     new Map(10),     new Map(20),     new Map(30),     new Map(40)};  arr [ 2 ].Size = 0;   But when using List, it doesn't compile:  List&lt;Map&gt; list = new List&lt;Map&gt; ( ) {     new Map(10),     new Map(20),     new Map(30),     new Map(40)};  list [ 2 ].Size = 0;   Why?
46689
I'm reading source code of the linux tool . They use the read() function there. Is there a difference to the standard C fread() function? (I'm not counting the arguments as a difference.)
13911
Possible Duplicate:          Here is a test case:   void foo(int i, int j) {    printf("%d %d", i, j); } ... test = 0; foo(test++, test);   I would expect to get a "0 1" output, but I get "0 0"  What gives??
13366
In , someone suggested in a  that I should not cast the result of malloc. i.e., I should do this: int *sieve = malloc(sizeof(int) * length);  rather than: int *sieve = (int *) malloc(sizeof(int) * length);  Why would this be the case?
107554
Is it possible in C# to have a Struct with a member variable which is a Class type?  If so, where does the information get stored, on the Stack, the Heap, or both?
54729
I'm looking for a way to convert a preprocessor token to a string.  Specifically, I've somewhere got:  #define MAX_LEN 16   and I want to use it to prevent buffer overrun:  char val[MAX_LEN+1]; // room for \0 sscanf(buf, "%"MAX_LEN"s", val);   I'm open to other ways to accomplish the same thing, but standard library only.
33661
What is the 'correct' way to store a native pointer inside a Java object?  I could treat the pointer as a Java int, if I happen to know that native pointers are &lt;= 32 bits in size, or a Java long if I happen to know that native pointers are &lt;= 64 bits in size. But is there a better or cleaner way to do this?  Edit: Returning a native pointer from a JNI function is exactly what I don't want to do. I would rather return a Java object that represents the native resource. However, the Java object that I return must presumably have a field containing a pointer, which brings me back to the original question.  Or, alternatively, is there some better way for a JNI function to return a reference to a native resource?
16535
Is there any good example to give the difference between a struct and a union? Basically I know that struct uses all the memory of its member and union uses the largest members memory space. Is there any other OS level difference?
981
How do I determine the size of my array in C?   That is, the number of elements the array can hold?
8220
I know that the compiler will sometimes initialize memory with certain patterns such as 0xCD and 0xDD.  What I want to know is when and why this happens.  When  Is this specific to the compiler used?  Do malloc/new and free/delete work in the same way with regard to this?  Is it platform specific?  Will it occur on other operating systems, such as Linux or VxWorks?  Why  My understanding is this only occurs in Win32 debug configuration, and it is used to detect memory overruns and to help the compiler catch exceptions.  Can you give any practical examples as to how this initialization is useful?  I remember reading something (maybe in Code Complete 2) saying that it is good to initialize memory to a known pattern when allocating it, and certain patterns will trigger interrupts in Win32 which will result in exceptions showing in the debugger.  How portable is this?
43933
#include &lt;stdio.h&gt; int main() {     unsigned long long int num = 285212672; //FYI: fits in 29 bits     int normalInt = 5;     printf("My number is %d bytes wide and its value is %ul. A normal number is %d.\n", sizeof(num), num, normalInt);     return 0; }   Output:  My number is 8 bytes wide and its value is 285212672l. A normal number is 0.   I assume this unexpected result is from printing the unsigned long long int. How do you printf() an unsigned long long int?
121889
I saw the "new type" BOOL (YES, NO).  I read that this type is almost like a char.  For testing I did :  NSLog(@"Size of BOOL %d", sizeof(BOOL)); NSLog(@"Size of bool %d", sizeof(bool));   Good to see that both logs display "1" (sometimes in C++ bool is an int and its sizeof is 4)  So I was just wondering if there were some issues with the bool type or something ?  Can I just use bool (that seems to work) without losing speed?
7842
I notice that modern C and C++ code seems to use size_t instead of int/unsigned int pretty much everywhere - from parameters for C string functions to the STL.  I am curious as to the reason for this and the benefits it brings.
42535
If I have a class as follows     class Example_Class     {        private:          int x;           int y;         public:           Example_Class()           {               x = 8;              y = 9;          }        ~Example_Class()         { }     };   And a struct as follows  struct {    int x;    int y; } example_struct;   Is the structure in memory of the  example_struct simmilar to that in Example_Class  for example if I do the following  struct example_struct foo_struct; Example_Class foo_class = Example_Class();  memcpy(&amp;foo_struct, &amp;foo_class, sizeof(foo_struct));   will foo_struct.x = 8 and foo_struct.y = 9 (ie: the same values as the x,y values in the foo_class) ?  The reason I ask is I have a C++ library (don't want to change it) that is sharing an object with C code and I want to use a struct to represent the object coming from the C++ library. I'm only interested in the attributes of the object.  I know the ideal situation would be to have Example_class wrap arround a common structure between the C and C++ code but it is not going to be easy to change the C++ library in use.
7720
In C++, sizeof('a') == sizeof(char) == 1. This makes intuitive sense, since 'a' is a character literal, and sizeof(char) == 1 as defined by the standard.  In C however, sizeof('a') == sizeof(int). That is, it appears that C character literals are actually integers. Does anyone know why? I can find plenty of mentions of this C quirk but no explanation for why it exists.