473,405 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

struct and multiple indirection for a variable

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
Nov 14 '05 #1
4 1502
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

Nov 14 '05 #2

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
Nov 14 '05 #3
On Tue, 25 May 2004 00:41:54 -0700, Martin Johansen <mf**@online.no>
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>>
Nov 14 '05 #4
Thank you for your suggestions. I have implemented them and the
compile issues have gone away.
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Josiah Carlson | last post by:
Good day everyone, I have produced a patch against the latest CVS to add support for two new formatting characters in the struct module. It is currently an RFE, which I include a link to at the...
5
by: PCHOME | last post by:
Hello! I am working on dividing a single C file into several files. Now I encounter a problem about the global variables and can not find a way to solve it. All global variables and codes used...
19
by: santosh | last post by:
Hi all, In the following program I allocate a block of pointers to type char, initialised to zero. I then point each of those pointers to a block of allocated memory of fixed size (33 bytes). A...
14
by: Lane Straatman | last post by:
I would like to write a 'struct'. I have a library that is all but completely inappropriate for this task. So I'm looking for C code that fills in the gaps between: #undef...
6
by: Angus | last post by:
I am using a global which is a void* I have it defined in one file as: void* hFlag; and one other header file as: extern void* hFlag; But I get this compile error:
6
by: Joseph Geretz | last post by:
I have the following class which I am serializing and passing back and forth between my Web Service application and the client. public class Token : SoapHeader { public string SID; public...
18
by: Ehud Shapira | last post by:
Is it possible to have a declaration of a struct pointer initialized to an unnamed struct? (I'm only concerned with static/global variables, if it matters.) I'm trying to do something like: ...
9
by: Jure Bogataj | last post by:
Hello! Since struct and class are very similar in C#, I was wondering why using struct would be better in some situations than using class. Any performance issues? What about sending struct or...
10
by: Frankie | last post by:
It appears that System.Random would provide an acceptable means through which to generate a unique value used to identify multiple/concurrent asynchronous tasks. The usage of the value under...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.