由买买提看人间百态

topics

全部话题 - 话题: struct
1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
s***e
发帖数: 122
1
来自主题: Programming版 - ask a question about struct in C programming
看来是因为这一句:
fread(&sacbin, sizeof(header), 1, fpr);
要改成
fread(&sacbin, sizeof(struct header), 1, fpr);
如果你是用C编译器的话。
当然你也可以定义struct header为typedef struct header {...} HEADER; 那样你就可以用sizeof(HEADER)了。
下面这段代码是可以编译运行的。
#include
struct header {
int hd;
};
struct sac
{
struct header hdr;
float * data;
};
void test2() {
const char * const fn = "test2.txt";
FILE* fpw = fopen(fn, "w");
struct header hdr;
hdr.hd = 18;
fwrite(&hdr, sizeof(hdr), 1, fpw);
fclose(fpw
X****r
发帖数: 3557
2
You need something like "struct X;" or "class X;" in forward declaration.
You also need this struct/class keyword along with the struct/class name
(called elaborated type specifier) if you want to specify a type name
shadowed by a function or variable name, e.g.
struct X {
int a;
};
int X() {
return 0;
}
int g = X(); // calls the function
// The 'struct' keyword is required here.
struct X x;

declare
unless
p***n
发帖数: 635
3
来自主题: DotNet版 - Can a struct be derived? in C#, in C++
in C++, yes, a struct can inherit from another. actually in C++ world, the only
difference btw a struct and a class is that the default access modifiers for
methods are different. for class: private for struct:public
in C#, it is a different story, structs cannot inherit from other structs
however, they can derive from interfaces. it is quite different from class
in C#.
s*******k
发帖数: 20
4
来自主题: DotNet版 - Question about struct
[quote]Within an instance constructor of a struct, "this" corresponds to an
out parameter of the struct type, and within an instance function member of a
struct, "this" corresponds to a ref parameter of the struct type. In both
cases, this is classified as a variable, and it is possible to modify the
entire struct for which the function member was invoked by assigning to "this"
or by passing this as a ref or out parameter.[/quote]
Not quite get it.
Would any DX help with some intances?
Thanks.
f*******n
发帖数: 12623
5
The second person might be confused with something from C. In C, to declare
a variable of a struct type, you have to write "struct MyStruct foo;" unless
you define a typedef. So the keyword "struct" is necessary in the
declaration. However, in C++, "struct" and "class" declare new types
directly, so there is no need to use "struct" or "class" in a declaration.
w*s
发帖数: 7227
6
来自主题: Programming版 - Python: how to do nested struct ?
say in c i have this
struct MyStruct {
struct MyHeader myHeader;
struct MyPayload myPayload;
};
struct MyHeader{
int id;
char name[MAX];
}
struct MyPayload {
int field1;
short field2;
.... // could be very long
}
what's the best ways in python pls ?
s********a
发帖数: 1447
7
来自主题: JobHunting版 - 问个bit struct的面试题 急
请教一下 如何写一个struct
这个struct里面只有 4个项 每个占 1bit
我写了一个
struct {
unsigned char priority :1;
unsigned char nonpriority :1;
unsigned char empty :1;
unsigen char full :1;
}flag;
但是这个占了bit 因为是char
如何写这个struct只占4bit
谢谢
c***k
发帖数: 1589
8
来自主题: Programming版 - 请问可以这样定义struct吗?
想在struct a里定义一个dynamic array of pointers,每个pointer都指向另一个
struct b
typedef struct {
b **p
} a;
typedef struct {
int i;
} b;
那么我以后能不能用这个格式来访问
a *ptr;
ptr->p[3]->i;
s*****w
发帖数: 215
9
来自主题: Programming版 - Marshal C++ struct to C# struct
最近在写一个C#调用C++ 的dll的东西
遇到这样一个struct
struct A
{
unsigned int numConnections;
char** connectionName;
char** connectionDesc;
}
请问在C#里面的对应的struct怎么写呢
c**********e
发帖数: 2007
10
第一个人的回答:
1. Struct has public access by default (class has private access).
2. When you are going to inherit from that class.
第一个人的第一点我明白。但第二点也是谈public/private的问题吗?
第二个人的回答:
Another difference between struct and class can be shown if you try to
declare struct MyClass; or class MyStruct; Both keywords are not
interchangeable in declarations, not just for class/struct definitions.
第二个人的回答是什么意思?
Source:
http://stackoverflow.com/questions/50447/favorite-c-interview-q
X****r
发帖数: 3557
11
For #2, note that this means the struct by default has public base
class(es). It does not mean the struct is a public base class by default
itself. e.g.
class X {};
struct Y : /* public */ X {};
versus
struct A {};
class B : /* private */ A {};
d****n
发帖数: 1241
12
来自主题: Programming版 - 问个c++ struct和指针问题
* 首先,struct的大小依赖于两个因素,一是field的大小,二是field的alignment,
这两个都是由ABI决定的,比如在64位的机器上,如果编译器选择遵守gcc/llvm所支持
的x86_64 ABI标准,那么double是8byte对齐,如果在32位的机器上,linux下gcc支持
的x86 abi是4byte对齐. 大小的话,double类型都是8 bytes. 在缺省情况下,如果某
个field不符合对齐标准,那么编译器会插入padding bytes,让这个field符合对齐的
标准。比如:
struct S {
char f1; // size是1,对齐是1
int f2; // size是4,对齐也是4
};
sizeof(struct S)是8,因为编译器会在f1后,加入3个padding bytes, 让f2符合对齐
的规范。
* 如果在定义struct的时候,添加了类似packed的attribute, 那么编译器会忽略对齐
的要求。
* auto跟动态类型没有关系,在你的例子里,pointer类型是编译器在静态的时候,根
据一定的类型推导规... 阅读全帖
w***g
发帖数: 5958
13
来自主题: Programming版 - size不固定的struct怎么定义呀?
struct Numbers {
size_t N;
float data[1]; /* 有的编译器要求数组长度至少为1 */
};
写的时候先把N写进去,然后fwrite(xx.data, sizeof(float), xx.N, file);
读的时候先把N读出来,然后
xx = (struct Numbers *)malloc(sizeof(struct Numbers) + (N-1) * sizeof(float)
);
然后xx里的data就可以当N个元素的素组用了。
d********i
发帖数: 8
14
这是今天的电话面试问题:
问:
struct test{
char a;
int b;
};
这个STRUCT 多大,我回答是: 假设CHAR 一个字节, INT 4个字节,理论上讲应该5个字节
,但是COMPILER应该会做调整, 有PADDING, 所以很多情况是8个字节. 最好用SIZEOF()
来确定而不是自己来算.
我后来用GCC写了小程序, 是8个字节,不过面试官好象不相信我. 我有说错吗?
p*******n
发帖数: 273
15
来自主题: Programming版 - ask a question about struct in C programming
I want to read data from files, and the format of data is below:
struct sac
{ struct header hdr;
float * data;
} sacbin;
I try fread(&sacbin.hdr,sizeof(struct header),1,fp), but it does not work.
I don't know why. Thank you!
w****h
发帖数: 212
16
运行时出线如3452816845 3452816845 3452816845这种奇怪的大数字
以下我本来用的是int,也有类似的错误。不知道哪里来的。
#define C 10000 //define a fixed maximum capacity for the list that can
be taken away of knapsack
#define N 200 //define number of items for the list
struct body { //define a struct with three parts, the item name, size and
value
unsigned int name;
unsigned int size;
unsigned int value;
};
typedef struct body Plist;
void disp(Plist*, unsigned int);
int _tmain(int argc, _TCHAR* argv[])
{
srand((un
p****s
发帖数: 32405
17
来自主题: Programming版 - typedef struct的问题
问一下, 是不是名字其实也可以取重名?
like this:
typedef struct list {
int val;
struct list* next;
} list;
很多人喜欢写成typedef struct _A
{
...
} A; 只是为了自己看着清楚,但是在C标准里其实写成一样也没事?
l*****d
发帖数: 359
18
来自主题: Programming版 - about struct and class in c++
it seems that struct and class are equivalent in c++ except for that the
default access for the member of a class is private while for that of a
struct is public.
My question is: can a struct inherit from a class, and vise versa?
D***h
发帖数: 183
19
【 以下文字转载自 Java 讨论区 】
发信人: Depth (等待), 信区: Java
标 题: error C2223: left of '->GetEnv' must point to struct/union
发信站: BBS 未名空间站 (Sat Aug 7 13:59:12 2010, 美东)
error C2223: left of '->GetEnv' must point to struct/union
直接copy的代码,应该不可能错,jni.h里面也是把JavaVM定义为struct了,为什么还会
有这样的错?
#include
jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
JNIEnv* env = NULL;
jint result = -1;
if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
return -1;
}
jniRegisterNativeMethods
d****n
发帖数: 1637
20
来自主题: Programming版 - Marshal C++ struct to C# struct
let me try to answer you this.
This would be like, in your example, a struct manage multiple "Connections."
Struct A a;
a.numberConnections=1;
a.ConnectionsName[0]="CName1"; //mem allocation and str copy ommited
a.ConnecttionDesc[0]="first Connections";
When you have more than one connections.
then ;
a.numberConnections++;
a.Connectionsnames[a.numberConnections-1] ="secondName";
a.ConnectionDesc[a.numberConnections-1]="Second Description here";
So char ** here is for maintain multiple c-string... 阅读全帖
A**u
发帖数: 2458
21
第一个人
2。struct所有成员都是public,继承时子类可以访问父类所有成员,和1类似
第2个人
你用class 定义类,那么只能用class声明,
你用struct定义类,那么只能用struct声明
c***m
发帖数: 2630
22
2002 Honda crv才跑不超过60,000迈,dealer说前面漏油了,要换structs。另外,刹
车片也要换。 请问,structs是最好在dealer换吗? 刹车片是不是在carx换比较扁宜
且好?
多谢了!
w******1
发帖数: 520
23
来自主题: JobHunting版 - 问个bit struct的面试题 急
这个问题, 我也很困惑啊。
如果是普通的PC机器, 下面的这个STRUCT 改是多少BIT 和BYTE 呢?
struct {
char priority :1;
int abc :3;
float fff:7;
double ddd:8;
}flag;
d******p
发帖数: 24
24
来自主题: DotNet版 - Can a struct be derived? in C#, in C++
While a struct cannot inherit from another struct, it
can extend a interface. Try to define an interface
for those common properties;
b*********n
发帖数: 1258
25
来自主题: Programming版 - size不固定的struct怎么定义呀?
Ansi C in Unix
我现在有一个struct要存储一些数字
但是有多少个数字是不确定的
需要程序的运行结果来确定
我要把这些数字save到一个file里面
然后另一个程序去open这个file
但是我不知道在存储那个struct的时候
怎么解决未知数目的问题
大家提点建议吧
c********e
发帖数: 383
26
来自主题: Programming版 - size不固定的struct怎么定义呀?
我今天和乐点酒,头脑不是很清楚,凑合看吧。
struct Array
{
unsigned long length_;
int * data_
explicit Array ( const int num) : length_ (num)
{ data = static_castmalloc (sizeof(int)*length);}
~Array (void) {delete data_;}
void init (...) {//initialize ur arrary of int}
int operator [] (int i) { //check boundary maybe; return data[i]; }
private: //or protected
const A& operator=(const A&);
A(const &);
};
或者用vector得乐。要不自己写个templated struct. 还可以用new 来代替malloc.
或者overlaod operator new,或者用allocat
d****i
发帖数: 4809
27
来自主题: Programming版 - size不固定的struct怎么定义呀?
这个应该是用指针来定义动态数组吧,如下
struct Numbers {
size_t N;
float *data;
};
struct Numbers numbers;
//read N in
scanf("%d", &numbers.N);
//create the new data array based on N
number.data = malloc(sizeof(float)*N);
//read data in
for(int i=0; i fscanf(pFile, "%f", &number.data[i]);
}

float)
l*l
发帖数: 26
28
来自主题: Programming版 - how to initialize this struct.
typedef struct __my_long_long{
union{
u_int64_t val;
struct{
u_int32_t low;
u_int32_t high;
};
}my_long_long_t;
my_long_lont_t instance = {
val: 0;
};
// This is reported error. How to initialize the "instance".
Thanks
S*******s
发帖数: 13043
29
来自主题: Programming版 - can struct be derived from?
can struct has child class(struct)?
c***g
发帖数: 472
30
来自主题: Programming版 - one question about struct
I know that struct is identical as class in C++ except the default access
level.
So... the struct has constructor destructor and copy constructor ?
s***e
发帖数: 793
31
来自主题: Programming版 - one question about struct
sure, struct has every thing. virtural function , function overloading ,
operator overloading .
another issue is that the struct inheritance is public inheritance by
default.
h**o
发帖数: 548
32
来自主题: Programming版 - 关于#define 和 struct
假设定义:
typedef struct _icap_flags
{
unsigned int initialized;
......
} icap_flags_t;
typedef struct _Node
{
icap_flags_t icap_flags;
......
} Node;
Node node;
#define icap_initialized icap_flags.initialized
请问能这样用吗?
Node.icap_initialized
还是只能用Node.icap_flags.initialized?
c*****t
发帖数: 1879
33
来自主题: Programming版 - typedef struct的问题
Do something like:
typedef struct list_tag
{
int val;
struct list_tag* next;
} list;
b***y
发帖数: 2799
34
来自主题: Programming版 - [合集] set of struct
☆─────────────────────────────────────☆
campos (campos) 于 (Tue Sep 27 14:42:10 2005) 提到:
typdef struct {
int key;
int data;
} node;
set set1;
现在需要利用set的count/find函数根据输入的key值retrieve到相应的node struct,不
知道该如何实现,请大侠支招,多谢。
☆─────────────────────────────────────☆
alexx (panda in love~八胖~饲羊员~水木十年) 于 (Tue Sep 27 14:48:37 2005) 提到:
is set supposed to do this?why not use a map?


☆─────────────────────────────────────☆
campos (campos) 于 (Tue Sep 27 14:52:29 2005) 提到:
t
o****e
发帖数: 916
35
来自主题: Programming版 - Marshal C++ struct to C# struct
here is an example:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
struct MyStruct
{
public static readonly int SizeOf = Marshal.SizeOf(typeof(MyStruct));
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr,
SizeConst = 256)]
public string name;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.I4)]
public int value;
}
p**o
发帖数: 3409
36
来自主题: Programming版 - Python: how to do nested struct ?
咱先不提"nested" struct,不"nested"的struct你知道怎么在python定义么?
x******a
发帖数: 6336
37
来自主题: Programming版 - 请教struct inside class的问题(C++)
code和编译的错误在下面。
尝试了另外两种方式编译都没有问题:
1. 把struct Obj和double plus(..)定义在class外面
2. 在total()的定义里面,用for loop而不用std::accumulate.
请教怎么回事?谢谢!
#include
#include
#include
class Foo{
struct Obj{
double a;
explicit Obj(const double& a_): a(a_){}
};
double plus(double result, const Obj& obj){
return result+ obj.a;
}
std::vector v;
public:
void append(const double& a){ v.push_back(Obj(a));}
double total() const{
return std::accumulate(v.begin(), v.... 阅读全帖
w*s
发帖数: 7227
38
来自主题: Programming版 - How to do nested struct in Node ?
i'm using struct modeule
https://github.com/xdenser/node-struct
many thanks !
w****q
发帖数: 133
39
来自主题: Automobile版 - 车的front shock 和struct要不要换
大家好,车子是04年suzuki的forenza,开了10万5千迈,修理厂说front shock和
structs要换将近500刀,我开车的时候没什么太大问题,请问需要换么?谢谢大家
d*******a
发帖数: 2336
40
来自主题: Automobile版 - 穷学生破车struct 全破了
前一段时间后轮总是慢慢漏气
半个月就从33降到25
今天来检查也没发现有钉子
记得前几天有人说气门芯也有可能有问题
正在拆下来查
这个struct 不想自己修
先凑合着 可以不修么
j****i
发帖数: 68152
41
来自主题: Automobile版 - 穷学生破车struct 全破了
牛逼 struct 也能破
r****o
发帖数: 1950
42
来自主题: JobHunting版 - 问个bit struct的面试题 急
恩,好像用unsigned int占用空间更大。
问一下,怎么看一个struct 占用了多少bit啊?用sizeof()好像只能看byte。
多谢。
r****o
发帖数: 1950
43
来自主题: JobHunting版 - 问个bit struct的面试题 急
假设一个结构
struct elem{
unsigned int k : 1;
};
怎么测量它的长度呢?
H*X
发帖数: 281
44
来自主题: JobHunting版 - 问个bit struct的面试题 急
一般的cpu框架都是最低支持到byte addressable, 所以一次就是操作一个byte, 你如
果一个struct只想用几个bit, 后边会加上padding bits,筹够是byte的整数
r****o
发帖数: 1950
45
来自主题: JobHunting版 - 问个bit struct的面试题 急
楼主的题目是写个struct只占4个bit,是不是没法实现了?

发帖数: 1
46
还有另一个区别,class可以代替typename, 而struct不行。
知道这些其实也没啥用。
m********8
发帖数: 9
47
struct默认是public,class默认private 面试问这种问题的公司还是不要去了 双向选
m****s
发帖数: 79
48
我也被问过这个问题。
我说我不知道。
我只是简单的只有数据的时候我就用struct,复杂的有member
functions的时候用class.
面试回来上网查到了答案。

发帖数: 1
49
还有另一个区别,class可以代替typename, 而struct不行。
知道这些其实也没啥用。
m********8
发帖数: 9
50
struct默认是public,class默认private 面试问这种问题的公司还是不要去了 双向选
1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)