由买买提看人间百态

topics

全部话题 - 话题: struct
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
Q*******e
发帖数: 939
1
int
queue_is_empty(struct queue_head *q) {
return (stack_is_empty(q->stackA) &&
stack_is_empty(q->stackB));
}
void
enqueue(struct queue_head *q, int value)
{
stack_push(&q->stackB, value);
}
int
dequeue(struct queue_head *q) {
if(queue_is_empty(q)) {
printf("Error: Empty queue\n");
abort();
}
if (stack_is_empty(q->stackA)) {
while(!stack_is_empty(q->stackB)) {
int tmp = stack_pop(&q->stackB);
stack_push(&q->stackA, tmp);
... 阅读全帖
h****n
发帖数: 1093
2
恩 O(n)时间 O(1)空间
typedef struct nodeT{
struct nodeT * next;
struct nodeT * down;
int value;
}node;
void FlattenSinglyList(node * head)
{
node*tail;
node* cur = head;
while(cur->next)
{
cur=cur->next;
}
tail = cur;
cur = head;
while(cur)
{
if(cur->down)
{
appendToTail(cur->down, tail);
}
}
}
void appendToTail(node* down, node* &tail)
{
node* cur = down;
tail->next = cur;
while(cur->next)
... 阅读全帖
d**********x
发帖数: 4083
3
我擦,这是c++的构造函数列表啊
在这里你可以有机会传进成员对象构造的参数
struct B {
B(int i = 0) {}
};
struct A {
A(){
//这里b已经构造
}
B b;
};
struct C {
C() : b(1) /*用自定义的参数构造*/ {
}
B b;
}
有些成员没有缺省构造函数,这里也是唯一可以传进参数的地方,这里也是给父类构造
函数传入参数的地方。。。
w****x
发帖数: 2483
4
做了一个QuadTree
struct PT
{
int x;
int y;
};
struct REC
{
POINT topLft;
POINT bottomRgt;
REC(int a, int b, int c, int d)
{
topLft.x = a;
topLft.y = b;
bottomRgt.x = c;
bottomRgt.y = d;
}
bool inRect(PT pt)
{
return pt.x >= topLft.x && pt.x <= bottomRgt.x
&& pt.y >= topLft.y && pt.y <= bottomRgt.y;
}
bool intersect(REC rect)
{
return min(bottomRgt.x, rect.bottomRgt.x) >= max(topLft.x, rect.
to... 阅读全帖
w****a
发帖数: 710
5
来自主题: JobHunting版 - 被鄙视了, c++基础知识
template
struct Num
{
enum { Result = Num::Result + 1 };
static void print(){
printf("%d ",Result);
Num::print();
};
};
template <>
struct Num<1>
{
enum { Result = 1 };
static void print(){
printf("%d ",Result);
Num<2>::print();
};
};
template <>
struct Num<10>
{
enum { Result = 10 };
static void print(){
printf("%d ",Result);
};
};
////////////
Num<1> x;
x.print();
w****a
发帖数: 710
6
来自主题: JobHunting版 - 被鄙视了, c++基础知识
template
struct Num
{
enum { Result = Num::Result + 1 };
static void print(){
printf("%d ",Result);
Num::print();
};
};
template <>
struct Num<1>
{
enum { Result = 1 };
static void print(){
printf("%d ",Result);
Num<2>::print();
};
};
template <>
struct Num<10>
{
enum { Result = 10 };
static void print(){
printf("%d ",Result);
};
};
////////////
Num<1> x;
x.print();
s****A
发帖数: 80
7
struct ST{
int *ptr;
size_t sz;
};
int main(){
ST* s;
s->sz = 7;
s->ptr = (int *)malloc((s->sz)*sizeof(int));
int allocsize = sizeof(ST) + (s->sz)*sizeof(int);
uint8 * buffer = (uint8 *)malloc(allocsize);
int offset = sizeof(ST);
memcpy(& buffer[offset], s->ptr, s->sz*sizeof(int));
*(int **)&buffer[(char*)&(s->ptr)-(char*)s] = (int *)&buffer[offset];
}
我大概知道好像是要分配一块连续的空间把struct和struct里指针指向的数组内容放在
一起,是这样吗?
但是最后一行code谁能讲一下大概什么意思?
特别是*(int **)&buf[...]和 (char*)&(s->p... 阅读全帖
z****e
发帖数: 54598
8
来自主题: JobHunting版 - 说一下学术派的代码(java)
如果图方便的话,hello world java肯定不如c方便
显然public member在很多时候有明显的缺陷
比如并发的时候,当然自己写的app的话就不需要考虑那么多了
any way,你扣概念木有什么意思,你说的struct就是简单的set/get包装的一个entity咯
表告诉我说entity只有web才有,ejb以前还有entitybean
你说的dto模式,经常被人一捅到底
从web捅到persistence,然后再拔出来
如果你要每一层都做一个值传递的话,会显得效率很低
所以一般只要是同一台jvm,我们就采用同一个set/get实体
而不是拆成一层一层独立的实体,然后互相之间挨个set(get())
当然理论上后者更正确
从mvc上说,model指的就是你说的struct,而不是什么构架层面的东西
每一个模块都要实现自己的mvc,struts里面的action和service都是controller
而struct这个在不同层面有不同的叫法,甚至还有你说的dto
u*****o
发帖数: 1224
9
来自主题: JobHunting版 - 分享NVIDIA的第一轮面试题
话说我前几天去了学校的campus fair, 看到了nvidia的小booth那里在发卷子做题。现
场那个火爆呀,6个座位永远是满满的,很多人站着答,我才知道这家真是popular呀。
我要了一份卷子,发现好几题都不会。。就没交卷,现在和大家分享一下。因为需要我
一点点打字,所以我只写有点难度的吧。
1. what will be printed by the following code
struct Object{
unsigned char x, y, z, w;
};
int main(){
Object obj;
obj.x = 0x11;
obj.y = 0x22;
obj.z = 0x33;
obj.w = 0x44;
char* p = &obj;
for(int i=0; i<4; i++){
printf("%0xn",*((unsigned short*)++p));
}
return 0;
}
话说这能compile吗? char* p = &obj; 这句用cha... 阅读全帖
w*******e
发帖数: 395
10
来自主题: JobHunting版 - G电面的一个题
这道题目非常复杂,首先能想到n*log(n)的算法就不简单,即使想到了,能够在面试的
压力下把逻辑coding出来也非常的难。我感觉电面中,问到这种题目而且要coding的,
如果从没有见过,基本就是不让你过。感觉这种题目适合onsite用。
在知道算法后,我大概花了一些时间,写了如下的代码,基本思路就是楼上讨论的。
1. 首先把所有的interval的起点和终点存进一个vector,并且sort
struct timePoint {
int point;
bool startORend;
int vol;
timePoint(int p, bool s, int v): point(p), startORend(s), vol(v) {}
};
2. 建立一个multiset用来存储还没有结束的volume,同时可以用来判断某个时间段是
否被输入的interval cover了。
3. 然后扫描vector开始建立输出的vector:
1)如果碰到start的点,先检查multiset是否为空,如果是,就是i... 阅读全帖
w*******e
发帖数: 395
11
来自主题: JobHunting版 - G电面的一个题
这道题目非常复杂,首先能想到n*log(n)的算法就不简单,即使想到了,能够在面试的
压力下把逻辑coding出来也非常的难。我感觉电面中,问到这种题目而且要coding的,
如果从没有见过,基本就是不让你过。感觉这种题目适合onsite用。
在知道算法后,我大概花了一些时间,写了如下的代码,基本思路就是楼上讨论的。
1. 首先把所有的interval的起点和终点存进一个vector,并且sort
struct timePoint {
int point;
bool startORend;
int vol;
timePoint(int p, bool s, int v): point(p), startORend(s), vol(v) {}
};
2. 建立一个multiset用来存储还没有结束的volume,同时可以用来判断某个时间段是
否被输入的interval cover了。
3. 然后扫描vector开始建立输出的vector:
1)如果碰到start的点,先检查multiset是否为空,如果是,就是i... 阅读全帖
S*A
发帖数: 7142
12
来自主题: JobHunting版 - G家intern电面新鲜面经
也写来玩玩。
struct node *getNextNode(struct node * node)
{
struct node * next;
assert(node);

if ((next = getFirstChild(node)))
return next;
while (node) {
if ((next = getNextSibling(node)))
return next;
node = getParent(node);
}
return NULL;
}
l******6
发帖数: 340
13
struct node;
void print100(stack& waitStack);
struct childList{
node* curNode;
childList* next;
};
struct node{
void* data;
childList* children;
};
void print(node* root)
{
if(!root)
return;
childList* dummy = new childList();
dummy -> curNode = root;
dummy -> next = NULL;
stack waitStack;
waitStack.push(dummy);
while(!waitStack.empty())
{
print100(waitStack);
}
delete dummy;
}
void print1... 阅读全帖
f**********t
发帖数: 1001
14
来自主题: JobHunting版 - 请教linkedin一个面试题
// Given a doubly linked list with a data item, a previous and a next ptr
along with another pointer "child" that may point to the head of another
doubly linked list of same type(it will lead to a general tree type of
structure) or it may be null. Flatten the tree into a linked list... minimum
space and time complexity(order n). The doubly linked lists's head and tail
are given.
struct List;
struct ListNode {
char val;
ListNode *pre, *next;
List *child;
};
struct List {
ListNode *head, *... 阅读全帖
z***b
发帖数: 127
15
来自主题: JobHunting版 - 感觉今天结结实实被烙印阴了
楼主这个程序签名就不对.应该为
void deleteNode(struct node **head, int data);
或者
struct node *deleteNode(struct node *head, int data);
c*******t
发帖数: 123
16
来自主题: JobHunting版 - 我连把SDET的coding都fail掉了,唉
其实你根本不需要map
你只需要set
set 的每一个元素是
struct page {
page 1;
page 2;
page 3;
occurrence;

bool operator>(struct pageX, struct pageY ){
compare first three elements.
}
}
search set, find max occurrence.
k***g
发帖数: 166
17
来自主题: JobHunting版 - g面经来一个。
struct Node {
int value;
struct Node *next;
struct Node *nested;
};
Node *h = get_list();
if (h->nested) { // then there is a nested list behind this node
h = h->nested;
} else { // otherwise there is no nested list behind it
h = h->next;
}

发帖数: 1
18
来自主题: Joke版 - 求助术版 - 加,减,乘,除
http://rosettacode.org/wiki/24_game/Solve#C
改一下就行了。这是输入4个的,可以改成输入3个的。
6 17 3 7: No solution
……
#include
#include
#include
#define n_cards 4
#define solve_goal 29
#define max_digit 9
typedef struct { int num, denom; } frac_t, *frac;
typedef enum { C_NUM = 0, C_ADD, C_SUB, C_MUL, C_DIV } op_type;
typedef struct expr_t *expr;
typedef struct expr_t {
op_type op;
expr left, right;
int value;
} expr_t;
void show_expr(expr e, op_type prec, int is_r... 阅读全帖
w********n
发帖数: 137
19
来自主题: BuildingWeb版 - 初级问题,web服务器的数据库
struct items{
string type;
integer value;
}
struct record
{
string name;
string address;
list or array of struct items
}
如果搭建一个服务器,有页面让用户提交一些信息,格式如上面定义
的 record 那样,实用什么数据库存储管理比较简单容易?
象查询,导入,导出,
增加、删除 record的 item list(不删除record 本身)
如果目前实用 Google App Engine(python), 但是以后可能使用独立的服务器,
所以希望数据能够容易的import, export.
另外,最好容易通过编辑器手工修改,像 csv xml 文件那样。
本人C/c++ 熟悉,但是网站相关的了解不多。
多谢
s********k
发帖数: 6180
20
【 以下文字转载自 Programming 讨论区 】
发信人: silverhawk (silverhawk), 信区: Programming
标 题: One question about Void pointer
发信站: BBS 未名空间站 (Mon Mar 28 10:32:50 2011, 美东)
Is it risk to use void pointer in the following case:
typedef struct
{
void *next;
UINT16 xx;
UINT16 yy;
byte zz;
} a;
mostly, I think
struct a
{
struct a *next;
UINT16 xx;
UINT16 yy;
byte zz;
};
could be better, but is there any risk to use void pointer? what we should
pay attention in order to carefully manipulate the poin... 阅读全帖
s********e
发帖数: 37
21
来自主题: EmergingNetworking版 - 请教高手: 如何写TCP-client program in C++?
客户端的...免费提供...:)
//
// Client Program: socket_c.c
//
#include
#include
#include
#include
#include
#define SOCKET_PORT 6500
int get_socket_connection()
{
struct sockaddr_in sin;
struct hostent *hp;
int s;
hp = gethostbyname("192.168.0.132");
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
sin.sin_port = htons(SOCKET_PORT);
if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{ perror("socket"); e
q***z
发帖数: 934
22
来自主题: Programming版 - One network C question
Hello I am a newbie on network programming.
I am trying to receive a packet
if((numbytes = recvfrom(udp_fd1, buf, MAXLEN-1, 0,(struct
sockaddr*)®ister_addr, &addr_len))==-1){
fprintf(stderr, "error in recvfrom.\n");
exit(1);
}
The packet I am receiving has the following possible structure.
typedef struct struct_CN
{
unsigned char magicA;
unsigned char magicB;
unsigned short msgLen;
} CN;
typedef struct struct_Cc
{
CN msgHeader;
uns
a**n
发帖数: 97
23
来自主题: Programming版 - Embedded C 编程问题求助
一个源文件同时包含了两个头文件, 调试的时候发现有个定义的变量冲突, 总是通不过
. 因为这个头文件都是公司提供的, 应该不会有问题, 所以请大家帮忙看看可能是什么
问题. 多谢了! 两个头文件相关部分附下.
头文件1
#define PCLKCR *(VOLATILE UNSIGNED16*)0x701C
头文件2
// Peripheral clock control register bit definitions:
struct PCLKCR_BITS {
...
};
union PCLKCR_REG {
Uint16 all;
struct PCLKCR_BITS bit;
};
struct SYS_CTRL_REGS {
...
union PCLKCR_REG PCLKCR; // This caused the problem!!!!
...
}
i****m
发帖数: 15
24
来自主题: Programming版 - 问一个简单的binary tree 问题
How to to find the maximum value in a tree? No special
ordering in the tree. May not be balanced.
struct node {
struct node *left;
struct node *right;
int value;};
Thanks.
k********r
发帖数: 18
25
来自主题: Programming版 - 烤树题。
Write a method: Node* Copy(Node* root)
that takes a pointer to a Node structure as a parameter. The
Node structure contains two pointers to other Node structures. The function
should return a complete copy of the passed-in data structure.
请问是这样做吗?
typedef struct node{
int value;
struct node *child1;
struct node *child2;
}Node;
Node* Copy(Node* root){
Node *n = new Node;
Copywrapper(n, root);
return n;
}
void Copywrapper(Node* n, Node* root){
if(!root) return;
n->v
K****n
发帖数: 5970
26
来自主题: Programming版 - typedef
typedef 把一个struct变成 struct*, 没见过这种用法.
一般都是
typedef struct myStruct{
...;
} myStruct;吧
K****n
发帖数: 5970
27
来自主题: Programming版 - typedef

冲突的原因估计是:
rrc_List 本来是struct名,现在变成了rrc_List*
我试了下(先不typedef成指针),c++里声明struct可以用
rrc_List rrc_list;//绕过 struct rrc_List rrc_list;
现在typedef以后声明
rrc_List不知道是在说指针还是说他自己.
s*****n
发帖数: 461
28
来自主题: Programming版 - 请教一个C的问题
借贴问一个相同问题
我感觉见到的更多的是这样的结构来使用一段dynamic的内存:
struct node{
int i;
int length;
struct node * prev;
struct node * next;
unsigned char data[1];
}
我的问题是,为什么最后一行不写成 unsigned char data?
好像没有什么区别
还有,写成 char data[1]哪里不好?反正使用的时候都需要cast
a**e
发帖数: 5794
29
来自主题: Programming版 - 请教一个命名的问题
有个字符串,可以分解成一些变量,这些变量可以构成一个struct。
以上三者之间的相互转换涉及六种操作,怎样命名这六种操作呢?
字符串和struct之间可以用serialize/deserialize,或者用
marshall/unmarshall,只是拼写太长。
字符串到变量可以用parse,但是反过来用format合适吗?
变量到struct可以用init/create/new,反过来呢?
谢谢!
e****d
发帖数: 895
30
来自主题: Programming版 - a careercup question
An example to use the compiler to do the calculation.
template struct BIT
{
const static int value = ((M >> N) & 0x01) + BIT::value;
};
template struct BIT
{
const static int value = (M & 0x01);
};
template struct CONVERT
{
const static int value = BIT<(A ^ B), (sizeof(A) - 1)>::value;
};
s********k
发帖数: 6180
31
来自主题: Programming版 - One question about Void pointer
Is it risk to use void pointer in the following case:
typedef struct
{
void *next;
UINT16 xx;
UINT16 yy;
byte zz;
} a;
mostly, I think
struct a
{
struct a *next;
UINT16 xx;
UINT16 yy;
byte zz;
};
could be better, but is there any risk to use void pointer? what we should
pay attention in order to carefully manipulate the pointer in this case?
thanks
h**********c
发帖数: 4120
32
来自主题: Programming版 - C++class指针转换
Ok let's just talk about the hole.
the hole can mean in struct, everything is public. first.
second, I didn't test, can you re-write your code with struct?
then try to do sth
void * pt = new bird() etc.
"to openly assign
via a void* (without a cast) is a “hole” in the language "
That is too much detailed. I think in the book, at least, the problem with c
is mentioned -- struct has no protected, private, everything is public.
So my understanding, in C, you have the interface of object, you can ca... 阅读全帖
G*****7
发帖数: 1759
33
yes, because you can use enums (compile time consts) or tag classes in place
of preprocessor defs, and dispatch to os-specific code paths.
no, because your enums or tages will be most likely based on preprocessor #
if.
struct linux_tag {};
struct win32_tag {};
#ifdef LINUX
struct os_tag : linux_tag {};
#...
...
#endif
M*********t
发帖数: 257
34
confused ?
struct sockaddr_in {
short sin_family; // e.g. AF_INET, AF_INET6
unsigned short sin_port; // e.g. htons(3490)
struct in_addr sin_addr; // see struct in_addr, below
char sin_zero[8]; // zero this if you want to
};
unsigned short is 16 bit so port number should be
0~65535
9000 is a good number to use呀 ?
d****i
发帖数: 4809
35
来自主题: Programming版 - 一道很奇怪的面试题
如果只是要返回数组大小的话,可以这样吧:
void getptr(struct ID **ptr_array, int * num_of_array)
{
// some code here
*num_of_array = num();
}
In calling function:
struct ID *array[10];
int length;
getptr(array, &length);
这里假设num()是如下定义:
int num()
{
return sizeof(array)/sizeof(struct ID *);
}

就是
d****i
发帖数: 4809
36
来自主题: Programming版 - 这段C++程序有错吗?
网上看到如下一段C++程序:
#include
#include
#include
using namespace std;
struct node {
int info;
struct node *next;
};
class stack{
struct node *top;
public:
stack();
void push(int);
int pop();
bool isEmpty();
void display();
};
void stack::push(int data){
node *p;
if((p=(node*)malloc(sizeof(node)))==NULL){
cout<<"Memory Exhausted";
exit(1);
}
p = new node;
p->info = data;
p->next ... 阅读全帖
p**o
发帖数: 3409
37
import struct
import socket
def inet_atoi (ipstr):
""" Converts xxx.xxx.xxx.xxx IPv4 string to a 32-bit int.
"""
#Equivalent pure Python implementations:
#lambda ipstr: reduce(lambda x, y: (x << 8) | y,
# map(int, ipstr.split('.')))
#lambda ipstr: long(''.join("%02x" % int(i)
# for i in ipstr.split('.')), 16)
#
return struct.unpack ("!I", socket.inet_aton (ipstr)) [0]
def inet_itoa (ipint):
""" Converts 32-bit int IPv4... 阅读全帖
h*****n
发帖数: 209
38
比如说void *p, 我们可以暂时把它cast成(struct A*)p,然后就可以假定p指向一个A结
构的地址了。但是每次我们这样做都必须加上(struct A*),
指针p本身还是void *的。有没有可能永久性的把p指针变成struct A*类型的呢?
如果C不能,C++是不是可以呢?
j******t
发帖数: 788
39
void *p_v
struct A* p_s
p_s = (struct A*)p_v;
或者还不甘心,
#define (struct A*)p_v p_s;
void *p_v
然后用p_s不就行了?
m**s
发帖数: 97
40
void foo(void *p, int type, int v)
{
switch (type)
{
case 1:
(struct A*)p->value = v;
case 2:
(struct B*)p->value = v;
case 3:
(struct C*)p->value = v;
default:
...
}
}
如果case不多就直接cast了;如果case太多,操作又一样,还是macro
n****1
发帖数: 1136
41
Poor man, you do not have a single clue about the connection between opaque
pointer and opaque struct, do you?
Can you given give me a precise definition of opaque pointer? It is a
pointer to some struct, right? Why should people call it opaque pointer if
it does not point to a opaque struct?
g*****g
发帖数: 34805
42
pointer to struct != struct. Period. Not to mention its definition is not
declared in header => opaque pointer is a pointer to a special form of
struct. How can these two be equal? You are so stupid it's not even funny.
n****1
发帖数: 1136
43
可能因为我opaque见得太多了,以为提以下struct 大家就知道我在说什么. 没说清楚很
抱歉.
在开源项目C中, 基本所有的struct都是以opaque struct形式提供api的
W***o
发帖数: 6519
44
这是一个作业,是应用这个提供的data structure Steque 控制程序里object的流向和
path。
源码贴这里了 http://pastebin.com/VzvcdJKu
我现在对如何使用这个data structure糊涂,从java过来的,不知道在C里面怎么初始
化一个object.
我自己想做一个下面的初始化测试:
typedef struct steque_t steque;
steque *stq;
int i;
for(i = 0; i < 10; i++)
{
steque_item *item = malloc(sizeof(steque_item)); // 这样初始化一个
item
steque_enqueue(stq, item); //然后把item enqueue到stq数据结构里面
}
可是目前这么做的结果是编译时报错:
gcc stequeTest.c -o stequeTest
stequeTest.c: In function ‘main’:... 阅读全帖
W***o
发帖数: 6519
45
来自主题: Programming版 - serialization 到底该怎么理解啊?
最近可能需要写一个serialization/deserialization的code, 是要把一个C里面的
struct保存信息,然后远程传输,再deserialization,运行。比如我的struct是这样:
typedef struct my_data {
char *data;
int *user_array;
int written_by;
int user_array_size;
} my_data;
上面的user_array这个field是一个动态数组,data这个field要保存大量的字符串。请
教一下,该如何serialize/deserialize? 是不是需要用 fopen, fwrite 之类的把 数
据写到一个file里面,利用指针控制fwrite()的位置?
谢谢指教
s*******g
发帖数: 243
46
来自主题: Programming版 - 国内的编程论坛很不自由
编,继续编。
一开始引用你的原文“static_cast不会让你做静态无法保证安全性的转换,比如基类
指针转派生类”。我想看的人都是认为你的意思是编译不过吧。我说code可以编译运行
。你又来“有消除不掉的警告”。我用gcc 4.8.3,-Wall没有任何警告,麻烦你告诉我
你用的什么compiler,平行宇宙的gcc?
还说“必须dynamic_cast才是正路”。你在那本书上看到是必须的?如果没有没有
virtual function,不能用dynamic_cast的时候该用什么?
“蓝翔都知道基类不能转派生类”?举个简单的例子,CRTP模式
http://stackoverflow.com/questions/262254/crtp-to-avoid-dynamic
template
struct base {
void foo() {
static_cast(this)->foo();
};
};
struct my_type : base {
void foo(); // requir... 阅读全帖
p*****2
发帖数: 21240
47
来自主题: Programming版 - go也是三种paradigm混合的语言

就是几种paradigm大混合
比如array, slice, map这些貌似只能写成PP的,
struct可以写成OO的,而且看上边几个go大牛的意思基本也是这么写的
风格很不统一。
fp的人进去就会开始搞fp,只是现在懂fp的还不太多。
我感觉会有两个趋势,
一个是OOP,就是struct,method,interface这些
一个是FP,就是function,array, slice, map, struct, higher order function这些
不过fp的话,go确实还是太弱了,估计fp的人不会喜欢,所以估计主要会是OO,但是里
边要混杂pp,也挺麻烦的,除非array,slice,map这些可以加method。
f****4
发帖数: 1359
48
struct packed_struct packed_data = {0};
packed_data.f1 = 45;
packed_data.f2 = 14;
packed_data.f3 = 0;
char buffer[2];
memcpy( buffer, &packed_data, sizeof(packed_struct));
struct packed_struct *ptr = (struct packed_struct *) buffer;
cout<< sizeof(packed_struct) < cout<< sizeof(buffer) < cout<f1< cout<f2< cout<f3<
r*g
发帖数: 186
49
struct S{
S(int){}
};
struct SS{
int m;
SS(int x): m(x){}
explicit operator S(){return S(m);}
};
void f(S s){}
int main()
{
SS ss(1); // ok; 默认构造函数
S s1 = ss; // 错误; 拷贝构造函数不能使用显式转换
S s2(ss); // ok; 直接构造函数可以使用显式转换 ???
// 为什么? 这里难道不是 S s2(static_cast(ss))吗???
// 而且我的理解这里是先ss转成S型的临时变量
// 然后调用copy constructor
// 为什么说这里是直接构造函数?
f(ss); // 错误; 从SS向S的转换必须是显式的.
// 译注: 强制类型转换也可使... 阅读全帖
a**n
发帖数: 313
50

It seems that what you said is not correct.
tp of course should be ptr to struct timeval.
What you need to do is to allocate memory for *tp,
so after struct timeval *tp ;
add
tp=(struct timeval*)malloc(1);
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)