I have the following code:  typedef struct {  int a;  int b; } Hello;  typedef struct {  char c;  int d; } World;  Hello hello[10][10]; World world[5];   Then I have the following functions:  void getInfo(Hello hello_p[][10], World* world_p) {      // Here I suppose to get the size of pointer to the array of hello and world      sizeof(hello_p);      sizeof(world_p);    }  void getHello(Hello (** hello_p)[10]) {     hello_p = hello; }  void getWorld(World ** world_p) {     world_p = world; }   then I call the function like:  Hello (*hello_p)[10]      = NULL; World *world_p    = NULL;   getHello(&amp;hello_p); getWorld(&amp;world_p);  getInfo(hello_p, world_p);   then in the getInfo function, can I get the size of the pointer to the array of hello and world?
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)?
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?
80212
For many questions the answer seems to be found in "the standard". However, where do we find that? Preferably online.  Googling can sometimes feel futile, again especially for the C standards, since they are drowned in the flood of discussions on programming forums.  To get this started, since these are the ones I am searching for right now, where are there good online resources for:   C89 C99 C11 C++98 C++03 C++11 C++14 C++17
70797
I was shocked to find out today that C# does not support dynamic sized arrays. How then does a  developer used to using  deal with this in C#?  At the beginning of the function I am not sure of the upper bound of the array. This depends on the rows returned from the database.
73357
On the discussion of dynamic memory here:   The author states:     A memory block like this can effectively be used as a more flexible array. This approach is actually much more common in real-world C programs. It's also more predictable and flexible than a "variable size array"   The type of memory block he is talking about is as such:  const int size = 5; int * array = calloc(size, sizeof(int));   and then using another pointer to iterate over the array:  int * index = array; for (i = 0; i &lt; size; i++) {     *index = 1; // or whatever value     index++; }   My question is how is this method better than a standard variable sized array like this?:  int array[variable];   or dynamic:  char name[] = "Nick";   The author doesn't really shed much light as to why I should prefer the former method to the latter. Or more specifically: How is it more "predictable and flexible"?
93837
I'm looking for a piece of code that can tell me the offset of a field within a structure without allocating an instance of the structure.  IE: given  struct mstct {     int myfield;     int myfield2; };   I could write:  mstct thing; printf("offset %lu\n", (unsigned long)(&amp;thing.myfield2 - &amp;thing));   And get offset 4 for the output. How can I do it without that mstct thing declaration/allocating one?  I know that &amp;&lt;struct&gt; does not always point at the first byte of the first field of the structure, I can account for that later.
2571
What is the correct (most efficient) way to define the main() function in C and C++ — int main() or void main() — and why? And how about the arguments? If int main() then return 1 or return 0?  There are numerous duplicates of this question, including:       — For C++, with a very good answer indeed.     Related:
29551
I've heard that the static_cast function should be preferred to C-style or simple function-style casting.  Is this true?  Why?
84232
Alright, I think we all agree that what happens with the following code is undefined, depending on what is passed,  void deleteForMe(int* pointer) {      delete[] pointer; }   The pointer could be all sorts of different things, and so performing an unconditional delete[] on it is undefined. However, let's assume that we are indeed passing an array pointer,  int main() {      int* arr = new int[5];      deleteForMe(arr);      return 0; }   My question is, in this case where the pointer is an array, who is it that knows this? I mean, from the language/compiler's point of view, it has no idea whether or not arr is an array pointer versus a pointer to a single int. Heck, it doesn't even know whether arr was dynamically created. Yet, if I do the following instead,  int main() {      int* num = new int(1);      deleteForMe(num);      return 0; }   The OS is smart enough to only delete one int and not go on some type of 'killing spree' by deleting the rest of the memory beyond that point (contrast that with strlen and a non-\0-terminated string -- it will keep going until it hits 0).  So whose job is it to remember these things? Does the OS keep some type of record in the background? (I mean, I realise that I started this post by saying that what happens is undefined, but the fact is, the 'killing spree' scenario doesn't happen, so therefore in the practical world someone is remembering.)
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.
1982
The following code is generating a stack overflow error for me  int main(int argc, char* argv[]) {     int sieve[2000000];     return 0; }   How do I get around this? I am using Turbo C++ but would like to keep my code in C  EDIT:  Thanks for the advice.  The code above was only for example, I actually declare the array in a function and not in sub main.  Also, I needed the array to be initialized to zeros, so when I googled malloc, I discovered that calloc was perfect for my purposes.  Malloc/calloc also has the advantage over allocating on the stack of allowing me to declare the size using a variable.
14231
What would be the best way to fill a C# struct from a byte[] array where the data was from a C/C++ struct?  The C struct would look something like this (my C is very rusty):  typedef OldStuff {     CHAR Name[8];     UInt32 User;     CHAR Location[8];     UInt32 TimeStamp;     UInt32 Sequence;     CHAR Tracking[16];     CHAR Filler[12]; }   And would fill something like this:  [StructLayout(LayoutKind.Explicit, Size = 56, Pack = 1)] public struct NewStuff {     [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]     [FieldOffset(0)]     public string Name;      [MarshalAs(UnmanagedType.U4)]     [FieldOffset(8)]     public uint User;      [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]     [FieldOffset(12)]     public string Location;      [MarshalAs(UnmanagedType.U4)]     [FieldOffset(20)]     public uint TimeStamp;      [MarshalAs(UnmanagedType.U4)]     [FieldOffset(24)]     public uint Sequence;      [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]     [FieldOffset(28)]     public string Tracking; }   What is best way to copy OldStuff to NewStuff, if OldStuff was passed as byte[] array?  I'm currently doing something like the following, but it feels kind of clunky.  GCHandle handle; NewStuff MyStuff;  int BufferSize = Marshal.SizeOf(typeof(NewStuff)); byte[] buff = new byte[BufferSize];  Array.Copy(SomeByteArray, 0, buff, 0, BufferSize);  handle = GCHandle.Alloc(buff, GCHandleType.Pinned);  MyStuff = (NewStuff)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(NewStuff));  handle.Free();   Is there better way to accomplish this?    Would using the BinaryReader class offer any performance gains over pinning the memory and using Marshal.PtrStructure?
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.
117196
I was at an interview for a C position in which they presented me with an idiom that I haven't previously encountered. This is a trick that simplifies implementation of various algorithms involving linked lists and I'm wondering if anybody else has encountered this.  Say we have a linked list record defined so:  typedef struct _record {     char* value;     struct _record* next; } record;   We need a function that inserts a new record so that the entire list remains sorted with respect to the value's in the records. The following implementation is simpler than anything I would have used, albeit less readable.  void insert_sorted(record** r, const char* value) {     record* newrec = NULL;     while(*r &amp;&amp; strcmp(value, (*r)-&gt;value) &gt; 0)         r = &amp;((*r)-&gt;next); /* move r to point to the next field of the record */     newrec = malloc(sizeof(record));     newrec-&gt;value = strdup(value);     newrec-&gt;next = *r;     *r = newrec; }   When the function is called, r points to the head pointer of the list. During the while loop, r is updated to point to the next field of the record that comes just before the point where we want to put the new record in. The last line of the function either updates the head pointer of the list (if the insertion happens at the beginning) or the next field of the previous record, which is quite cool.  A couple of questions:   Does this idiom have a name or is it mentioned in any literature? Are there others like it in the C language?   I thought I knew C pretty well and had pointers and indirection pretty well figured out, but this one took me a while to fully understand.
30414
Why can't we initialize members inside a structure ?  example:  struct s {    int i = 10; };
122420
I get a transmission over the network that's an array of chars/bytes. It contains a header and some data. I'd like to map the header onto a struct. Here's an example:  #pragma pack(1)  struct Header {     unsigned short bodyLength;     int msgID;     unsigned short someOtherValue;     unsigned short protocolVersion; };  int main() {     boost::array&lt;char, 128&gt; msgBuffer;     Header header;      for(int x = 0; x &lt; sizeof(Header); x++)         msgBuffer[x] = 0x01; // assign some values      memcpy(&amp;header, msgBuffer.data(), sizeof(Header));      system("PAUSE");          return 0; }   Will this always work assuming the structure never contains any variable length fields? Is there a platform independent / idiomatic way of doing this?  Note:  I have seen quite a few libraries on the internet that let you serialize/deserialize, but I get the impression that they can only deserialize something if it has ben previously serialized with the same library. Well, I have no control over the format of the transmission. I'm definitely going to get a byte/char array where all the values just follow upon each other.
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).