473,804 Members | 3,019 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to convert from a string to an expression?

for example:
int x=10;
char *str="\"x=%d\", x";

if I use
printf(expressi on(str));
then the results is
x=10

even I know it's not easy to return a expression from a function.

can u give me a good idea?
Nov 14 '05 #1
3 1836
David wrote:
for example:
int x=10;
char *str="\"x=%d\", x";

if I use
printf(expressi on(str));
then the results is
x=10

even I know it's not easy to return a expression from a function.

can u give me a good idea?


If you tell us what you want to achieve and give us the code
you have up to now or at least your representation of an "expression "
(which is no builtin C datatype).

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #2
Michael Mair <Mi**********@i nvalid.invalid> writes:
David wrote:
for example:
int x=10;
char *str="\"x=%d\", x";

if I use
printf(expressi on(str));
then the results is
x=10

even I know it's not easy to return a expression from a function.

can u give me a good idea?


If you tell us what you want to achieve and give us the code
you have up to now or at least your representation of an "expression "
(which is no builtin C datatype).


I think he's done that. His representation of an expression seems to
be a string containing a sequence of arguments to printf, separated by
commas. He wants a function

char *expression (char *expr);

that will return the string that would have been printed by printf().

David, is that about right?

Unfortunately, what you're trying to do is pretty much impossible.
Given a string "x", there's no way in C to get the value of the
variable whose name happens to be x. Variable names appear in source
code, but typically do not appear in the executable program; even if
they do, there's no way to get at them. Roughly speaking, the
compiler decides where x is going to be stored; all references to x in
your program are transformed into references to that memory location,
and the name vanishes.

Now if you explicitly associate the string "x" with the variable,
something like
record_name("x" , &x);
you might be able to do something like what what you describe, but
it's going to be complicated. The above would not be adequate; you'll
also need some way to associate "x" with the type of x, which happens
to be int -- and there's no simple way to represent a type at runtime.

The first thing you should do is decide whether whatever problem
you're trying to solve can be solved in some other way.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <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.
Nov 14 '05 #3
Keith Thompson wrote:
Michael Mair <Mi**********@i nvalid.invalid> writes:
David wrote:
for example:
int x=10;
char *str="\"x=%d\", x";

if I use
printf(expressi on(str));
then the results is
x=10

even I know it's not easy to return a expression from a function.

can u give me a good idea?
If you tell us what you want to achieve and give us the code
you have up to now or at least your representation of an "expression "
(which is no builtin C datatype).


I think he's done that. His representation of an expression seems to
be a string containing a sequence of arguments to printf, separated by
commas. He wants a function

char *expression (char *expr);

that will return the string that would have been printed by printf().


Ah, I completely misread that. He sort of stringizes the arguments
he would normally pass to printf() and wants convert() to do
printf()s job but only with the string as input.

Hmmm...
[snip: Explanation of problems] Now if you explicitly associate the string "x" with the variable,
something like
record_name("x" , &x);
you might be able to do something like what what you describe, but
it's going to be complicated. The above would not be adequate; you'll
also need some way to associate "x" with the type of x, which happens
to be int -- and there's no simple way to represent a type at runtime.


Yep. Specialised solution:
One decides that a to z are "enough" and that all variables are
of either the largest integer or the largest floating point type.
Then you can store the addresses in an array:
void *vars['z'+1];

.....
unsigned long var[26], *ptmp, sink;
int i;
for (i=0, ptmp = var; i<='z'; i++)
if (isalpha(i) && islower(i))
vars[i] = ptmp++;
else
vars[i] = &sink;
(untested)
Alternatively, one could use NULL instead of &sink.
However, this is no general and no easily extendible _and_
maintainable solution.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
7294
by: Lauren Quantrell | last post by:
I have a stored procedure using Convert where the exact same Convert string works in the SELECT portion of the procedure but fails in the WHERE portion. The entire SP is listed below. Specifically, I have a problem with this portion in the WHERE clause: DATEADD(Day,tblMyEventTableName.ReminderDays, @DateNow) Between CONVERT(smalldatetime,str(DATEPART(Month, @DateNow)+1) + '/' + str(DATEPART(Day, tblMyEventTableName.TaskDateTime)) + '/'...
7
1840
by: spiros | last post by:
Hi, suppose you have the class Class1 and the main program: class Class1 { public: Class1(); ~Class1();
4
9774
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a numeric value according to an arbitrary regular expression.
19
11312
by: caramel | last post by:
i've been working on this program forever! now i'm stuck and going insane because i keep getting a syntax error msg and i just can't see what the compiler is signaling to! #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h>
4
39349
by: perryclisbee via AccessMonster.com | last post by:
I have dates of service for several people that range all over each month. ie: patient had dates of service of: 7/3/2006, 7/24/2006 and 7/25/2006. I need to create a new field via a query that will convert each of the records of these service dates to the first date of that month, with results showing: 7/1/2006, 7/1/2006, 7/1/2006. How would you place an expression on a query that will convert any given date to the first day of the month...
2
2416
by: Jakub Bednarczuk | last post by:
Hi everybody I am trying to find the solution for the problem stated in the topic, but so far without any result. Let's see the problem. I have set of conditions and the description (logical sentence) how should be these conditions connected with each other. See how does it look in xml
4
25070
by: meendar | last post by:
Hi, I am having a character pointer which contains ascii values. i just want to convert all these ascii values to respective characters and again store it in another character pointer. Anybody please help in c language. Thanks in Advance.
1
2190
by: Michel Walsh | last post by:
In the same spirit, but more LINQ related, you can also use ExecuteQuery: var query = dataContext.ExecuteQuery<className>( @"SELECT ... WHERE ... AND... OR... "); where className is appropriate to recuperate the result of your dynamically built SQL statement, as a string.
6
2038
by: priravi | last post by:
Hi, I'm having a numeric expression say "((2*3)-(3*4+5))" returned from a method as a string value... now i have to evaluate the expression and store it in an integer vaariable. Convert.toint or int.parse is not working and since i'm new to .net development i have no idea of how else to convert this.Please do help me regarding this
0
9705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10564
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10320
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7609
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6846
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5513
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5645
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2981
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.