帮助无助..代码阅读 [英] Help the helpless.. code read

查看:51
本文介绍了帮助无助..代码阅读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编程好多年了,但最近才拿了一个

深的C潜水(坏双关语,我知道)并需要来自

专家的大量解释。我的问题围绕那些神秘的指针怪。


以下代码摘自digitemp,这是一个读取
单线设备的程序。 Digitemp是Brian C. Lane的作品。


当我浏览这段代码时,我有很多问题。我希望

有指针经验的人可以为我提供一些(另一个

双关语):)另外,我很抱歉这条消息很长。我试图将b $ b缩短它,但无法通过任何其他方式解决我的问题。


我缩进了代码,我的问题和评论先于

//所以他们会更容易理解。我希望这不会让人感到困惑,并且b / b
尊重每个人的时间。

//以下_roms结构包含一个8字节地址列表...


struct _roms

{

unsigned char * roms; / * 8字节数组* /

int max; / *最大数量* /

};


//变量,sensor_list已初始化...


struct _roms * sensor_list

//结构的roms部分用数组填充各种各样,

//包含一个或多个8字节地址。打印时,这些地址解析为
//为十六进制值。


//例如:" 12BAA91C000000A3"是一个有效的地址字符串。


// _roms结构中的另一个变量是max这是

//加载了存储在roms中的地址总数。


//据我了解,* sensor_list如上所定义是一个指向

//开头的整个结构定义为_roms。


//这是正确的吗?

//让我们说sensor_list加载了一个8字节宽的地址:

//" 12BAA91C000000A3"。


//这是如何在代码中传递:


read_all(& sensor_list);


//上面,read_all(& sensor_list)说调用read_all函数和

//作为参数传递(起始)地址,其中变量

// sensor_list是。这是正确的读数吗?


int read_all(struct _roms * sensor_list)

{

int x;

for(x = 0; x<(num_cs + sensor_list-> max); x ++)

{

read_device(sensor_list,x); < br $>
}

返回0;

}


//看来是一个取消引用的指针在函数头中定义了sensor_list是否为
//,对吗? (由于

// sensor_list的地址是在read_all(& sensor_list)的调用中传递的。)


//从这里,它是否正确假设不需要进一步取消引用

// sensor_list?


//这里是行动发生的地方:


int read_device(struct _roms * sensor_list,int sensor)

{

...

}


//对我来说似乎很奇怪的是,另外,sensor_list在

//中被定义为此函数头作为解除引用的指针。然而,调用

//函数使用了一个已经被取消引用的变量(sensor_list)

//。这就是为什么它只用

//sensor_list来调用read_device,对吗?


//所以这里有一个根本就是我不理解。


//在read_device中,sensor_list结构用于将

//完整的8字节地址传递给名为owSerialNum的函数:


owSerialNum(0,& sensor_list-> roms [sensor * 8],FALSE);


//如果我调用函数像这样:


owSerialNum(0," 12BAA91C000000A3",FALSE);


//它完美无缺。 owSerialNum的文档说

//第二个参数是一个包含地址的缓冲区。


//我可以看到& sensor_list-> roms [sensor * 8]在我们称之为结构的结构中说roms [从这个偏移处开始]的地址

//

//" sensor_list" 。


//由于

中的第二个参数// owSerialNum(0,12BAA91C000000A3,FALSE);作为

//缓冲区的起始地址传递,并且因为& sensor_list-> roms [sensor * 8]

//也是地址一个缓冲区,它们必须是同义词,对吗?


//好吧,它们似乎不是这样。

//以下代码有效:


printf("地址:%s"," 12BAA91C000000A3");


//这会打印垃圾:


printf("地址:%s",& sensor_list-> roms [sensor * 8]);


//实际上,它看起来像它正在打印一个内存地址而不是该地址的

//内容。为什么?


//如果我这样做...


printf(传感器在这里:%s \ n,传感器列表 - > roms [sensor * 8]);


//我遇到分段错误(崩溃)。

//如果我这样做:


printf(传感器在这里:%s \ n,* sensor_list-> roms [sensor * 8]);


//编译器抱怨:'unary *'的无效类型参数

*********


我最终想做什么将一个地址作为

a字符串参数传入应用程序,并将其传递给read_device

函数,以便将其正确传递给owSerialNum()。看起来很简单,但是我现在还挂了上述问题。如果我可以通过泥浆引导
,这一次,我想我会拥有它。

帮助!

解决方案

Mark Richards写道:


.... snip ...
我缩进了代码和我的问题//之前的评论是
,因此更容易理解。我希望这不会让每个人的时间变得混乱和尊重。

//以下_roms结构包含一个8字节
地址列表...

struct _roms
{
unsigned char * roms; / * 8字节数组* /
int max; / *最大数量* /
};




让我们停在那里,因为其余的假设将是

错了。 roms是指向unsigned char的指针。它不是任何类型的数组

。我不再读了。


