Printf, array of structures and pointers...

E

Eddahbi Karim

Hi,

I have a pointer *pos, an array of structure "valid_param_s[]" and a
member name.

(void) fprintf(stderr, "Invalid parameter %s\n"
"Type --help for more "
"informations\n"
,valid_params_s[*pos].name);

This code doesn't work, so I want to know how make it work :).
I could create another temporary variable to store *pos and do :


(void) fprintf(stderr, "Invalid parameter %s\n"
"Type --help for more "
"informations\n"
,valid_params_s[variable].name);

But I would do it without temporary variable, if it's possible ;).
 
T

T.M. Sommers

Eddahbi said:
Hi,

I have a pointer *pos, an array of structure "valid_param_s[]" and a
member name.

(void) fprintf(stderr, "Invalid parameter %s\n"
"Type --help for more "
"informations\n"
,valid_params_s[*pos].name);

Assuming pos is pointing to the array element that you want to print,
the last line should be:

,pos->name);
 
T

Thes

Eddahbi said:
Hi,

I have a pointer *pos, an array of structure "valid_param_s[]" and a
member name.

(void) fprintf(stderr, "Invalid parameter %s\n"
"Type --help for more "
"informations\n"
,valid_params_s[*pos].name);

This code doesn't work, so I want to know how make it work :).
I could create another temporary variable to store *pos and do :


(void) fprintf(stderr, "Invalid parameter %s\n"
"Type --help for more "
"informations\n"
,valid_params_s[variable].name);

But I would do it without temporary variable, if it's possible ;).

If pos is a pointer to a particular member of your valid_param_s[]
array, then just access the member directly through the pointer:

(void) fprintf(stderr, "Invalid parameter %s\n"
"Type --help for more "
"informations\n"
,pos->name);

An array subscript (that is inside the []'s) must be an integer
expression - you can't use the pointer like that.
 
E

Eddahbi Karim

If pos is a pointer to a particular member of your valid_param_s[]
array, then just access the member directly through the pointer:

(void) fprintf(stderr, "Invalid parameter %s\n"
"Type --help for more "
"informations\n"
,pos->name);

An array subscript (that is inside the []'s) must be an integer
expression - you can't use the pointer like that.

Ok, So I'll do without pointers ;)

Thank you.
 
E

Ed Morton

Eddahbi said:
If pos is a pointer to a particular member of your valid_param_s[]
array, then just access the member directly through the pointer:

(void) fprintf(stderr, "Invalid parameter %s\n"
"Type --help for more "
"informations\n"
,pos->name);

An array subscript (that is inside the []'s) must be an integer
expression - you can't use the pointer like that.


Ok, So I'll do without pointers ;)

No need. The code segment you posted was fine, it just wasn't clear that
*pos was an integer.

I looked at your code posting at http://nopaste.php-q.net/17283 and the
line you say is a problem does not match the code you posted in your
article. The code at line 153 on your web page DOES have a problem. It is:

(void) fprintf(stderr, "Invalid parameter %s\n"
"Type --help for more "
"informations\n"
,valid_params_s.name);
^NOTE!

i.e. it's missing the index ([*pos]) to valid_params_s.

Ed.
 
B

Bertrand Mollinier Toublet

Eddahbi said:
If pos is a pointer to a particular member of your valid_param_s[]
array, then just access the member directly through the pointer:

(void) fprintf(stderr, "Invalid parameter %s\n"
"Type --help for more "
"informations\n"
,pos->name);

An array subscript (that is inside the []'s) must be an integer
expression - you can't use the pointer like that.


Ok, So I'll do without pointers ;)

For us to find out what is wrong with your code, we need to see a
definition of all the involved elements. In particular, we do not have a
definition of struct parameters. You don't tell us what exactly your
problem is either (does not compiler ? gives you a warning ? crashes at
runtime ? demons flying out of your nose ? random cities blasted to
smitherens ?). Be more specific and we should be able to help you...

Were you tired of your co-nationals that you prefered c.l.c to f.c.l.c :) ?
 
E

Eddahbi Karim

For us to find out what is wrong with your code, we need to see a
definition of all the involved elements. In particular, we do not have
a definition of struct parameters. You don't tell us what exactly your

problem is either (does not compiler ? gives you a warning ? crashes
at runtime ? demons flying out of your nose ? random cities blasted to

smitherens ?). Be more specific and we should be able to help you...

Ok ok, I'll do it next time :)
Were you tired of your co-nationals that you prefered c.l.c to f.c.l.c
:) ?

Not really :-D
 

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

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top