"Making" a string in C 
Author Message
 "Making" a string in C

     I was wondering if there is any way to "make" a string out of, say, a
variable name, in C.   I was trying to do this using token-parsing but didn't
get very far.   What I am trying to do is something like:
MAKESTRING(lalala) -> "lalala"
     So I tried stuff like:
#define MAKESTRING(x) "##x##"
     ...and other variations, but, of course, none of them worked because once
the first double-quotation mark is reached, the #'s lose their token-pasting
meaning...   Is there a way to do this or is this a completely futile
endeavour?

                      -=<[Zibalatz]>=- Vincent Stephen-Ong

                        http://www.*-*-*.com/ ~zibalatz
                   I should be shot for the good of humanity.



Thu, 20 May 1999 03:00:00 GMT  
 "Making" a string in C

Quote:

>      I was wondering if there is any way to "make" a string out of, say, a
> variable name, in C.   I was trying to do this using token-parsing but didn't
> get very far.   What I am trying to do is something like:
> MAKESTRING(lalala) -> "lalala"
>      So I tried stuff like:
> #define MAKESTRING(x) "##x##"
>      ...and other variations, but, of course, none of them worked because once
> the first double-quotation mark is reached, the #'s lose their token-pasting
> meaning...   Is there a way to do this or is this a completely futile
> endeavour?

        #define MAKESTRING #x

Michael M Rubenstein



Fri, 21 May 1999 03:00:00 GMT  
 "Making" a string in C

:      I was wondering if there is any way to "make" a string out of, say, a
: variable name, in C.   I was trying to do this using token-parsing but didn't
: get very far.   What I am trying to do is something like:
: MAKESTRING(lalala) -> "lalala"
:      So I tried stuff like:
: #define MAKESTRING(x) "##x##"
:      ...and other variations, but, of course, none of them worked because once
: the first double-quotation mark is reached, the #'s lose their token-pasting
: meaning...   Is there a way to do this or is this a completely futile
: endeavour?

     Before my post generates a thousand replies, I'd just like to say that I
got a very complete reply via email, so there is no need to clutter up c.l.c
any further!
     Thanks!

                      -=<[Zibalatz]>=- Vincent Stephen-Ong

                        http://www.cs.mcgill.ca/~zibalatz
                 "Keep passing the open windows." - John Irving



Fri, 21 May 1999 03:00:00 GMT  
 "Making" a string in C

Quote:


> :      I was wondering if there is any way to "make" a string out of, say, a
> : variable name, in C.   I was trying to do this using token-parsing but didn't
> : get very far.   What I am trying to do is something like:
> : MAKESTRING(lalala) -> "lalala"
> :      So I tried stuff like:
> : #define MAKESTRING(x) "##x##"
> :      ...and other variations, but, of course, none of them worked because once
> : the first double-quotation mark is reached, the #'s lose their token-pasting
> : meaning...   Is there a way to do this or is this a completely futile
> : endeavour?
>      Before my post generates a thousand replies, I'd just like to say that I
> got a very complete reply via email, so there is no need to clutter up c.l.c
> any further!
>      Thanks!

Would you be so kind to share it with us, please?

Quote:
>                       -=<[Zibalatz]>=- Vincent Stephen-Ong

>                         http://www.cs.mcgill.ca/~zibalatz
>                  "Keep passing the open windows." - John Irving

Christ.

[posted & emailed]

+------------------------------------------------+
| Christ Vandromme (ping2844 "at" ping "dot" be) |
|                                                |
|Home page:                                      |
| http://www.ping.be/user/Christ.Vandromme       |
+------------------------------------------------+



Sat, 22 May 1999 03:00:00 GMT  
 "Making" a string in C

Quote:

>      I was wondering if there is any way to "make" a string out of, say, a
> variable name, in C.

#define stringify(x) stringify__magic(x)
#define stringify__magic(x) #x

If you're wondering what stringify__magic's for, read the FAQ.  Wait:
you should have done this already.  {*filter*} you.
--
[mdw]