此外,标识符''_roms''是为

实现保留的。你不应该使用它。这样的使用可能导致

未定义的行为。


-

答:因为它会破坏人们通常阅读文本的顺序。

问:为什么顶级发布这么糟糕的事情?

A:热门发布。

问:什么是最烦人的usenet和电子邮件中的东西?


Mark Richards写道:

我已编程多年,但最近才开始编程采取了深刻的C潜水(坏双关语,我知道)并且需要来自
专家的大量解释。我的问题围绕那些神秘的指针怪。


我会处理你问题的一小部分。

//以下_roms结构包含一个8字节地址列表...

struct _roms
{
unsigned char * roms; / * 8字节数组* /
int max; / *最大数量* /
};

//变量,sensor_list已初始化...

struct _roms * sensor_list


已注册。虽然这条线不是C的原样。它是
需要一个分号作为终止,可能在

实际初始化之前。

//据我所知,* sensor_list as上面定义的是指向整个结构定义为_roms的
//开头的指针。


不完全:sensor_list被声明为具有类型,指向

struct _roms的指针。 " * sensor_list",这是你得到的东西

dereference sensor_list,然后有类型struct _roms。

//以下是它的传递方式在代码中:

read_all(& sensor_list);

//上面,read_all(& sensor_list)说调用read_all函数和
//作为参数传递(起始)地址变量
// sensor_list是。这是正确的阅读吗?


是的,这是正确的。

int read_all(struct _roms * sensor_list)




停在那儿。除非您的报价中有一些滑点来自原始C源,否则这是一个错误。 " sensor_list"是

类型指向struct _roms的指针。通过写作获得的东西

"& sensor_list"然后是类型指向struct _roms的指针,

或struct _roms **。这与read_all()所声明的

参数类型不兼容。


Allin Cottrell

维克森林大学


>此外,标识符''_roms''保留用于

实现。你不应该使用它。这种使用可能会导致未定义的行为。


当你说它是为实现而保留时,你的意思是它可能是一个保留字,或者是下划线应该是b / b不能使用?

roms是指向unsigned char的指针。



If我定义:


struct _roms * sensor_list

然后变量sensor_list充当指向结构的指针,对吗?



CBFalconer写道:Mark Richards写道:

... snip ...

我缩进了代码我的问题和评论以//开头,因此更容易理解。我希望这不会让每个人的时间变得混乱和尊重。

//以下_roms结构包含一个8字节
地址列表...

struct _roms
{
unsigned char * roms; / * 8字节数组* /
int max; / *最大数量* /
};



让我们停在那里,因为其余的假设将是错误的。 roms是指向unsigned char的指针。它不是任何类型的数组。我不再读了。



I''ve been programming for many years, but have only recently taken a
deep "C" dive (bad pun, i know) and need a lot of explanation from an
expert. My questions center around those mysterious pointer beasties.

The following code is excerpted from digitemp, a program that reads
1-wire devices. Digitemp is the work of Brian C. Lane.

As I walk through this code, I have a number of questions. I''m hoping
someone with experience in pointers can provide a few for me (another
pun) :) Also, I am sorry that this message is long. I tried to
shorten it, but could not get my questions across any other way.

I have indented the code and my questions and comments are preceded by
// so they will be easier to follow. I hope this is less confusing and
respectful of everyone''s time.
// The following _roms structure contains a list of 8 byte addresses...

struct _roms
{
unsigned char *roms; /* Array of 8 bytes */
int max; /* Maximum number */
};

// A variable, sensor_list is initialized...

struct _roms *sensor_list

// The roms part of the structure is filled with an "array" of sorts,
// containing one or more 8-byte addresses. These addresses resolve,
// when printed, as hex values.

// For example: "12BAA91C000000A3" is a valid address string.

// The other variable within the _roms structure is "max" which is
// loaded with the total number of addresses stored in "roms".

// As I understand it, *sensor_list as defined above is a pointer to the
// beginning of the entire structure defined as _roms.

// Is this correct?
// Let''s say sensor_list is loaded with one 8-byte-wide address:
// "12BAA91C000000A3".

// Here is how it''s passed through in the code:

read_all(&sensor_list);

// Above, read_all(&sensor_list) says "call the read_all function and
// pass as a parameter the (starting) address where the variable
// sensor_list is. Is this a correct reading?

int read_all(struct _roms *sensor_list)
{
int x;
for(x = 0; x < (num_cs + sensor_list->max); x++)
{
read_device(sensor_list, x);
}
return 0;
}

// It appears that a de-referenced pointer to sensor_list is being
// defined in the function header, correct? (Since the address of
// sensor_list was passed in the call to read_all(&sensor_list).)

// From this, is it correct to assume that further de-referencing of
// sensor_list will not be needed?

// Here''s where the action takes place:

int read_device(struct _roms *sensor_list, int sensor)
{
...
}

