本文介绍了指向stuct数据的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 你好, 我正在研究一个代码,它的数据结构如下: typedef struct { char * infile; char inoptsbuf [10]; char * outfile; } cmdoption_a; cmdoption_a * cmdoption; 和初始化如下: cmdoption-> infile = 1; cmdoption-> outfile = 1; cmdoption-> inoptsbuf =''\ 0''; 我真的很困惑,因为整数是分配给上面的变量吗?!!!! 任何帮助?? !! bob 解决方案 你*应该*混淆;代码(正如你所引用的那样)没有任何意义 ,会(应该?)不能编译,当然也无法工作。 为什么不发布*真*代码(即剪切和粘贴,不要转录)?我是 肯定有人可以解决任何困惑。 HTH, --ag - Artie Gold - 德克萨斯州奥斯汀 http://it-matters.blogspot.com (新帖子12/5) http://www.cafepress.com/goldsays 你*应该*混淆;代码(正如你所引用的那样)毫无意义,(应该?)不能编译,当然也不可能工作。 为什么不发布*真正的*代码(即剪切和粘贴,不要转录)?我确定这里有人可以解决任何困惑。 HTH, --ag - Artie Gold - 德克萨斯州奥斯汀 http://it-matters.blogspot.com (新帖子12/5) http://www.cafepress.com / goldsays 但实际代码说(修剪了一下): typedef struct { char * infile; [...] char * outfile; [...] char outoptsbuf [OPTSMAX + 1]; [...]} cmdopts_t; [...] cmdopts_t * cmdopts; cmdopts-> infile = 0; [...] cmdopts-> outfile = 0; [...] cmdopts-> outoptsbuf [0] =''\'''; 初始化一个带有1的指针几乎肯定会使没意义,但0 是一个空指针常量。我更喜欢使用宏NULL(在< stddef.h>和其他几个标准头文件中定义 ),但是一个朴素的0 是完全合法的。 这就是为什么最好剪切并粘贴你要求的实际代码,而不是试图解释它;这很容易 意外地改变了你要问的事情。 - Keith汤普森(The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst> 圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst> 我们必须做点什么。这是事情。因此,我们必须这样做。 hello,I am investigating a code,which has data struct deelacration as follow:typedef struct {char *infile;char inoptsbuf[10];char *outfile;} cmdoption_a;cmdoption_a *cmdoption;and intialization as follow:cmdoption->infile=1;cmdoption->outfile=1;cmdoption->inoptsbuf=''\0'';I am really confused since an integer is assign to the above variables?!!!!any help??!!bob 解决方案You *should* be confused; the code (as you''ve quoted it) makes no senseat all, would (should?) not compile and certainly couldn''t possibly work.Why not post the *real* code (i.e. cut and paste, don''t transcribe)? I''msure someone here could clear up any confusion.HTH,--ag--Artie Gold -- Austin, Texas http://it-matters.blogspot.com (new post 12/5) http://www.cafepress.com/goldsays You *should* be confused; the code (as you''ve quoted it) makes no sense at all, would (should?) not compile and certainly couldn''t possibly work. Why not post the *real* code (i.e. cut and paste, don''t transcribe)? I''m sure someone here could clear up any confusion. HTH, --ag -- Artie Gold -- Austin, Texas http://it-matters.blogspot.com (new post 12/5) http://www.cafepress.com/goldsaysbut the actual code says (trimmed a bit): typedef struct { char *infile;[...] char *outfile;[...] char outoptsbuf[OPTSMAX + 1];[...] } cmdopts_t;[...] cmdopts_t *cmdopts; cmdopts->infile = 0;[...] cmdopts->outfile = 0;[...] cmdopts->outoptsbuf[0] = ''\0'';Initializing a pointer with 1 almost certainly makes no sense, but 0is a null pointer constant. I''d prefer to use the macro NULL (definedin <stddef.h> and several other standard headers), but an unadorned 0is perfectly legal.This is why it''s best to cut-and-paste the actual code you''re askingabout rather than trying to paraphrase it; it''s too easy toaccidentally change the very thing you''re asking about.--Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>We must do something. This is something. Therefore, we must do this. 这篇关于指向stuct数据的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-03 06:44