473,785 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1524
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.p gm, ' ', sizeof dummy.pgm);
memset(&dummy.r ec_type, ' ', sizeof dummy.rec_type) ;
memset(&dummy.f iller, ' ', sizeof dummy.filler);
memset(&dummy.f orm, ' ', sizeof dummy.form);
memset(&dummy.f iller1, ' ', 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_struc t = (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
2303
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 end of this post. Please read the email before you respond to it. Generally, the struct module is for packing and unpacking of binary data. It includes support to pack and unpack the c types: byte, char, short, long, long long, char, *, and...
5
3307
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 to be in that single file, that worked OK. But when I divdie that file into several ones, I have many "invalid use of undefined type" errors. The four files are main.c, main.h, readLP.h, and readLP.c. In readLP.h
19
3066
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 unique 'string' is then generated using itoa() and rand() for each block of memory. Finally using pointer-to-pointer of type char, I print the contents of the blocks of memory, i.e. the strings, using both printf() and manually, character by...
14
2310
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 whateverkeithsaysfor99compliance #define whateverkeithsays for99compliance int main(void { struct foo
6
13734
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
3991
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 string UID; public string PWD; }
18
9126
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: struct st_a { int i, j; };
9
1718
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 class as parameter to function? Is it by value or reference? Appreciate any thoughts on this issue!
10
4513
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 consideration here is that it is supplied to the AsyncOperationManager.CreateOperation(userSuppliedState) method... with userSuppliedState being, more or less, a taskId. In this case, the userSuppliedState {really taskId} is of the object type,...
0
9646
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10350
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10097
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9957
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7505
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.