What this means? ---> struct resource *r = v, *p;

M

michael

I found follow function in the Linux kernel (kernel/resource.c)

static int r_show(struct seq_file *m, void *v)
{
struct resource *root = m->private;
struct resource *r = v, *p;
int width = root->end < 0x10000 ? 4 : 8;
int depth;

for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p =
p->parent)
if (p->parent == root)
break;
seq_printf(m, "%*s%0*lx-%0*lx : %s\n",
depth * 2, "",
width, r->start,
width, r->end,
r->name ? r->name : "<BAD>");
return 0;
}

What's mean of ---> " struct resource *r = v, *p; "
Thanks.
 
J

junky_fellow

michael said:
I found follow function in the Linux kernel (kernel/resource.c)

static int r_show(struct seq_file *m, void *v)
{
struct resource *root = m->private;
struct resource *r = v, *p;
<snip>
What's mean of ---> " struct resource *r = v, *p; "
Thanks.

Read this statement as,
struct resource *r = v;
struct resource *p;
 
R

Richard Bos

michael said:
What's mean of ---> " struct resource *r = v, *p; "

It's a normal declaration statement, containing the declarations of two
pointers to struct resource, one of which is initialised to v.

Richard
 
R

Rufus V. Smith

michael said:
I found follow function in the Linux kernel (kernel/resource.c)

static int r_show(struct seq_file *m, void *v)
{
struct resource *root = m->private;
struct resource *r = v, *p;
int width = root->end < 0x10000 ? 4 : 8;
int depth;

for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p =
p->parent)
if (p->parent == root)
break;
seq_printf(m, "%*s%0*lx-%0*lx : %s\n",
depth * 2, "",
width, r->start,
width, r->end,
r->name ? r->name : "<BAD>");
return 0;
}

What's mean of ---> " struct resource *r = v, *p; "
Thanks.

Horrible and confusing statement!

It's declaring two pointers, one being initialized
from the function argument.

They should be on separate lines.

It's probably fair to say you should always (at least
99.44% of the time)keep your declarations on separate lines.

That v,*P (to me anyway) can be easily confused with (v,*p), which
means evaluate v, then evaluate *p and return the value of *p. This
may be disallowed in initializers, though.

To further convince you to use separate lines, consider the following:

#define intpointer_t int*

intpointer_t a,b,c;

Will not give you 3 pointers to int, but one pointer to int (a)
while b and c are simple integers.

typedef int* intpointer_t;

intpointer_t a,b,c;

Will work as expected, but that doesn't make it a Good Thing.

Rufus
 
J

Jonathan Burd

Rufus said:
"michael" <[email protected]> wrote in message

Horrible and confusing statement!

It's declaring two pointers, one being initialized
from the function argument.

They should be on separate lines.

It's probably fair to say you should always (at least
99.44% of the time)keep your declarations on separate lines.

That v,*P (to me anyway) can be easily confused with (v,*p), which
means evaluate v, then evaluate *p and return the value of *p. This
may be disallowed in initializers, though.

The comma operator ',' has the least precedence of all operators.

int a, b = 5;
a = 10, b;

will always assign 10 to a.
To further convince you to use separate lines, consider the following:


Regards,
Jonathan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top