String: "anti" string oprator

Discussion in 'C Programming' started by paragk, Jan 28, 2011.

  1. paragk

    paragk Guest

    Hi,

    The # operator converts a parameter to a string. Is there a way to
    "anti" string?

    For e.g.

    char *p="abc";
    char *q="xyz";

    #define declare_fun(x) void <anti string>x( void)

    main()
    {

    declare_func(*p);
    declare_func(*q);
    }

    I would like to get the following after the preprocessor is run:

    main()
    {
    void abc(void);
    void xyz(void);
    }

    Is there a way to achieve this?

    Thanks,
    Parag
     
    paragk, Jan 28, 2011
    #1
    1. Advertising

  2. paragk

    Seebs Guest

    On 2011-01-28, paragk <> wrote:
    > The # operator converts a parameter to a string. Is there a way to
    > "anti" string?


    No.

    > char *p="abc";
    > char *q="xyz";


    > #define declare_fun(x) void <anti string>x( void)
    >
    > main()
    > {
    >
    > declare_func(*p);
    > declare_func(*q);
    > }
    >
    > I would like to get the following after the preprocessor is run:
    >
    > main()
    > {
    > void abc(void);
    > void xyz(void);
    > }


    > Is there a way to achieve this?


    No. You're trying to perform a runtime computation at compile time, and
    you can't do it. You need an interpreted language, or at least one with
    support for compiling new code at runtime.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
     
    Seebs, Jan 28, 2011
    #2
    1. Advertising

  3. paragk

    paragk Guest

    On Jan 28, 11:40 am, Seebs <> wrote:
    > On 2011-01-28, paragk <> wrote:
    >
    > > The # operator converts a parameter to a string. Is there a way to
    > > "anti" string?

    >
    > No.
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > > char *p="abc";
    > > char *q="xyz";
    > > #define declare_fun(x) void <anti string>x( void)

    >
    > > main()
    > > {

    >
    > >   declare_func(*p);
    > >   declare_func(*q);
    > > }

    >
    > > I would like to get the following after the preprocessor is run:

    >
    > > main()
    > > {
    > >   void abc(void);
    > >   void xyz(void);
    > > }
    > > Is there a way to achieve this?

    >
    > No.  You're trying to perform a runtime computation at compile time, and
    > you can't do it.  You need an interpreted language, or at least one with
    > support for compiling new code at runtime.
    >
    > -s
    > --
    > Copyright 2010, all wrongs reversed.  Peter Seebach / ://www.seebs.net/log/<-- lawsuits, religion, and funny pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    > I am not speaking for my employer, although they do rent some of my opinions.


    How about:

    main()
    {
    declare_func("abc");
    declare_func("pqr");
    }

    Will this work?

    Thanks,
    Parag
     
    paragk, Jan 28, 2011
    #3
  4. paragk <> writes:
    > On Jan 28, 11:40 am, Seebs <> wrote:
    >> On 2011-01-28, paragk <> wrote:
    >>
    >> > The # operator converts a parameter to a string. Is there a way to
    >> > "anti" string?

    >>
    >> No.
    >>
    >> > char *p="abc";
    >> > char *q="xyz";
    >> > #define declare_fun(x) void <anti string>x( void)

    >>
    >> > main()
    >> > {

    >>
    >> >   declare_func(*p);
    >> >   declare_func(*q);
    >> > }

    >>
    >> > I would like to get the following after the preprocessor is run:

    >>
    >> > main()
    >> > {
    >> >   void abc(void);
    >> >   void xyz(void);
    >> > }
    >> > Is there a way to achieve this?

    >>
    >> No.  You're trying to perform a runtime computation at compile time, and
    >> you can't do it.  You need an interpreted language, or at least one with
    >> support for compiling new code at runtime.

    >
    > How about:
    >
    > main()
    > {
    > declare_func("abc");
    > declare_func("pqr");
    > }
    >
    > Will this work?


    I don't believe there's any way to get the proprocessor to convert
    a string literal to an identifier.

    Even if there were, what would be the point? Why not just write:

    int main(void) /* note proper declaration of main */
    {
    declare_func(abc);
    declare_func(pqr);
    }

    Or, for that matter:

    int main(void)
    {
    void abc(void);
    void pqr(void);
    }

    And in fact, it rarely makes sense to declare a function inside
    another function. (*Defining* a function inside another function
    is illegal, unless you're using some compiler-specific extension).

    Generally declarations for functions not defined in the current
    source file should be in header files that you can #include:

    #include "abc_pqr.h" /* declare abc and pqr */
    int main(void)
    {
    /* ... */
    }

    Exactly what problem are you trying to solve?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jan 28, 2011
    #4
  5. paragk

    Rui Maciel Guest

    paragk wrote:

    > Hi,
    >
    > The # operator converts a parameter to a string. Is there a way to
    > "anti" string?
    >
    > For e.g.
    >
    > char *p="abc";
    > char *q="xyz";
    >
    > #define declare_fun(x) void <anti string>x( void)
    >
    > main()
    > {
    >
    > declare_func(*p);
    > declare_func(*q);
    > }
    >
    > I would like to get the following after the preprocessor is run:
    >
    > main()
    > {
    > void abc(void);
    > void xyz(void);
    > }
    >
    > Is there a way to achieve this?



    From your example, and noting that your intended result doesn't include
    any string literal, it would be simpler to define the function-declaring
    macro with a parameter intended to directly set that token. So, for
    example:


    <code>
    #define declare_fun(x) void x( void)

    declare_func(abc);
    declare_func(xyz);
    </code>

    That would generate:
    <code>
    void abc(void);
    void xyz(void);
    </code>


    If you really need to declare functions whose identifiers include a token
    which is also present in a string literal then you can always define that
    token separately and then include it in the declarations of both your
    function and your string literals.


    Rui Maciel
     
    Rui Maciel, Jan 28, 2011
    #5
  6. paragk

    paragk Guest

    On Jan 28, 12:28 pm, Keith Thompson <> wrote:
    > paragk <> writes:
    > > On Jan 28, 11:40 am, Seebs <> wrote:
    > >> On 2011-01-28, paragk <> wrote:

    >
    > >> > The # operator converts a parameter to a string. Is there a way to
    > >> > "anti" string?

    >
    > >> No.

    >
    > >> > char *p="abc";
    > >> > char *q="xyz";
    > >> > #define declare_fun(x) void <anti string>x( void)

    >
    > >> > main()
    > >> > {

    >
    > >> >   declare_func(*p);
    > >> >   declare_func(*q);
    > >> > }

    >
    > >> > I would like to get the following after the preprocessor is run:

    >
    > >> > main()
    > >> > {
    > >> >   void abc(void);
    > >> >   void xyz(void);
    > >> > }
    > >> > Is there a way to achieve this?

    >
    > >> No.  You're trying to perform a runtime computation at compile time, and
    > >> you can't do it.  You need an interpreted language, or at least one with
    > >> support for compiling new code at runtime.

    >
    > > How about:

    >
    > >  main()
    > >  {
    > >    declare_func("abc");
    > >    declare_func("pqr");
    > >  }

    >
    > > Will this work?

    >
    > I don't believe there's any way to get the proprocessor to convert
    > a string literal to an identifier.
    >
    > Even if there were, what would be the point?  Why not just write:
    >
    > int main(void) /* note proper declaration of main */
    > {
    >     declare_func(abc);
    >     declare_func(pqr);
    >
    > }
    >
    > Or, for that matter:
    >
    > int main(void)
    > {
    >     void abc(void);
    >     void pqr(void);
    >
    > }
    >
    > And in fact, it rarely makes sense to declare a function inside
    > another function.  (*Defining* a function inside another function
    > is illegal, unless you're using some compiler-specific extension).
    >
    > Generally declarations for functions not defined in the current
    > source file should be in header files that you can #include:
    >
    > #include "abc_pqr.h" /* declare abc and pqr */
    > int main(void)
    > {
    >     /* ... */
    >
    > }
    >
    > Exactly what problem are you trying to solve?
    >
    > --
    > Keith Thompson (The_Other_Keith)  <http://www.ghoti.net/~kst>
    > Nokia
    > "We must do something.  This is something.  Therefore, we must do this."
    >     -- Antony Jay and Jonathan Lynn, "Yes Minister"



    I have a function which takes a number of parameters. The parameter
    names have a fixed template.

    For e.g.

    foo( int a, int b, int c)

    I need to call foo() multiple times with the following arguments

    foo( param_first, param_second, param_third);
    foo( param1_first, param1_second, param1_third);
    foo( test_first, test_second, test_third);

    ....

    Lets assume the parameters are global variables.

    I can have a table which lists all the parameters:

    for e.g.

    struct {

    int first,
    int second,
    int third
    } param_template

    and then an array of this struct

    [ {param_first, param_second, param_third},
    {param1_first, param1_second, param1_third},
    {test_first, test_second, test_third},
    ...
    ]


    I can then iterate through this array and call foo().

    I was thinking of having a array of strings and generating the
    parameter names on the fly.

    That is:

    [ "param", "param1", "test"... ]

    and then generate the parameter names on the fly and call foo()

    I just realized this won't work.

    A better approach would be to generate the static table.

    #define GEN_TBL_ENTRY(x) { x##_first, x##_second, x##_third}

    And then:

    [ GEN_TBL_ENTRY(param),
    GEN_TBL_ENTRY(param1),
    GEN_TBL_ENTRY(test),
    ...
    ]


    Thanks,
    Parag
     
    paragk, Jan 28, 2011
    #6
  7. paragk

    Seebs Guest

    On 2011-01-28, paragk <> wrote:
    >seebs said:
    >> No. ?You're trying to perform a runtime computation at compile time, and
    >> you can't do it. ?You need an interpreted language, or at least one with
    >> support for compiling new code at runtime.


    > How about:


    Read that paragraph again. No change in punctuation will change the
    essence of the problem, which is that you're trying to look at the contents
    of a variable and use it as a symbol to compile at compile time.

    On some systems there's ways to look up symbols that were already defined
    by names, but there's no way to do this at compile time. Try an
    intepreted language.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
     
    Seebs, Jan 28, 2011
    #7
  8. paragk <> writes:
    > On Jan 28, 12:28 pm, Keith Thompson <> wrote:
    >> paragk <> writes:

    [...]
    >> > How about:

    >>
    >> >  main()
    >> >  {
    >> >    declare_func("abc");
    >> >    declare_func("pqr");
    >> >  }

    >>
    >> > Will this work?

    >>
    >> I don't believe there's any way to get the proprocessor to convert
    >> a string literal to an identifier.

    [...]
    >> Exactly what problem are you trying to solve?

    >
    > I have a function which takes a number of parameters. The parameter
    > names have a fixed template.
    >
    > For e.g.
    >
    > foo( int a, int b, int c)
    >
    > I need to call foo() multiple times with the following arguments
    >
    > foo( param_first, param_second, param_third);
    > foo( param1_first, param1_second, param1_third);
    > foo( test_first, test_second, test_third);


    If you only have one function to be called, why are you looking for
    a macro that generates a function declaration? Just declare foo,
    and you're done (at least with that part).

    > ...
    >
    > Lets assume the parameters are global variables.


    Why are they global variables?

    > I can have a table which lists all the parameters:
    >
    > for e.g.
    >
    > struct {
    >
    > int first,
    > int second,
    > int third
    > } param_template
    >
    > and then an array of this struct
    >
    > [ {param_first, param_second, param_third},
    > {param1_first, param1_second, param1_third},
    > {test_first, test_second, test_third},
    > ...
    > ]


    If you have a series of objects, each of which is to be passed as the
    first (, second, third) argument to foo(), perhaps the whole series
    should just be declared as an array. Or maybe you should have an array
    of structs, where each struct contains all three parameters.

    Just declare your array(s) and loop over it. No need for any macros.

    [snip]

    Oh, and please snip some of the quoted text when you post a followup.
    Just keep whatever is needed for the followup to make sense.
    In particular, please don't quote signatures (the lines following
    the "-- " delimiter).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jan 28, 2011
    #8
  9. paragk

    James Kuyper Guest

    On 01/28/2011 01:47 PM, paragk wrote:
    > Hi,
    >
    > The # operator converts a parameter to a string. Is there a way to
    > "anti" string?


    More accurately, it generates a string literal. String literals are
    features of your source code which cease to exist by the time your
    program has been completely translated, whereas strings don't exist
    until your program starts executing. Most string literals get translated
    into code which causes the creation of a string when the program is
    executed, which could cause some confusion about that distinction.

    > For e.g.
    >
    > char *p="abc";
    > char *q="xyz";
    >
    > #define declare_fun(x) void<anti string>x( void)
    >
    > main()
    > {
    >
    > declare_func(*p);
    > declare_func(*q);


    Even if your idea were feasible to implement, I think you would want q,
    not *q, in this location. q points at a string; *q is an expression with
    a integer value of 'x'.

    > }
    >
    > I would like to get the following after the preprocessor is run:
    >
    > main()
    > {
    > void abc(void);
    > void xyz(void);
    > }
    >
    > Is there a way to achieve this?


    The fundamental problem is that p points at a string which does not
    exist until the program starts executing, while declarations require
    names that must be specified while the program is still being
    translated. What you're asking for can only be done in a language that
    is, by design, interpreted. I suspect that what you actually need is
    something different from what you've asked for.

    Something which could be done without turning C into an interpreted
    language is to convert a string literal into a similar token, but
    without the double quotes, making it usable as an identifier. However,
    C's preprocessing features are not powerful enough to do something like
    that. If you think you need that feature, I recommend trying to figure
    out how you can reverse the organization of your code so you can start
    with a token, and convert it to a string literal.

    --
    James Kuyper
     
    James Kuyper, Jan 29, 2011
    #9
  10. Seebs wrote:
    > Read that paragraph again. No change in punctuation will change the
    > essence of the problem, which is that you're trying to look at the contents
    > of a variable and use it as a symbol to compile at compile time.
    >
    > On some systems there's ways to look up symbols that were already defined
    > by names, but there's no way to do this at compile time. Try an
    > intepreted language.


    As a matter of nitpick, it doesn't have to be an interpreted language.
    D is an example of a compiled language which allows this sort of
    'de-tokenization' of string constants, as well as compile-time string
    operations like concatenation and substringing.

    But, of course, none of that is available in standard C.
    --
    Marcin Grzegorczyk
     
    Marcin Grzegorczyk, Jan 29, 2011
    #10
  11. paragk

    Rui Maciel Guest

    Marcin Grzegorczyk wrote:

    > As a matter of nitpick, it doesn't have to be an interpreted language.
    > D is an example of a compiled language which allows this sort of
    > 'de-tokenization' of string constants, as well as compile-time string
    > operations like concatenation and substringing.
    >
    > But, of course, none of that is available in standard C.


    While we are in the process of picking nits, I believe that, in this
    context, Seebs' reference to "compile time" was made regarding the
    preprocessing stage. Considering this, it is not possible to look at the
    contents of a variable which is defined through a grammar which your
    parser (in this case the C preprocessor) either doesn't recognize as such
    or simply ignores.

    To put it in other words, in CPP terms a variable declaration/definition
    in C is not a variable declaration at all. It is simply noise which
    should be ignored. As a consequence, it doesn't exist and therefore it's
    contents cannot be accessed.


    Rui Maciel
     
    Rui Maciel, Jan 29, 2011
    #11
  12. paragk

    James Kuyper Guest

    On 01/29/2011 12:59 PM, Marcin Grzegorczyk wrote:
    > Seebs wrote:

    ....
    >> On some systems there's ways to look up symbols that were already defined
    >> by names, but there's no way to do this at compile time. Try an
    >> intepreted language.

    >
    > As a matter of nitpick, it doesn't have to be an interpreted language. D
    > is an example of a compiled language which allows this sort of
    > 'de-tokenization' of string constants, as well as compile-time string
    > operations like concatenation and substringing.


    The OP's example was not trying to detokenize a string constant, it was
    trying to make use of a variable declared as a pointer to char.

    --
    James Kuyper
     
    James Kuyper, Jan 29, 2011
    #12
  13. James Kuyper wrote:
    > The OP's example was not trying to detokenize a string constant, it was
    > trying to make use of a variable declared as a pointer to char.


    Yes, but the OP also talked about “"anti" stringâ€, presumably meaning a
    "destringization" operator ("de-tokenization" was a wrong word to use on
    my part, sorry about that). And his second example did not involve any
    variables.
    --
    Marcin Grzegorczyk
     
    Marcin Grzegorczyk, Jan 31, 2011
    #13
    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. anon
    Replies:
    2
    Views:
    436
    MadCrazyNewbie
    Apr 6, 2004
  2. ~~~ .NET Ed ~~~
    Replies:
    1
    Views:
    337
    Scott Allen
    Mar 21, 2005
  3. Mark Rae
    Replies:
    0
    Views:
    640
    Mark Rae
    Mar 7, 2006
  4. Tim Tyler

    LCD anti-aliasing in Java

    Tim Tyler, Sep 4, 2003, in forum: Java
    Replies:
    2
    Views:
    1,301
    Tim Tyler
    Sep 5, 2003
  5. xx

    Graphics2D anti-alias

    xx, Apr 12, 2004, in forum: Java
    Replies:
    1
    Views:
    5,976
    Chris Lamprecht
    Apr 12, 2004
Loading...

Share This Page