有这样的用法吗: void *p; p++;
有这样的用法吗?
char *s = "123456";
void *p = s;
p++;
问p加几?
刚才的一个面试题。
问题点数:20、回复次数:37Top
1 楼dzy1982(dislike programming!(神经病帮副帮主))回复于 2006-03-08 16:31:45 得分 0
还有,
struct {
int a;
int c;
char c[];
}d;
求sizeof(d)?Top
2 楼kentxp(kent)回复于 2006-03-08 16:32:15 得分 0
void *p 可以指向任何类型
加几是什么意思? p++不就是遍历这个字符串Top
3 楼jinjiajie(leorio)回复于 2006-03-08 16:38:13 得分 1
第2题应该是4+4+4吧?一个内存地址的大小,其实可以直接告诉他,题目不全,回答不了...呵呵,机器的配置还没说呢Top
4 楼nasdaq7e7(轩辕泽泓)回复于 2006-03-08 16:38:36 得分 0
不同编译器不同啊Top
5 楼jinjiajie(leorio)回复于 2006-03-08 16:40:39 得分 0
汗,直接用第2题的结构体,编译还不通过,有高手指教吗?Top
6 楼bm1408(向va_list学习~不用VC好多年~)回复于 2006-03-08 16:42:17 得分 0
还有,
struct {
int a;
int c;
char c[];//你都不知道长度,编译器如何知道?
}d;
求sizeof(d)?
Top
7 楼kentxp(kent)回复于 2006-03-08 16:45:32 得分 0
char c[];
在CC下编译给一个WARNING 但是这个CHAR []明显是个C风格的字符串 不给维数是否影响大小的初始化Top
8 楼jinjiajie(leorio)回复于 2006-03-08 16:47:00 得分 0
我也是这个意思...刚才看成*c了,呵呵Top
9 楼zez(思恩 闭关练功ing...)回复于 2006-03-08 16:47:17 得分 5
char *s = "123456";
void *p = s;
p++;
问p加几? 加4
struct {
int a;
int c;
char c[];//有的编译器报错,有的编译器为0. 这是一种编程技巧.某些时候必须这样定义
}d;
求sizeof(d)? 8Top
10 楼bm1408(向va_list学习~不用VC好多年~)回复于 2006-03-08 16:47:41 得分 1
char *s = "123456";
void *p = s;
p++;
------
p可以指向 任何类型的数据,但是对它自加运算,我觉得不点不正对了,你没有显示的改变p的类型,
编译器会给你转变吗?
有点变态了~Top
11 楼jinjiajie(leorio)回复于 2006-03-08 16:49:26 得分 1
第一题也是void大小不清楚,vc就直接报错了....其他的估计也要给个warning然后按默认的+1吧Top
12 楼jinjiajie(leorio)回复于 2006-03-08 16:50:13 得分 1
什么公司的招聘题,这家公司出题得人要不是用心良苦就是bt的要命....Top
13 楼flyhly(云飞扬)回复于 2006-03-08 16:54:30 得分 1
char * s = "123456";
void * p = s;
p++;
编译器报错,error C2036: 'void *' : unknown size
int i = 0 ;
struct{
int a;
int b;
char c[];
}d;
i = sizeof(d);
i显示为8Top
14 楼kentxp(kent)回复于 2006-03-08 16:54:44 得分 1
第一题报错?
char *s = "123456";
void *p = s;
p++;
这个应该是没有问题 但是我看不懂你说加几是什么意思????
void *p = s;的时候应该对p进行初始化了 什么改变P的类型Top
15 楼kentxp(kent)回复于 2006-03-08 16:58:24 得分 0
char *s = "123456";
void *p = s;
p++;
。。。。。确实p++的时候编译器报错Top
16 楼ningsheng(闲云野鹤)回复于 2006-03-08 17:26:23 得分 0
void *p = s;
我觉得应该是这样void *p;
(char *)p=s;不知道行不行Top
17 楼david430(馒头)回复于 2006-03-08 19:22:48 得分 0
偶也不是很明白,学习...Top
18 楼zhNKUjw(淡淡的云彩悠悠的游)回复于 2006-03-08 22:23:26 得分 0
奇怪,mark一下,思考中...Top
19 楼popoxx(我笑)回复于 2006-03-09 08:47:08 得分 2
vc7下两个都试过了,第一个是不确定长度
第二个是8。看来char* 变成void*以后vc7不能识别指针指向的对象的类型了。
第二个char c【】的长度为0
Top
20 楼ydfivy(我就是一送外卖的)回复于 2006-03-09 09:16:57 得分 1
指针类型表示的是一个地址覆盖了一块多大的内存空间.
void很显然,编译器不能确定其它类型++是不会有结果的.出错是肯定的.Top
21 楼shxliangs()回复于 2006-03-09 09:57:47 得分 1
在linux 下gcc 3.4.3上,第一题可以编译通过,答案是23456,p自加了1
第二题答案是8,应该sizeof(int)是4,所以char c[]长度是零。
Top
22 楼wqtl_357(Step By Step!)回复于 2006-03-09 11:12:31 得分 0
#include <stdio.h>
int main()
{
char *s = "123456";
void *p = s;
p++; //error : unknown size
printf("%s\n", p);
typedef struct {
int a;
int b;
char c[];
}d;
printf("%d\n", sizeof(d)); // 8,zez(思恩 闭关练功ing...) 说得蛮有道理
return 0;
}
Top
23 楼bbbbcccc()回复于 2006-03-09 11:31:48 得分 0
http://valenhua.go3.icpcn.com/Top
24 楼krfstudio()回复于 2006-03-09 11:39:26 得分 0
有这样的用法吗,void* 不能这么加吧!Top
25 楼sea_sharka_17()回复于 2006-03-09 13:10:58 得分 0
不会吧 答案不会是 报错吧?
void *p;
没有类型,那P以什么字节长加一呢?
char c[];
所有人都知道,在C\C++中这样定义,是一定报错的
结果,无解Top
26 楼firetoucher(风焱)回复于 2006-03-09 13:24:57 得分 5
回复人:dzy1982(dislike programming!(神经病帮副帮主)) ( 二级(初级)) 信誉:100 2006-3-8 16:31:46 得分:0
?
还有,
struct {
int a;
int c;
char c[];
}d;
求sizeof(d)?
-------------------------------
=8
flexible array member是C99加入的特性,相关信息参见C标准6.7.2.1:
16 As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member. With two
exceptions, the flexible array member is ignored. First, the size of the structure shall be equal to the offset of the last element of an otherwise identical structure that replaces the flexible array member with an array of unspecified length.106) Second, when a . (or ->) operator has a left operand that is (a pointer to) a structure with a flexible array member and the right operand names that member, it behaves as if that member were replaced with the longest array (with the same element type) that would not make the structure larger than the object being accessed; the offset of the array shall remain that of the flexible array member, even if this would differ from that of the replacement array. If this array would have no elements, it behaves as if it had one element but the behavior is undefined if any attempt is made to access that element or to generate a pointer one past it.
而楼主的问题在标准中:
17 EXAMPLE Assuming that all array members are aligned the same, after the declarations:
struct s { int n; double d[]; };
struct ss { int n; double d[1]; };
the three expressions:
sizeof (struct s)
offsetof(struct s, d)
offsetof(struct ss, d)
have the same value. The structure struct s has a flexible array member d.Top
27 楼firetoucher(风焱)回复于 2006-03-09 13:41:33 得分 0
sorry,补充,在假设sizeof(int)=4的情况下。Top
28 楼xiaohuoma7620(小火马)回复于 2006-03-09 13:55:22 得分 0
#include "iostream.h"
struct {
int a;
int c;
char c[];
}d;
这样不行,编译报错,有2个c
#include "iostream.h"
struct {
int a;
int c;
char e[];
}d;
这样可以,答案是8Top
29 楼firetoucher(风焱)回复于 2006-03-09 14:14:25 得分 0
有这样的用法吗?
char *s = "123456";
void *p = s;
p++;
问p加几?
刚才的一个面试题。
------------
这操作是错误的,C规定+操作符必须是整数或者a pointer to an object type指向对象类型的指针(而另外一个是整数,对于++就是1)。
operands shall have arithmetic type, or one operand shall be a pointer to an object type and the other shall have integer type. (Incrementing is equivalent to adding 1.)Top
30 楼dfkjewyoldfjkleoe()回复于 2006-03-09 22:15:49 得分 0
www.source520.com 免费免注册80G源码书籍下载Top
31 楼wangmuqq88(挥着翅膀的尼姑)回复于 2006-03-12 00:56:25 得分 0
欢迎加群20641933,欢迎大家共同交流.提高!(不怕不会,就怕不学.)Top
32 楼sinbade(泉)回复于 2006-03-12 09:28:12 得分 0
++的时候要知道++的size大小的,void都不确定的,系统怎么知道++多少地址啊
Top
33 楼zzw820626(偶要分,偶要星星)回复于 2006-03-12 10:25:10 得分 0
char *s = "123456";
void *p = s;
p++;
问p加几?
加4
struct {
int a;
int c;
char c[];//有的编译器报错,有的编译器为0. 这是一种编程技巧.某些时候必须这样定义
}d;
求sizeof(d)? 8Top
34 楼xombat(壞牧羊人)回复于 2006-03-12 10:27:23 得分 0
firetoucher(风焱) ( ) 信誉:100 2006-03-09 13:24:00 得分: 0
good!Mark!Top
35 楼Loveflys()回复于 2006-03-12 17:06:05 得分 0
学习中^_^Top
36 楼wjd7623054(千古风流)回复于 2006-03-12 18:21:05 得分 0
编译可以通过吗?Top
37 楼pengyan(彭岩)回复于 2006-03-12 19:22:32 得分 0
我感到这个问题有不着那么复杂啊,p++=p+sizeof(char),即加上sizeof(char),因为它是个指针啊.一般的指针p+n=p+nsizeof(p所指的元素的类型)
第2个你把长度加上就行了啊,你想啊,计算机连它的长度都不知道怎么分配空间啊.
还有sizeof(d)=sizeof(int)+sizeof(int)+sizeo(char[num]);Top