`When our backs are against the wall, we shall turn and fight.'
                -- John Major



Sat, 22 May 1999 03:00:00 GMT  
 "Making" a string in C

Here's a very straight forward thing:

#define STR(x,y) y##x##y
#define MKSTR(x) STR(x,")

main() {
  printf ("%s", MKSTR(hello));

Quote:
}

Then again I'd rather just do:
printf("%s", "hello"); /* :) */

Roberto

:      I was wondering if there is any way to "make" a string out of, say, a
: variable name, in C.   I was trying to do this using token-parsing but didn't
: get very far.   What I am trying to do is something like:
: MAKESTRING(lalala) -> "lalala"
:      So I tried stuff like:
: #define MAKESTRING(x) "##x##"
:      ...and other variations, but, of course, none of them worked because once
: the first double-quotation mark is reached, the #'s lose their token-pasting
: meaning...   Is there a way to do this or is this a completely futile
: endeavour?

:                       -=<[Zibalatz]>=- Vincent Stephen-Ong

:                         http://www.cs.mcgill.ca/~zibalatz
:                    I should be shot for the good of humanity.

--




----------------------------------------------------------------------



Mon, 24 May 1999 03:00:00 GMT  
 "Making" a string in C

In referenced article, Mark Wooding says...

Quote:

>>      I was wondering if there is any way to "make" a string out of,
>>say, a variable name, in C.

>#define stringify(x) stringify__magic(x)
>#define stringify__magic(x) #x

>If you're wondering what stringify__magic's for, read the FAQ.  Wait:
>you should have done this already.  {*filter*} you.

I'm inclined to believe the question was a little more profound than
that.

In 'C' there is a distinct separation between compile-time and
run-time.  At compile time the compiler knows the names of all your
variables etc, but these (except to a de{*filter*}) are not available at
run time.  Only the variables themselves have meaning at run-time, not
their names.  Unlike some other languages there is no way at run-time
that you can create a name, e.g. as a string, and access the value of
the corresponding variable.

--
Ray Dunn (opinions are my own) | Phone: (514) 938 9050
Montreal                       | Phax : (514) 938 5225



Tue, 25 May 1999 03:00:00 GMT  
 "Making" a string in C

Quote:

> In referenced article, Mark Wooding says...

> >>      I was wondering if there is any way to "make" a string out of,
> >>say, a variable name, in C.

> >#define stringify(x) stringify__magic(x)
> >#define stringify__magic(x) #x

> >If you're wondering what stringify__magic's for, read the FAQ.  Wait:
> >you should have done this already.  {*filter*} you.

> I'm inclined to believe the question was a little more profound than
> that.

I've read the question again, and I still disagree.

Quote:
> In 'C' there is a distinct separation between compile-time and
> run-time.  At compile time the compiler knows the names of all your
> variables etc, but these (except to a de{*filter*}) are not available at
> run time.  Only the variables themselves have meaning at run-time, not
> their names.  Unlike some other languages there is no way at run-time
> that you can create a name, e.g. as a string, and access the value of
> the corresponding variable.

The question was how to `"make" a string out of ... a variable name',
not how to find a variable given a string of its name.  If you're right,
then the original poster asked the wrong question.
--
[mdw]

`When our backs are against the wall, we shall turn and fight.'
                -- John Major



Fri, 28 May 1999 03:00:00 GMT  
 "Making" a string in C

Quote:

> Here's a very straight forward thing:

> #define STR(x,y) y##x##y
> #define MKSTR(x) STR(x,")

> main() {
>   printf ("%s", MKSTR(hello));
> }

Doesn't work.  You've got an unterminated string literal on the second
line.  Tokenisation into pp-tokens happens before macros are
substituted (translation phase 3 rather than phase 4).

I've already posted the correct answer; it's also in the FAQ.
--
[mdw]

`When our backs are against the wall, we shall turn and fight.'
                -- John Major



Fri, 28 May 1999 03:00:00 GMT  
 "Making" a string in C

Quote:



> > > In referenced article, Mark Wooding says...

> > > >>      I was wondering if there is any way to "make" a string out of,
> > > >>say, a variable name, in C.

> > > >#define stringify(x) stringify__magic(x)
> > > >#define stringify__magic(x) #x

> > > I'm inclined to believe the question was a little more profound than
> > > that.

> > I've read the question again, and I still disagree.

> > The question was how to `"make" a string out of ... a variable name',
> > not how to find a variable given a string of its name.  If you're right,
> > then the original poster asked the wrong question.

> Perhaps because of my experience of langauges like Lisp and POP-2, I'm reading
> something into the question that doesn't exist, but the key information missing
> from the question is whether he is implying "at compile time" or "at run time".

> If at compile time, isn't the answer to how to create a string from the variable
> name foo, simply, "foo"?

I think Vincent wants the user to be able to change the value of a
variable at runtime by supplying the variable name and value.  Why, I
don't know.  

Vincent, it can be done in a round about way.  Declare the variable.
Declare a pointer to the variable.  Use strcmp() and and if/else if
block to assing the entered value to the variable.

int magic;
int * pmagic;

if(strcmp(instring, "magic"))
        *pmagic = invalue;

Mike



Sun, 30 May 1999 03:00:00 GMT  
 "Making" a string in C

     Sorry about the very late replies, but I am a student and was busy
failing things.   :)

     Originally, I just wanted to be able to do exactly what the initial
responses gave me, that is, how to "stringify" a variable name.   The problem
was to do this generally (i.e.: use a macro) and I did not realize that the
answer was in the FAQ.   Sorry, I really must read this, I just wasn't certain
as to its location, initially!

     Anyway, I actually had a second question which I didn't ask, because I
was nearly certain that there was no way to do it, and it was exactly brought
up by Mike & Ray (I think).

: This was my interpretation.  I'm not sure why your solution necessitates a
: pointer though, because
:    if(strcmp(instring, "magic"))
:       magic = invalue;
: works just as well.
:
: The *general* solution is much more complex, involving building some sort of
: dictionary of the names with pointers to the corresponding variables.

     This is the problem...   the general solution.   I guess the question
then arises:   Is there anyway to "unstringify" a token?   I strongly doubt
that the answer is yes...
     This would allow such odd things as:
char fcn[1024];

printf("Which function would you like to test?\n");
fgets(fcn, 1024, stdin);
UNSTRING(fcn)();

or, better:

#define UNSTRING(x) ...           // Magical operations

enum type { INT, CHAR, FLOAT, ... , FCN1, FCN2 };    // (fcns w/varying args)

struct {
  char thing[1024];
  enum type is;

Quote:
} dynamic;

     And then prompt for what the "thing" is, if so, set dynamic.type
to the appropriate thing, and use UNSTRING(dynamic.thing) with the appropriate
operator (i.e.: (), [], or nothing) to access the var or fn...

     If there were a "type" type, this could be easier!

     As it is, I am resigned to the belief that this is impossible, regardless
of how cool and potentially destructive this could be!   :)

     I was also thinking of a possible solution comparing the string to a
symbol table, and then assigning the appropriate memory location...   but this
would require a lot more assembler than it would C, I'd think.

                      -=<[Zibalatz]>=- Vincent Stephen-Ong

                        http://www.cs.mcgill.ca/~zibalatz
        "Silver medal means you died." - Renshi D. Akutagawa on "jitsu"



Thu, 03 Jun 1999 03:00:00 GMT  
 "Making" a string in C


Quote:

> Here's a very straight forward thing:

> #define STR(x,y) y##x##y
> #define MKSTR(x) STR(x,")

There is a much easier way:

#define MKSTR(x) #x

The "stringize" operator '#' converts the text following it into a
string literal.  Thus you can do:

#define MBZ(x) do {int xx = (x); \
        if (xx != 0) \
        fprintf (stderr, "%s must be zero.  It is actually %d\n", #x, xx ); \

Quote:
} while (0);

This could be used as 'MBZ(x+y);', and the error would be
"x+y must be zero.  It is actually 5"

Quote:

> main() {
>   printf ("%s", MKSTR(hello));
> }

> Then again I'd rather just do:
> printf("%s", "hello"); /* :) */

> Roberto


> :      I was wondering if there is any way to "make" a string out of, say, a
> : variable name, in C.   I was trying to do this using token-parsing but didn't
> : get very far.   What I am trying to do is something like:
> : MAKESTRING(lalala) -> "lalala"
> :      So I tried stuff like:
> : #define MAKESTRING(x) "##x##"
> :      ...and other variations, but, of course, none of them worked because once
> : the first double-quotation mark is reached, the #'s lose their token-pasting
> : meaning...   Is there a way to do this or is this a completely futile
> : endeavour?

> :                       -=<[Zibalatz]>=- Vincent Stephen-Ong

> :                         http://www.cs.mcgill.ca/~zibalatz
> :                    I should be shot for the good of humanity.

> --




> ----------------------------------------------------------------------

Martin Bonner
Pi Technology, Milton Hall, Church Lane, Milton, Cambridge, ENGLAND
+44 1223 203894



Fri, 11 Jun 1999 03:00:00 GMT  
 
 [ 15 post ] 

 Relevant Pages 

1. making computer go "beep"

2. Help(making a process "sleep")

3. Making files "Installable"

4. Neophyte going nuts "making INN"

5. ClassView makes code with #include ""

6. What makes a handle "invalid"?

7. Implementing "HELP" made by wizard

8. Making a chat room "bot"

9. Making a dialog "TASKMODAL"

10. S"String" and L"String"

11. remove() vrs fopen("""w")

12. Displaying binary data as ascii "1"'s and "0"'s

 

 
Powered by phpBB® Forum Software