Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > struct and multiple indirection for a variable

Reply
Thread Tools

struct and multiple indirection for a variable

 
 
Brian Stubblefield
Guest
Posts: n/a
 
      05-24-2004
Dear group,

I am a beginner to C.

Below is a segment of a program that I am trying to debug:

#include <stdio.h>
#include <signal.h>
/*#include <stdlib.h>*/
#define BIT char

int main(void)
{
/* editted code */

void *p;

char rec1_io[81] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '
', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '
', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '
', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '
', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '
', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '
', ' '};

p = &(rec1_io);

struct RECIN1 {
char pgm[10];
char rec_type[2];
char filler[4];
char form[6];
char filler1[59];
} **recin1_struct =&p;

/* editted code */

When I compile this code, I received the following error message
pertaining to the struct statement:

warning: initialization from incompatible pointer type

Can somebody please recommend a better way that the above code can be
accomplished or fixed?

Thank you in advance,

Brian
 
Reply With Quote
 
 
 
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      05-25-2004

On Tue, 25 May 2004, Martin Johansen wrote:

> Brian Stubblefield wrote:
> >
> > #include <stdio.h>
> > #include <signal.h>
> > /*#include <stdlib.h>*/
> > #define BIT char


I *strongly* recommend you use 'unsigned char' for anything even
remotely resembling bitwise operations. While it is possible to use
signed types with bitwise operations, it's also possible to juggle
chainsaws. I don't do either one, generally speaking.

> > int main(void)
> > {
> > /* editted code */
> >
> > void *p;


Icky hard tabs. Don't use tabs on Usenet; they don't survive the
ASCIIfication. Google 'detab'.

> > char rec1_io[81] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '

> ^^ this 81 is implicit in the length of the contant.


Actually, the "constant" is a syntax error: you can't put a literal
newline inside single quotes like that. Assuming the OP meant to
initialize the array to 81 blank spaces, he could have written either
the above --- properly line-broken --- or else

char rec1_io[81] = " "
" ";

or, far more readably,

char rec1_io[81];
memset(rec1_io, ' ', sizeof rec1_io);


> > p = &(rec1_io);

> Brackets not needed.


Nit FYI: the canonical names are (parentheses), [brackets], {braces},
and <angle brackets>.

> > struct RECIN1 {
> > char pgm[10];
> > char rec_type[2];
> > char filler[4];
> > char form[6];
> > char filler1[59];
> > } **recin1_struct =&p;

>
> &p is of type char**,


s/char**/void**/

> while recin1_struct is of type struct RECIN1 **,
> you need to cast by doing this:


No, casting is precisely the wrong thing in this situation (as in
every situation except one that I can think of, and that's nothing to
worry a newbie). NEVER CAST ANYTHING IN C!
In this case, casting is wrong because it hides undefined behavior.
What the OP apparently means to do is initialize every field of his
structure to all-blanks; Lord knows why --- he's probably confused
about the role of the terminating null character in C-style strings.
But for the sake of argument, let's assume he really wants to do
this. Then one correct approach would be

#include <string.h>
[...]

struct RECIN1 dummy, *dummy2 = &dummy;
struct RECIN1 **recin1_struct = &dummy2;

memset(&dummy.pgm, ' ', sizeof dummy.pgm);
memset(&dummy.rec_type, ' ', sizeof dummy.rec_type);
memset(&dummy.filler, ' ', sizeof dummy.filler);
memset(&dummy.form, ' ', sizeof dummy.form);
memset(&dummy.filler1, ' ', sizeof dummy.filler1);

Now you have a properly initialized structure (named 'dummy'),
a nice readable initialization procedure (which could be made into
its own function if desired), and no warnings or errors --- by
*design*, not by accident.

> > When I compile this code, I received the following error message
> > pertaining to the struct statement:
> >
> > warning: initialization from incompatible pointer type

>
> This is a warning, and is not fatal.


Do not spout malicious garbage, please. Plain garbage is at
least sometimes funny, but if you *know* there's a major flaw in
the code, you should not imply that it's "only a warning." Bugs
is bugs, by and large, and this one was glaringly big.

> > Can somebody please recommend a better way that the above code can be
> > accomplished or fixed?


I do believe this is a classic example of the "How do I insert
this bullet into my foot?" newbie-question. We'll gladly tell you
how to purchase the gun and shoot yourself in the foot with it,
but it always turns out to be the case that you really wanted to
shoot a gopher and your foot was in the way. If you had just asked
how to kill a gopher in the first place, you would have received
much more helpful answers.

-Arthur
 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      05-25-2004
On Tue, 25 May 2004 00:41:54 -0700, Martin Johansen <(E-Mail Removed)>
wrote:

>Brian Stubblefield wrote:
>> Dear group,
>>
>> I am a beginner to C.
>>
>> Below is a segment of a program that I am trying to debug:
>>
>> #include <stdio.h>
>> #include <signal.h>
>> /*#include <stdlib.h>*/
>> #define BIT char
>>
>> int main(void)
>> {
>> /* editted code */
>>
>> void *p;
>>
>> char rec1_io[81] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '

>^^ this 81 is implicit in the length of the contant.
>
>> ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '
>> ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '
>> ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '
>> ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '
>> ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '
>> ', ' '};
>>
>> p = &(rec1_io);

>Brackets not needed.
>>
>> struct RECIN1 {
>> char pgm[10];
>> char rec_type[2];
>> char filler[4];
>> char form[6];
>> char filler1[59];
>> } **recin1_struct =&p;

>
>&p is of type char**, while recin1_struct is of type struct RECIN1 **,
>you need to cast by doing this:


&p is of type void**.

>
>**recin1_struct = (struct RECIN1 **)&p;


Syntactically the way to avoid the compiler diagnostic but
operationally valid only

if the alignment of rec1_io is compatible with that of a struct
RECIN1
and if the size and format of a struct RECIN1* is the same as a
void*

>
>> /* editted code */
>>
>> When I compile this code, I received the following error message
>> pertaining to the struct statement:
>>
>> warning: initialization from incompatible pointer type

>
>This is a warning, and is not fatal.
>
>> Can somebody please recommend a better way that the above code can be
>> accomplished or fixed?
>>
>> Thank you in advance,
>>
>> Brian




<<Remove the del for email>>
 
Reply With Quote
 
Martin Johansen
Guest
Posts: n/a
 
      05-25-2004
Brian Stubblefield wrote:
> Dear group,
>
> I am a beginner to C.
>
> Below is a segment of a program that I am trying to debug:
>
> #include <stdio.h>
> #include <signal.h>
> /*#include <stdlib.h>*/
> #define BIT char
>
> int main(void)
> {
> /* editted code */
>
> void *p;
>
> char rec1_io[81] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '

^^ this 81 is implicit in the length of the contant.

> ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '
> ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '
> ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '
> ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '
> ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '
> ', ' '};
>
> p = &(rec1_io);

Brackets not needed.
>
> struct RECIN1 {
> char pgm[10];
> char rec_type[2];
> char filler[4];
> char form[6];
> char filler1[59];
> } **recin1_struct =&p;


&p is of type char**, while recin1_struct is of type struct RECIN1 **,
you need to cast by doing this:

**recin1_struct = (struct RECIN1 **)&p;

> /* editted code */
>
> When I compile this code, I received the following error message
> pertaining to the struct statement:
>
> warning: initialization from incompatible pointer type


This is a warning, and is not fatal.

> Can somebody please recommend a better way that the above code can be
> accomplished or fixed?
>
> Thank you in advance,
>
> Brian

 
Reply With Quote
 
Brian Stubblefield
Guest
Posts: n/a
 
      05-25-2004
Thank you for your suggestions. I have implemented them and the
compile issues have gone away.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
Multiple indirection mess-up... santosh C Programming 19 12-20-2005 04:06 AM
XSL variable indirection sucheta.phatak@gmail.com XML 2 09-21-2005 05:44 AM
assign a struct variable into another struct variable ineedyourluvin1@yahoo.com C++ 18 07-22-2005 04:08 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments