结构成员访问问题 [英] structure member access issue

查看:68
本文介绍了结构成员访问问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:我对新闻组没有经验,所以我很抱歉,如果我已经完成了

任何错误。


在函数dosomething( )我想跳过workingstuffptr

workaround。有没有什么方法可以直接使用stuffptr而不用

使用临时工具?


提前致谢。


#include< stdio.h>

#include< stdlib.h>


typedef struct _stuff

{

unsigned int value;

} STUFF,* LPSTUFF;


无效dosomething(LPSTUFF * stuffptr)

{

STUFF * workingstuffptr;


workingstuffptr = * stuffptr;

workingstuffptr-> value = 5;


stuffptr-> id = 5; / *不能做* /


}


int main(无效)

{

LPSTUFF某些东西;


somestuff = malloc(sizeof(STUFF));

if(!somestuff)

返回5;

somestuff-> value = 10;


dosomething(& somestuff);


printf(" Value =%d \ n",somestuff-> value);

返回0;

}

Note: I''m not experienced with newsgroups so my apologies if I''ve done
any errors.

In the function dosomething() I would like to skip the workingstuffptr
"workaround". is there any way I can use stuffptr directly without
using a temp?

Thanks in advance.

#include <stdio.h>
#include <stdlib.h>

typedef struct _stuff
{
unsigned int value;
} STUFF, *LPSTUFF;

void dosomething(LPSTUFF *stuffptr)
{
STUFF *workingstuffptr;

workingstuffptr = *stuffptr;
workingstuffptr->value = 5;

stuffptr->id = 5; /* Can''t be done */

}

int main(void)
{
LPSTUFF somestuff;

somestuff = malloc(sizeof(STUFF));
if(!somestuff)
return 5;
somestuff->value = 10;

dosomething(&somestuff);

printf("Value = %d\n",somestuff->value);
return 0;
}

推荐答案

" Waxhead" < ZO **** @ online.no>写道:
"Waxhead" <zo****@online.no> wrote:
注意:我对新闻组没有经验,所以我很抱歉,如果我已经做了任何错误。

在函数dosomething()我想跳过workingstuffptr
workaround。有没有什么方法可以直接使用stuffptr而不使用临时工具?

提前致谢。

#include< stdio.h>
#include< stdlib.h>

typedef struct _stuff


以下划线开头的标识符保留给

实现。

{
unsigned int value;
} STUFF,* LPSTUFF;


请注意,使用typedef隐藏结构类型通常被认为是非常糟糕的样式,使用typedef隐藏指针类型。

void dosomething(LPSTUFF * stuffptr)


为什么要传递一个指向指针的指针?实际上这看起来像是一个很好的例子,程序员如何对他自己的typedef'ing

练习感到困惑。

{
STUFF * workingstuffptr;


为什么不在这里使用LPSTUFF?至少与你的

类型别名一致。

workingstuffptr = * stuffptr;
workingstuffptr-> value = 5;

stuffptr-> id = 5; / *无法完成* /


有两个显而易见的原因:

1. stuffptr是一个指向指针的指针-struct _stuff

2.你的struct _stuff没有声明成员名为id。

}
int main(void)
{
LPSTUFF somestuff;

somestuff = malloc(sizeof(STUFF));


clc批准的成语是:


somestuff = malloc(sizeof * somestuff);


至少你没有犯罪来施放malloc的结果。 :) if(!somestuff)
返回5;


5不是main的便携式返回值。由于你已经包含了

stdlib.h EXIT_FAILURE和EXIT_SUCCESS是你的命令。

somestuff-> value = 10;

dosomething(& ; somestuff);


盲目添加间接级别,可能是因为编译器

barfed,不是解决方案

printf(" Value = %d\\\
",&somestuff- GT;值);


%d不是打印unsigned int的正确格式说明符。使用%u

代替。

返回0;
}
Note: I''m not experienced with newsgroups so my apologies if I''ve done
any errors.

In the function dosomething() I would like to skip the workingstuffptr
"workaround". is there any way I can use stuffptr directly without
using a temp?

Thanks in advance.

#include <stdio.h>
#include <stdlib.h>

typedef struct _stuff
Identifiers beginning with an underscore are reserved for the
implementation.
{
unsigned int value;
} STUFF, *LPSTUFF;
Note that hiding structural types with typedef is often considered
very bad style, hiding pointer types with a typedef as well.
void dosomething(LPSTUFF *stuffptr)
Why do you pass a pointer-to-pointer? Actually this looks like a
good example how a programmer got confused by his own typedef''ing
practice.
{
STUFF *workingstuffptr;
Why not be use LPSTUFF here as well? At least be consistent with your
typename aliases.

workingstuffptr = *stuffptr;
workingstuffptr->value = 5;

stuffptr->id = 5; /* Can''t be done */
An for two obvious reasons:
1. stuffptr is a pointer-to-a-pointer-to-struct _stuff
2. your struct _stuff has no member named id declared.

}

int main(void)
{
LPSTUFF somestuff;

somestuff = malloc(sizeof(STUFF));
The c.l.c approved idiom for this is:

somestuff = malloc(sizeof *somestuff);

At least you didn''t commit the crime to cast the result of malloc. :) if(!somestuff)
return 5;
5 is not a portable return value for main. Since you already included
stdlib.h EXIT_FAILURE and EXIT_SUCCESS are at your command.
somestuff->value = 10;

