static-array

Difference between char and char[1]

假装没事ソ 提交于 2019-12-18 10:37:31
问题 In C++ what is the difference (if any) between using char and char[1]. examples: struct SomeStruct { char x; char y[1]; }; Do the same reasons follow for unsigned char? 回答1: The main difference is just the syntax you use to access your one char. By "access" I mean, act upon it using the various operators in the language, most or all of which do different things when applied to a char compared with a char array. This makes it sound as if x and y are almost entirely different. If fact they both

static arrays defined with unspecified size, empty brackets?

牧云@^-^@ 提交于 2019-12-18 04:44:13
问题 For the C++ code fragment below: class Foo { int a[]; // no error }; int a[]; // error: storage size of 'a' isn't known void bar() { int a[]; // error: storage size of 'a' isn't known } why isn't the member variable causing an error too? and what is the meaning of this member variable? I'm using gcc version 3.4.5 (mingw-vista special) through CodeBlocks 8.02. On Visual Studio Express 2008 - Microsoft(R) C/C++ Optimizing Compiler 15.00.30729.01 for 80x86, I got the following messages: class

how to extract the name attribute from string array?

时光怂恿深爱的人放手 提交于 2019-12-17 11:50:06
问题 Hi I build a quiz application. I have the following (values/)question.xml <?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="question"> <item name="correct">A</item> <item name="wrong">B</item> <item name="wrong">C</item> <item name="wrong">D</item> </string-array> </resources> I would like to have a question with four possible answers but when i retrieve my answers in Java.. I don't know which answer is correct. So I decided to use name attribute in the item tags to pass

A better way to initialize a static array member of a class in C++ ( const would be preferred though )

点点圈 提交于 2019-12-12 01:59:06
问题 I have a static array of pointers to functions as a member of a class. I need to initialize it, but it turns out this array is 64K items long, so it's impractical to initialize it with a static initializer like { x, y, z, ... } as it would clutter code. I have instead to initialize it by code, with several loops. The way I figured to do this is by initializing the static array in the constructor and setting a flag to it, so only the construction of the first instance of the class would fire

java null pointer exception with static array

独自空忆成欢 提交于 2019-12-11 05:02:23
问题 I got a null pointer exception when accessing a static array from a static member method. The exception is thrown when i call setData(x, y, z) from a thread. When I debugged it I found out data[0] is null when i try to write to it. I just don't understand how it can be null public class dataContainer { private static final short nrOfDataElements = ids.total_ids; private static regularDataElement[] data = new regularDataElement[nrOfDataElements]; public static synchronized void getData(final

Java: Generic Static Multidimensional Arrays

帅比萌擦擦* 提交于 2019-12-11 04:35:37
问题 If it is possible, how can I create a static multidimensional array in Java with different primitive datatypes per dimension? By static, I mean the primitive array that is not dynamic like an ArrayList would be. 回答1: Dimensions in an Array are always from type int. Think about it! int a = 4; int b = 5; Shoe shoe = new Shoe (Color.RED, 42, "Leather"); Hat hat = new Hat (17, Color.Black); Foo foo = foos[a][b]; Zilch pop = bars[shoe][hat]; // no go If you have a multidimensional array of Foos,

static array class variable “multiple definition” C++

痴心易碎 提交于 2019-12-01 04:40:39
I'm writing some code where I need to have a class variable that's a static int array. I understand that I can do this with something like this in the header file, A.h: #ifndef A_H_ #define A_H_ class A { public: static const int a[]; }; const int A::a[] = {1,2}; #endif This works just fine if I'm then including this header in only one other file, something like the following, main.cpp: #include "A.h" #include <iostream> using namespace std; int main() { A myA; cout << "0: " << myA.a[0] << endl; cout << "1: " << myA.a[1] << endl; } But suppose I need my class A to be a bit more complicated,

static array class variable “multiple definition” C++

送分小仙女□ 提交于 2019-12-01 03:05:56
问题 I'm writing some code where I need to have a class variable that's a static int array. I understand that I can do this with something like this in the header file, A.h: #ifndef A_H_ #define A_H_ class A { public: static const int a[]; }; const int A::a[] = {1,2}; #endif This works just fine if I'm then including this header in only one other file, something like the following, main.cpp: #include "A.h" #include <iostream> using namespace std; int main() { A myA; cout << "0: " << myA.a[0] <<

Count the number of elements in an array in C [duplicate]

这一生的挚爱 提交于 2019-11-30 14:57:09
This question already has an answer here: size of array in c 6 answers How can I obtain the number of elements present in an integer array in C after the array is passed to a function? The following code doesn't work. size=sizeof(array)/sizeof(array[0]); In C, you can only get the size of statically allocated arrays, i.e. int array[10]; size = sizeof(array) / sizeof(int); would give 10. If your array is declared or passed as int* array , there is no way to determine its size, given this pointer only. You are most likely doing this inside the function to which you pass the array. The array