I am creating two structs in C that have the same number of variables: 3 integers and 3 chars. When i look at the size of an instance of each of the structs using the sizeof() operator i get two different values. I wonder Why is this happening, here is my code: I tried rearranging the variable with the same result.  typedef struct a { int x; char a; int y; char b; int z; char c; }s1;  typedef struct b { int x; int y; int z; char a; char b; char c; }s2;  int main() { printf("%d %d\r\n", sizeof (s1), sizeof (s2)); return 0; }
1450
Why does the sizeof operator return a size larger for a structure than the total sizes of the structure's members?
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?
59948
I originally had an array[1..1000] that was defined as a global variable. But now I need that to be n, not 1000 and I don't find out n until later. I know what n is before I fill the array up but I need it to be global therefore need a way to define the size of a global array at run time.  Context is filling an array with a linear transformation of the bytes in a file.  I don't know how big the file is until someone wants to open it and the files can be of any size.
52593
Yesterday, I found myself writing code like this:  SomeStruct getSomeStruct() {     SomeStruct input;      cin &gt;&gt; input.x;     cin &gt;&gt; input.y; }   Of course forgetting to actually return the struct I just created.  Oddly enough, the values in the struct that was returned by this function got initialized to zero (when compiled using g++ that is).  Is this just a coincidence or did another SomeStruct get created and initialized somewhere implicitly?
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?
69127
I'm looking for detailed information regarding the size of basic C++ types. I know that it depends on the architecture (16 bits, 32 bits, 64 bits) and the compiler.  But are there any standards for C++?  I'm using Visual Studio 2008 on a 32-bit architecture. Here is what I get:  char  : 1 byte short : 2 bytes int   : 4 bytes long  : 4 bytes float : 4 bytes double: 8 bytes   I tried to find, without much success, reliable information stating the sizes of char, short, int, long, double, float (and other types I didn't think of) under different architectures and compilers.
680
First off, here is some code:  int main()  {     int days[] = {1,2,3,4,5};     int *ptr = days;     printf("%u\n", sizeof(days));     printf("%u\n", sizeof(ptr));      return 0; }   Is there a way to find out the size of the array that ptr is pointing to (instead of just giving its size, which is four bytes on a 32-bit system)?
18083
I have seen many programs consisting of structures like the one below  typedef struct  {     int i;     char k; } elem;  elem user;   Why is it needed so often? Any specific reason or applicable area?
57276
I don't truly understand some basic things in C like dynamically allocating array of arrays. I know you can do:  int **m;   in order to declare a 2 dimensional array (which subsequently would be allocated using some *alloc function). Also it can be "easily" accessed by doing *(*(m + line) + column). But how should I assign a value to an element from that array? Using gcc the following statement m[line][column] = 12; fails with a segmentation fault.  Any article/docs will be appreciated. :-)
12339
What is the difference between a[bc]d and a{b,c}d? Why do people use a{b,c}d when there is already a[bc]d?
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?
13053
Why do most C programmers name variables like this:  int *myVariable;   rather than like this:  int* myVariable;   Both are valid. It seems to me that the asterisk is a part of the type, not a part of the variable name. Can anyone explain this logic?
40847
I can create a literal long by appending an L to the value; why can't I create a literal short or byte in some similar way? Why do I need to use an int literal with a cast?  And if the answer is "Because there was no short literal in C", then why are there no short literals in C?  This doesn't actually affect my life in any meaningful way; it's easy enough to write (short) 0 instead of 0S or something. But the inconsistency makes me curious; it's one of those things that bother you when you're up late at night. Someone at some point made a design decision to make it possible to enter literals for some of the primitive types, but not for all of them. Why?
116782
Possible Duplicate:          see this code    #define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))     int array[] = {23,34,12,17,204,99,16};     int main()     {         int d;         for(d=-1;d &lt;= TOTAL_ELEMENTS-2;d++)            printf("%d\n",array[d+1]);         return 0;    }    now this loop won't run. sizeof() would return an unsigned value so TOTAL_ELEMENTS has an unsigned value. now , coming to the for loop, please tell me if the unary operator '-' works on signed int 2 or an implicit conversion takes place into unsigned and then the '-' operator works.
5742
I have a large array in C (not C++ if that makes a difference). I want to initialize all members of the same value.  I could swear I once knew a simple way to do this. I could use memset() in my case, but isn't there a way to do this that is built right into the C syntax?
112071
We had a discussion here at work regarding why fread() and fwrite() take a size per member and count and return the number of members read/written rather than just taking a buffer and size. The only use for it we could come up with is if you want to read/write an array of structures which aren't evenly divisible by the platform alignment and hence have been padded but that can't be so common as to warrant this choice in design. From :  The function fread() reads nmemb elements of data, each size bytes long, from the stream pointed to by stream, storing them at the location given by ptr.   The  function  fwrite()  writes  nmemb elements of data, each size bytes long, to the stream pointed to by stream, obtaining them from the location given by ptr.   fread() and fwrite() return the number of items successfully read or written (i.e., not the number of characters).  If an error occurs, or  the end-of-file is reached, the return value is a short item count (or zero).
68068
How can I find out the size of a file I opened with an application written in C ?  I would like to know the size, because I want to put the content of the loaded file into a string, which I allocate using malloc(). Just writing malloc(10000*sizeof(char)); is IMHO a bad idea.