dosomething(&somestuff);
Blindly adding levels of indirection, probably because the compiler
barfed, is not the solution

printf("Value = %d\n",somestuff->value);
%d is not the right format specifier to print an unsigned int. Use %u
instead.
return 0;
}




对于初学者你的代码事实并非如此(我肯定会看到更糟糕的情况),但肯定没有雪茄。 :)


结论:抛弃那些可怕的typedef,然后重写

程序逻辑。下面你会找到一个经过纠正和清理的版本

你的代码,如果你想通过自己动手学习,不要看。


..

..

..

..

..

..

..

..

..

..

..

..


#include< stdio.h>

#include< stdlib.h>


struct stuff

{

unsigned int value;

};


void dosomething(struct stuff * stuffptr)

{

stuffptr-> value = 5;

}


int main(无效)

{

struct stuff * somestuff;


somestuff = malloc( sizeof * somestuff);

if(somestuff == NULL)

返回EXIT_FAILURE;


somestuff-> value = 10 ;

dosomething(somestuff);

printf(" Value =%u \ n",somestuff-> value);

返回EXIT_SUCCESS;

}

最好的问候

-

Irrwahn Grausewitz(ir*******@freenet.de)

欢迎到clc: http://www.ungerhu.com/jxh/clc .welcome.txt

clc faq-list: http://www.faqs.org/faqs/C-faq/faq/

clc常见答案: http://benpfaff.org/writings/clc



For a beginner your code is not too bad, actually (I''ve definitely
seen worse), but certainly no cigar. :)

Conclusion: throw out those hideous typedefs, and then rewrite the
program logic. Below you find a corrected and cleaned up version of
your code, don''t look if you want to learn by doing it yourself.

..
..
..
..
..
..
..
..
..
..
..
..

#include <stdio.h>
#include <stdlib.h>

struct stuff
{
unsigned int value;
};

void dosomething(struct stuff *stuffptr)
{
stuffptr->value = 5;
}

int main(void)
{
struct stuff *somestuff;

somestuff = malloc(sizeof *somestuff);
if(somestuff == NULL)
return EXIT_FAILURE;

somestuff->value = 10;
dosomething(somestuff);
printf("Value = %u\n",somestuff->value);
return EXIT_SUCCESS;
}
Best regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc.


Waxhead写道:
Waxhead wrote:
注意:我对新闻组没有经验,所以我很抱歉,如果我已经做了任何错误。

在函数dosomething()我想跳过workingstuffptr
workaround。有没有什么方法可以直接使用stuffptr而不使用临时工具?

提前致谢。

#include< stdio.h>
#include< stdlib.h>

typedef struct _stuff
{
unsigned int value;
} STUFF,* LPSTUFF;

void dosomething(LPSTUFF * stuffptr)
{
STUFF * workingstuffptr;

workingstuffptr = * stuffptr;
workingstuffptr-> value = 5;

stuffptr-> id = 5; / *无法完成* /
Note: I''m not experienced with newsgroups so my apologies if I''ve done
any errors.

In the function dosomething() I would like to skip the workingstuffptr
"workaround". is there any way I can use stuffptr directly without
using a temp?

Thanks in advance.

#include <stdio.h>
#include <stdlib.h>

typedef struct _stuff
{
unsigned int value;
} STUFF, *LPSTUFF;

void dosomething(LPSTUFF *stuffptr)
{
STUFF *workingstuffptr;

workingstuffptr = *stuffptr;
workingstuffptr->value = 5;

stuffptr->id = 5; /* Can''t be done */




(* stuffptr) - > id = 5; / *可以是* /


-

Eric Sosman
es ***** @ acm-dot-org.inva 盖子


Eric Sosman< es **** *@acm-dot-org.invalid>写道:
Eric Sosman <es*****@acm-dot-org.invalid> wrote:
Waxhead写道:
Waxhead wrote:



< snip>


<snip>

#include< stdio .h>
#include< stdlib.h>

typedef struct _stuff
{
unsigned int value;
} STUFF,* LPSTUFF;

无效的东西(LPSTUFF * stuffptr)
{
STUFF * workingstuffptr;

workingstuffptr = * stuffptr;
workingstuffptr-> value = 5;

stuffptr-> id = 5; / *无法完成* /
#include <stdio.h>
#include <stdlib.h>

typedef struct _stuff
{
unsigned int value;
} STUFF, *LPSTUFF;

void dosomething(LPSTUFF *stuffptr)
{
STUFF *workingstuffptr;

workingstuffptr = *stuffptr;
workingstuffptr->value = 5;

stuffptr->id = 5; /* Can''t be done */



(* stuffptr) - > id = 5; / *可以是* /



(*stuffptr)->id = 5; /* Can be */




不错的尝试,但是:没有。 :)

-

Irrwahn Grausewitz(ir*******@freenet.de)

欢迎来到clc: http://www.ungerhu.com/jxh/clc.welcome.txt

clc faq-list: http://www.faqs.org/faqs/C-faq/faq/

clc常见答案: http://benpfaff.org/writings/clc



Nice try, but: no. :)
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc.


这篇关于结构成员访问问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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