pointer to stuct data

R

Robert S

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
 
A

Artie Gold

Robert said:
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??!!
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
 
R

Robert S

thanks
It is a transcoder proram which compile and run without a problem
here is the code:

typedef struct {

char *infile;
/* The input image file. */

int infmt;
/* The input image file format. */

char *inopts;
char inoptsbuf[OPTSMAX + 1];

char *outfile;
/* The output image file. */

int outfmt;

char *outopts;
char outoptsbuf[OPTSMAX + 1];

int verbose;
/* Verbose mode. */

int debug;

int version;

int_fast32_t cmptno;

int srgb;

} cmdopts_t;
..
..
..
..
..
cmdopts_t *cmdopts;

cmdopts->infile = 0;
cmdopts->infmt = -1;
cmdopts->inopts = 0;
cmdopts->inoptsbuf[0] = '\0';
cmdopts->outfile = 0;
cmdopts->outfmt = -1;
cmdopts->outopts = 0;
cmdopts->outoptsbuf[0] = '\0';
cmdopts->verbose = 0;
cmdopts->version = 0;
cmdopts->cmptno = -1;
cmdopts->debug = 0;
cmdopts->srgb = 0;
..
..
..
..

Artie Gold said:
Robert said:
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??!!
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
 
K

Keith Thompson

Please don't top-post. Your response belongs below, or interspersed
with, any quoted text.

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';

but 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 0
is a null pointer constant. I'd prefer to use the macro NULL (defined
in <stddef.h> and several other standard headers), but an unadorned 0
is perfectly legal.

This is why it's best to cut-and-paste the actual code you're asking
about rather than trying to paraphrase it; it's too easy to
accidentally change the very thing you're asking about.
 
R

Robert S

Thanks
So 0 means NULL,however I thought '\0' means NULL
Now,how about -1 which assign as follows:

cmdopts->infile = 0;
cmdopts->infmt = -1;
cmdopts->inopts = 0;
cmdopts->inoptsbuf[0] = '\0';
cmdopts->outfile = 0;
cmdopts->outfmt = -1;
cmdopts->outopts = 0;
cmdopts->outoptsbuf[0] = '\0';
cmdopts->verbose = 0;
cmdopts->version = 0;
cmdopts->cmptno = -1;
cmdopts->debug = 0;
cmdopts->srgb = 0;
Keith Thompson said:
Please don't top-post. Your response belongs below, or interspersed
with, any quoted text.

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';

but 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 0
is a null pointer constant. I'd prefer to use the macro NULL (defined
in <stddef.h> and several other standard headers), but an unadorned 0
is perfectly legal.

This is why it's best to cut-and-paste the actual code you're asking
about rather than trying to paraphrase it; it's too easy to
accidentally change the very thing you're asking about.

--
Keith Thompson (The_Other_Keith) (e-mail address removed)
<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.
 
J

Jason

Robert said:
Thanks
So 0 means NULL,however I thought '\0' means NULL
Now,how about -1 which assign as follows:

'\0' in a char sense means ascii zero. 0 in a char* sense means memory
address 0x0, or NULL.
 
K

Keith Thompson

Robert S said:
Thanks
So 0 means NULL,however I thought '\0' means NULL
Now,how about -1 which assign as follows:
[snip]

Again, please don't top-post. And please don't send me copies of
Usenet articles by e-mail.

-1 is just the integer value -1. I believe all the members to which
-1 is assigned are integers. What -1 means is up to the application.
 
K

Keith Thompson

Jason said:
'\0' in a char sense means ascii zero. 0 in a char* sense means memory
address 0x0, or NULL.

ASCII is just one possible character representation. '\0' is a
character constant with value 0 (a null character). It happens to be
of type int, but it's used for clarity in a character context.

A null pointer is not necessarily address 0x0 (i.e., it's not
necessarily represented as all-bits-zero).

Please read section 5 of the C FAQ,
<http://www.eskimo.com/~scs/C-faq/top.html>.
 
J

Jason

Keith said:
ASCII is just one possible character representation. '\0' is a
character constant with value 0 (a null character). It happens to be
of type int, but it's used for clarity in a character context.

Of course, I forgot about other charsets.
A null pointer is not necessarily address 0x0 (i.e., it's not
necessarily represented as all-bits-zero).

I had no idea...
Please read section 5 of the C FAQ,
<http://www.eskimo.com/~scs/C-faq/top.html>.

According to section 5.5 of the FAQ, "it is the compiler's
responsibility to generate whatever bit pattern the machine uses for
that null pointer."

Thanks for setting me straight.

-Jason
 
F

Fred L. Kleinschmidt

Robert S wrote:

thanks
It is a transcoder proram which compile and run without a problem
here is the code:

typedef struct {

char *infile;
/* The input image file. */

int infmt;
/* The input image file format. */

char *inopts;
char inoptsbuf[OPTSMAX + 1];

char *outfile;
/* The output image file. */

int outfmt;

char *outopts;
char outoptsbuf[OPTSMAX + 1];

int verbose;
/* Verbose mode. */

int debug;

int version;

int_fast32_t cmptno;

int srgb;

} cmdopts_t;
.
.
.
.
.
cmdopts_t *cmdopts;

cmdopts->infile = 0;
This line is still an error, if you have shown the entire code.
cmdopts is a pointer, but does not yet point to anything. You cannot
dereference it.

There should be something like this somewhere:
cmdopts_t xxx;
cmdopts_t *cmdopts = &xxx;
cmdopts->infmt = -1;
cmdopts->inopts = 0;
cmdopts->inoptsbuf[0] = '\0';
cmdopts->outfile = 0;
cmdopts->outfmt = -1;
cmdopts->outopts = 0;
cmdopts->outoptsbuf[0] = '\0';
cmdopts->verbose = 0;
cmdopts->version = 0;
cmdopts->cmptno = -1;
cmdopts->debug = 0;
cmdopts->srgb = 0;
.
.
.
.

Artie Gold said:
Robert said:
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??!!
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
 
R

Richard Bos

Christopher Benson-Manica said:
'\0' is NUL, the null character. It is not the same as NULL.

All the same, '\0' is a null pointer constant, so it's legal (though
massively unwise) as a value for NULL.

Richard
 
P

pete

Richard said:
All the same, '\0' is a null pointer constant, so it's legal (though
massively unwise) as a value for NULL.

'\0' can be used anywhere that NULL can be used,
but NULL can't be used everywhere that '\0' can be used.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,431
Latest member
ElyseG3173

Latest Threads

Top