Let's say that I want to get the size in bytes or in characters for the name field from:
struct record { int id; TCHAR name [50]; };
sizeof(record.name) does not work.
sizeof(record.name)
The solution for this is not as pretty as you think:
size_in_byte = sizeof(((struct record *) 0)->name)
size_in_chars = _countof(((struct record *) 0)->name)
If you want to use the second one on platforms other than Windows, follow these steps:
#define _countof(array) (sizeof(array)/sizeof(array[0]))
If you first create an instance, it will work.
record r; sizeof(r.name);
++:
#include <iostream> using namespace std;; struct record { int id; char name [50]; }; int main() { cout << sizeof( record::name) << endl; }
:. , ++ 0x, , , V++. , - ++-, , sizeof ++ 03? , . , . ++: -)
record - , record.name - . - . - C:
record
record.name
sizeof ((struct record*)0)->name;
- ( -) struct record, name sizeof. , sizeof , .
struct record
name
sizeof
, , , .
struct record { static const int kMaxNameChars=50; int id; TCHAR name [kMaxNameChars]; }; sizeof(TCHAR)*record::kMaxNameChars //"sizeof(record.name)" //record::kMaxNameChars sufficient for many purposes.
, IMO, , .
(edit: you may need a C macro if the compiler is upset about the length of the array of variables. If you do, consider defining a static int constant for the macro value anyway!)
Source: https://habr.com/ru/post/1725301/More articles:JSP template implementation (composite view template) - javaSQL searches for multiple fields, returns one - sqlUITableView in UINavigationController falls under navigation bar after rotation - uitableviewHow to create a demo such as Convertbot - iphonejQuery cancel and reset slide animations - jqueryPHP preg-match_all calling Apache Segfault - segmentation-faultVisual Studio IDE Development - asp.netSkip play / search MP4 file - iphoneRuby / Rails: get elements from an array where indices are divided by x - arrayshttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1725306/can-i-add-mvc-2-dataannotation-attributes-to-existing-properties&usg=ALkJrhjUv54E77bFR1n3ZAeUxf0iCWXRtwAll Articles