Printf, array of structures and pointers...

Discussion in 'C Programming' started by Eddahbi Karim, Oct 17, 2003.

  1. 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 ;).

    --
    --
    ThE_TemPLaR
     
    Eddahbi Karim, Oct 17, 2003
    #1
    1. Advertising

  2. Eddahbi Karim

    T.M. Sommers Guest

    Eddahbi Karim wrote:
    > 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.M. Sommers, Oct 17, 2003
    #2
    1. Advertising

  3. On Fri, 17 Oct 2003 09:16:41 GMT
    "T.M. Sommers" <> wrote:

    >
    > Assuming pos is pointing to the array element that you want to print,
    > the last line should be:
    >
    > ,pos->name);
    >


    pos is the position in the array.

    Maybe http://nopaste.php-q.net/17283 would help you ?
    Line 153 ;)

    (I prefer to paste a link, because there's no need to flood here :))

    --
    --
    ThE_TemPLaR
     
    Eddahbi Karim, Oct 17, 2003
    #3
  4. Eddahbi Karim

    Thes Guest

    Eddahbi Karim wrote:
    > 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.
     
    Thes, Oct 17, 2003
    #4
  5. On Fri, 17 Oct 2003 10:28:22 +0100
    Thes <> wrote:


    > 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.


    --
    --
    ThE_TemPLaR
     
    Eddahbi Karim, Oct 17, 2003
    #5
  6. Eddahbi Karim

    Ed Morton Guest

    Eddahbi Karim wrote:

    > On Fri, 17 Oct 2003 10:28:22 +0100
    > Thes <> wrote:
    >
    >
    >
    >>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.

    > Thank you.
    >
    >
     
    Ed Morton, Oct 17, 2003
    #6
  7. Eddahbi Karim wrote:
    > On Fri, 17 Oct 2003 10:28:22 +0100
    > Thes <> wrote:
    >
    >
    >
    >>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 :) ?

    --
    Bertrand Mollinier Toublet
    int main(){char*strchr();int j=1234;char t[]=":mad:abcdefghij-lmnopqrstuv"
    "wxyz.\n",*i="iqgbgxmbbla.llsvoaz:zdxylaxejivnidhd@ttopnjeftuh-i";while
    (*i){j+=strchr(t,*i++)-t;j%=sizeof t-1;putchar(t[j]);}return 0;}
     
    Bertrand Mollinier Toublet, Oct 17, 2003
    #7
  8. On Fri, 17 Oct 2003 08:05:51 -0700
    Bertrand Mollinier Toublet
    <> wrote:


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


    --
    --
    ThE_TemPLaR
     
    Eddahbi Karim, Oct 17, 2003
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jimjim

    copying an array of pointers to structures

    jimjim, Dec 13, 2003, in forum: C Programming
    Replies:
    5
    Views:
    424
    Barry Schwarz
    Dec 14, 2003
  2. tweak
    Replies:
    14
    Views:
    2,789
    Eric Sosman
    Jun 11, 2004
  3. Alfonso Morra
    Replies:
    11
    Views:
    722
    Emmanuel Delahaye
    Sep 24, 2005
  4. neo-saiya-jin
    Replies:
    1
    Views:
    673
    Martin York
    Mar 23, 2008
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    682
Loading...

Share This Page