由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 最新某公司onsite面试题 (转载)
相关主题
const char *p, is it ok to change p[1] ?c++ 问题 (转载)
c++ 中如何把str转换为float?大家看看这个简单的qsort排序的问题
a simple C++ questionC++ Q05: pointer to constant variable
C++ formatted output questionarray和pointer在作为函数返回时有啥区别 (C)
c++ 弱问题:static const char* const 这两个const 分别是什么意思?C 语言,初学者,简单问题
free(char *)的问题 (转载)why int** cannot convert to const int** ?
再问一个free()的问题请问const myClass &src 和myClass const &src有什么区别?
怎么得到char *分配空间的大小?问一道brainbench上的问题
相关话题的讨论汇总
话题: char话题: const话题: fred话题: int话题: pointer
进入Programming版参与讨论
1 (共1页)
c*****e
发帖数: 737
1
【 以下文字转载自 JobHunting 讨论区 】
发信人: coollpe (coollpe), 信区: JobHunting
标 题: 最新某公司onsite面试题
发信站: BBS 未名空间站 (Mon Feb 20 04:45:24 2012, 美东)
1,
int a[] = {1,2,3,4,5};
int *p2 = &a + 1;
printf("%d, %d", *(a+1), *(p - 1));
说出结果
2, const char* p;
char* const p;
char const* p;
解释
第三题太长了记不得
4, 如果你在linux下要编译一个项目,但磁盘已经满了,于是你mount了一个win的fs到
你home下,但有个问题,不能soft link,你如何build?
5,用C(不是C++)实现从/etc/resolve.conf下读取所有ip地址,返回char ** dns;
e.g.
mitbbs.com 74.125.78.121
mitbbs.ca 78.45.147.145
...
返回的就是所有ip地址的string array(所以是char **)
p*********t
发帖数: 2690
2
这什么公司啊这是?

【在 c*****e 的大作中提到】
: 【 以下文字转载自 JobHunting 讨论区 】
: 发信人: coollpe (coollpe), 信区: JobHunting
: 标 题: 最新某公司onsite面试题
: 发信站: BBS 未名空间站 (Mon Feb 20 04:45:24 2012, 美东)
: 1,
: int a[] = {1,2,3,4,5};
: int *p2 = &a + 1;
: printf("%d, %d", *(a+1), *(p - 1));
: 说出结果
: 2, const char* p;

J*****n
发帖数: 4859
3

这么C style的东西,多半是bloomberg。

【在 p*********t 的大作中提到】
: 这什么公司啊这是?
p*********t
发帖数: 2690
4
这么纠结于某个细节的,多半不是什么大公司。

【在 J*****n 的大作中提到】
:
: 这么C style的东西,多半是bloomberg。

d****n
发帖数: 1637
5
1. I cant make sure.
but its outputs this
2,5
2. googled
1. const char *p : means p is pointer pointing to a constant char i.e.
you can not change the content of the location where it is pointing but u
can change the pointer itself to point to some other char.
2. char const *p, and char * const p : both are same & in this case p
is a constant pointer poiting to some char location. you can change the
contents of that location but u can't change the pointer to point to some
other location.
3. I dont understand why it can be built, anyone?
4. I am thinking a better solution now...
S*********g
发帖数: 5298
6
第一题编译都过不了
p没定义

e.

【在 d****n 的大作中提到】
: 1. I cant make sure.
: but its outputs this
: 2,5
: 2. googled
: 1. const char *p : means p is pointer pointing to a constant char i.e.
: you can not change the content of the location where it is pointing but u
: can change the pointer itself to point to some other char.
: 2. char const *p, and char * const p : both are same & in this case p
: is a constant pointer poiting to some char location. you can change the
: contents of that location but u can't change the pointer to point to some

c*****e
发帖数: 737
7
p2, 打字大错了。
好奇的是这题为啥g++编译不过,但gcc能编译通过?

【在 S*********g 的大作中提到】
: 第一题编译都过不了
: p没定义
:
: e.

h*******s
发帖数: 8454
8
这些题真bt啊,试着说一下第一个
a是数组,也可以拿来当首个int的地址
&a 是一个指向数组的指针,对它做指针运算加一相当于指向下一个数组的起始位置,
比如3
2位整数的话实际上是a的地址加上20字节啦
然后你把这个东西转化为int*再后退一个,不就是16字节也就是指向5了么
第二个好像说错了吧。。。
第三个和第四个都不大会。。。c的东西忘的差不多了。。。

e.

【在 d****n 的大作中提到】
: 1. I cant make sure.
: but its outputs this
: 2,5
: 2. googled
: 1. const char *p : means p is pointer pointing to a constant char i.e.
: you can not change the content of the location where it is pointing but u
: can change the pointer itself to point to some other char.
: 2. char const *p, and char * const p : both are same & in this case p
: is a constant pointer poiting to some char location. you can change the
: contents of that location but u can't change the pointer to point to some

t****t
发帖数: 6806
9
const char* <===> char const* (pointer to const char)
char* const (const pointer to char)