// What seems odd to me is that, again, sensor_list is being defined in
// this function header as a dereferenced pointer. Yet, the calling
// function used a variable (sensor_list) that had already been
// de-referenced. That''s why it called read_device with just
// "sensor_list", correct?

// So there is a fundamental here that I am not understanding.

// Within read_device, the sensor_list structure is used to pass a
// complete 8-byte address to a function called owSerialNum:

owSerialNum( 0, &sensor_list->roms[sensor*8], FALSE );

// If I call the function like this:

owSerialNum( 0, "12BAA91C000000A3", FALSE );

// it works perfectly. The documentation for owSerialNum says that the
// second parameter is a buffer containing the address.

// I can see that &sensor_list->roms[sensor*8] is saying, "the address
// of roms[beginning at this offset] within a structure we call
// "sensor_list".

// Since the second parameter in
// owSerialNum( 0,"12BAA91C000000A3",FALSE); is passed through as the
// starting address of a buffer, and since &sensor_list->roms[sensor*8]
// is also the address of a buffer, they must be synonymous, correct?

// Well, they appear not to be so.
// The following code works:

printf(" Address: %s","12BAA91C000000A3");

// This prints garbage:

printf(" Address: %s",&sensor_list->roms[sensor*8] );

// In fact, it looks like it''s printing a memory address instead of the
// contents of that address. Why?

// If I do this...

printf("Sensor here: %s\n",sensor_list->roms[sensor*8]);

// I get a segmentation fault (crash).
// If I do this:

printf("Sensor here: %s\n",*sensor_list->roms[sensor*8]);

// The compiler complains: invalid type argument of `unary *''
*********

What I eventually want to do is pass into the application an address as
a string parameter, and be able to pass it into the read_device
function so that it is properly passed to owSerialNum(). It seems
simple enough, but I''m hung up on the above issues at the moment. If I
can be guided through the mud, this once, I think I''ll have it.
Help!

解决方案

Mark Richards wrote:

.... snip ...
I have indented the code and my questions and comments are
preceded by // so they will be easier to follow. I hope this is
less confusing and respectful of everyone''s time.

// The following _roms structure contains a list of 8 byte
addresses...

struct _roms
{
unsigned char *roms; /* Array of 8 bytes */
int max; /* Maximum number */
};



Let''s stop right there, as the rest of the assumptions will be
wrong. roms is a pointer to an unsigned char. It is not an array
of any sort. I read no further.

In addition the identifier ''_roms'' is reserved for the
implementation. You should not be using it. Such use may cause
undefined behaviour.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Mark Richards wrote:

I''ve been programming for many years, but have only recently taken a
deep "C" dive (bad pun, i know) and need a lot of explanation from an
expert. My questions center around those mysterious pointer beasties.
I''ll tackle a small portion of your question.
// The following _roms structure contains a list of 8 byte addresses...

struct _roms
{
unsigned char *roms; /* Array of 8 bytes */
int max; /* Maximum number */
};

// A variable, sensor_list is initialized...

struct _roms *sensor_list
Registered. Although this line is not valid C as it stands. It
needs a semi-colon as termination, possibly preceded by an
actual initialization.
// As I understand it, *sensor_list as defined above is a pointer to the
// beginning of the entire structure defined as _roms.
Not quite: sensor_list is declared as having the type, "pointer to
struct _roms". "*sensor_list", that is the thing you get when you
dereference sensor_list, then has type struct _roms.
// Here is how it''s passed through in the code:

read_all(&sensor_list);
// Above, read_all(&sensor_list) says "call the read_all function and
// pass as a parameter the (starting) address where the variable
// sensor_list is. Is this a correct reading?
Yes, it''s correct.
int read_all(struct _roms *sensor_list)



Stop right there. Unless there''s some slippage in your quotation
from the original C source, this is an error. "sensor_list" was
of type "pointer to struct _roms". The thing you get by writing
"&sensor_list" is then of type "pointer to pointer to struct _roms",
or struct _roms **. And this is not compatible with the stated
parameter type for read_all().

Allin Cottrell
Wake Forest University


> In addition the identifier ''_roms'' is reserved for the

implementation. You should not be using it. Such use may cause
undefined behaviour.
When you say it''s reserved for the implementation, do you mean that it''s
possibly a reserved word, or that the underscore should not be used?

roms is a pointer to an unsigned char.


If I define:

struct _roms *sensor_list

then the variable sensor_list acts as a pointer to the structure, correct?


CBFalconer wrote: Mark Richards wrote:

... snip ...

I have indented the code and my questions and comments are
preceded by // so they will be easier to follow. I hope this is
less confusing and respectful of everyone''s time.

// The following _roms structure contains a list of 8 byte
addresses...

struct _roms
{
unsigned char *roms; /* Array of 8 bytes */
int max; /* Maximum number */
};


Let''s stop right there, as the rest of the assumptions will be
wrong. roms is a pointer to an unsigned char. It is not an array
of any sort. I read no further.



这篇关于帮助无助..代码阅读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