e.

【在 d****n 的大作中提到】
: 1. I cant make sure.
: but its outputs this
: 2,5
: 2. googled
: 1. const char *p : means p is pointer pointing to a constant char i.e.
: you can not change the content of the location where it is pointing but u
: can change the pointer itself to point to some other char.
: 2. char const *p, and char * const p : both are same & in this case p
: is a constant pointer poiting to some char location. you can change the
: contents of that location but u can't change the pointer to point to some

t****t
发帖数: 6806
10
a has type int[5]
&a has type (pointer to int[5])
&a+1 has type (pointer to int[5])
you assign (pointer to int[5]) to int* and you get an error in c++.
c++ is strong type language, while in c all types of pointer can be
implicitly converted.

【在 c*****e 的大作中提到】
: p2, 打字大错了。
: 好奇的是这题为啥g++编译不过,但gcc能编译通过?

j*******e
发帖数: 674
11
这样面试的公司不能去。去了人就毁了,以后的职业发展没前途。
d****n
发帖数: 1637
12
Sorry something was wrong from googled.
here is my test.
#include
#include
int main(){
char const * p ="Hello";
char q[]="York";
printf("%p\n",p);
p=q; //okay
printf("%p\n",p);
const char *k="two";
printf("%p\n",k);
k=q; // okay
printf("%p\n",k);
char * const s="one";
printf("%p\n",s);
s=q; //segfault
printf("%p\n",s);
}
s****s
发帖数: 50
13
C++ Faq:
[18.5] What's the difference between "Fred const* p", "Fred* const p" and "F
red const* const p"?
You have to read pointer declarations right-to-left.
Fred const* p means "p points to a constant Fred": the Fred object can't be
changed via p.
Fred* const p means "p is a const pointer to a Fred": you can't change the p
ointer p, but you can change the Fred object via p.
Fred const* const p means "p is a constant pointer to a constant Fred": you
can't change the pointer p itself, nor can you change the Fred object via p.
d****n
发帖数: 1637
14
my shabby code for questions 4.
//File resovle.c
#include
#include
#include
#define MAXLINECHARSIZE 1000
inline int fsplit(char *line, char **items, char delim);
int main(int argc, char * argv[]){
/***prepare && allocate buffer/space***/
int n,nbytes=100, maxcol=10, maxchar=1000;
char **items;
{
items=(char **) malloc(maxcol*sizeof(char *));
int i;
for(i=0; i items[i]=(char*)malloc(maxchar*sizeof(char));
}
}
char *buff=(char * )malloc((maxchar*maxcol)*sizeof(char));
/***The core reading and spliting***/
while( (n=getline(&buff, &nbytes, stdin) )>=0 ){
if(buff[0]==';') continue ; //skip comments
int nr=fsplit(buff, items, ' ' );
{
if (nr>=2)
printf("Name\t%s,Address\t%s\n",items[0], items[1]);
}
}

/***destroy each buffer/space***/
free(buff);
{
int i;
for(i=0; i free(items[i]);
}
free(items);
}
}
inline int fsplit(char *line, char **items, char delim)
{
size_t i, j, k, n, lsz, ns;
ns = 0;
lsz = strlen(line);
if (lsz > MAXLINECHARSIZE) {
return -1;
} else if (lsz == 0) {
return 0;
}
while (line[lsz - 1] == '\n' || line[lsz - 1] == delim)
lsz--; // chop off the last new line character
k = 0;
while (line[k] == delim)
k++;
for (i = k, j = 0, n = 0; i < lsz;) {
char c = line[i];
if (c != delim) {
items[j][n++] = c;
i++;
} else {
items[j][n] = '\0';
n = 0;
j++;
while (line[i] == delim)
i++;
}
}
items[j][n] = '\0';
return j + 1;
}
#####
gcc resovle.c -o res
./res ###########input file##############
; generated by /sbin/dhclient-script
domain XXXXX.com
nameserver 192.168.1.101
nameserver XX.XX.XX.90
nameserver XX.XX.XX.20
########## output ##################
Name domain,Address XXX.com
Name nameserver,Address XX.XX.XX.101
Name nameserver,Address XX.XX.XX.90
Name nameserver,Address XX.XX.XX.20
######end of output######
welcome to make any comments.
1 (共1页)
进入Programming版参与讨论
相关主题
问一道brainbench上的问题c++ 弱问题:static const char* const 这两个const 分别是什么意思?
谁给解释一下这个c questionfree(char *)的问题 (转载)
数组指针的问题再问一个free()的问题
问一个 char * 和 char [] 的问题怎么得到char *分配空间的大小?
const char *p, is it ok to change p[1] ?c++ 问题 (转载)
c++ 中如何把str转换为float?大家看看这个简单的qsort排序的问题
a simple C++ questionC++ Q05: pointer to constant variable
C++ formatted output questionarray和pointer在作为函数返回时有啥区别 (C)
相关话题的讨论汇总
话题: char话题: const话题: fred话题: int话题: pointer