472,339 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,339 software developers and data experts.

Translating a DEFINE to char*

Dear people,

The code below is compiling:

#define BUF_SZ 16383
.....
strcat(ConnectString, "Content-Length: BUF_SZ\n");

but it does not work since it give me:

Content-Length: BUF_SZ and not
Content-Length: 16838

Is there a way to handle this? What I want is to get buffersize used in
the string, but also somehow keep it simple, and keep it flexible.

Jan 5 '06 #1
13 1930
ma*********@hotmail.com <ma*********@hotmail.com> wrote:
#define BUF_SZ 16383
....
strcat(ConnectString, "Content-Length: BUF_SZ\n");

but it does not work since it give me:

Content-Length: BUF_SZ and not
Content-Length: 16838

Is there a way to handle this? What I want is to get buffersize used in
the string, but also somehow keep it simple, and keep it flexible.


Try snprintf(), simpe and flexible.
--
:wq
^X^Cy^K^X^C^C^C^C
Jan 5 '06 #2
ma*********@hotmail.com wrote:
Dear people,

The code below is compiling:

#define BUF_SZ 16383
....
strcat(ConnectString, "Content-Length: BUF_SZ\n");

but it does not work since it give me:

Content-Length: BUF_SZ and not
Content-Length: 16838

Is there a way to handle this? What I want is to get buffersize used in
the string, but also somehow keep it simple, and keep it flexible.


Macros are not substituted inside strings or inside comments. You can
achieve what you want using sprintf like this:

char my_string[BIG_ENOUGH];

sprintf(my_string, "Content-Length: %d\n", BUF_SZ);

HTH

Jan 5 '06 #3
Thu, 05 Jan 2006 00:38:32 -0800ma*********@hotmail.comд
Dear people,

The code below is compiling:

#define BUF_SZ 16383
....
strcat(ConnectString, "Content-Length: BUF_SZ\n"); sprintf("ConnectString, "Content-Length: %d\n",BUF_SZ);
/*
%d -- int
%u -- unsigned int
%s -- char *
%c -- char
....
*/

but it does not work since it give me:

Content-Length: BUF_SZ and not
Content-Length: 16838

Is there a way to handle this? What I want is to get buffersize used in
the string, but also somehow keep it simple, and keep it flexible.


Jan 5 '06 #4
iamgodk wrote:
于 Thu, 05 Jan 2006 00:38:32 -0800,ma*********@hotmail.com写到:

The code below is compiling:

#define BUF_SZ 16383
....
strcat(ConnectString, "Content-Length: BUF_SZ\n");

sprintf("ConnectString, "Content-Length: %d\n",BUF_SZ);


you've gained an extra quote

sprintf(ConnectString, "Content-Length: %d\n",BUF_SZ);

<snip>

--
Nick Keighley

De maan likt niet hoog
Maar het is niet zo
De maan is wel hoog
Of niet sams?

Jan 5 '06 #5
ma*********@hotmail.com wrote:

The code below is compiling:

#define BUF_SZ 16383
.....
strcat(ConnectString, "Content-Length: BUF_SZ\n");

but it does not work since it give me:

Content-Length: BUF_SZ and not
Content-Length: 16838

Is there a way to handle this? What I want is to get buffersize
used in the string, but also somehow keep it simple, and keep it
flexible.


Try the following:

#define BUF_SZ "16383"
...
strcat(ConnectString, "Content-Length: " BUF_SZ "\n");

assuming you can modify the #define as shown.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Jan 5 '06 #6
Chuck
modify the #define


Then I could rather make two defines:

#define BUF_SZ 16383
#define CONTENTLENGTH "Contect-Length 16383 \n"
.....
strcat(ConnectString, CONTENTLENGTH);

but I think the "sprintf(ConnectString, "Content-Length:
%d\n",BUF_SZ);" is the neatest.

Jan 5 '06 #7
<ma*********@hotmail.com>
??????:11**********************@z14g2000cwz.google groups.com...
Dear people,

The code below is compiling:

#define BUF_SZ 16383
....
strcat(ConnectString, "Content-Length: BUF_SZ\n");

but it does not work since it give me:

Content-Length: BUF_SZ and not
Content-Length: 16838

Is there a way to handle this? What I want is to get buffersize used in
the string, but also somehow keep it simple, and keep it flexible.


no need to call snprintf at run time, the compiler could do that. try this

#define BUF_SZ 16383
#define TO_STR(a) _TO_STR(a)
#define _TO_STR(a) #a
strcat(ConnectString, "Content-Length: " TO_STR(BUF_SZ) "\n");

kaikai
Jan 5 '06 #8

ma*********@hotmail.com wrote:
Dear people,
Hello,

The code below is compiling:

#define BUF_SZ 16383
....
strcat(ConnectString, "Content-Length: BUF_SZ\n");

but it does not work since it give me:

Content-Length: BUF_SZ and not
Content-Length: 16838

Is there a way to handle this? What I want is to get buffersize used in
the string, but also somehow keep it simple, and keep it flexible.


Here is another solution to stringize (I think it's in the FAQ) your
macro:

#include <stdio.h>
#include <string.h>

#define BUF_SZ 16383

#define STR(n) #n
#define MKSTR(n) STR(n)

int main(void)
{
char ConnectString[50+1];
*ConnectString = '\0';
strncat(ConnectString,"Content-Length: " MKSTR (BUF_SZ) "\n", 50);
puts(ConnectString);
return 0;
}

Regis

Jan 5 '06 #9
kaikai wrote:
<ma*********@hotmail.com>
??????:11**********************@z14g2000cwz.google groups.com...
Dear people,

The code below is compiling:

#define BUF_SZ 16383
....
strcat(ConnectString, "Content-Length: BUF_SZ\n");

but it does not work since it give me:

Content-Length: BUF_SZ and not
Content-Length: 16838

Is there a way to handle this? What I want is to get buffersize used in
the string, but also somehow keep it simple, and keep it flexible.

no need to call snprintf at run time, the compiler could do that. try this

#define BUF_SZ 16383
#define TO_STR(a) _TO_STR(a)
#define _TO_STR(a) #a
strcat(ConnectString, "Content-Length: " TO_STR(BUF_SZ) "\n");


This could be made conforming by changing the name of
_TO_STR to avoid using the reserved identifier. However, it's
probably better to construct the string at run-time with
sprintf() or some such than to depend on the lexical form of
the BUF_SZ definition. Consider what happens when somebody
rewrites BUF_SZ in a more mnemonic way, e.g.

#define BUF_SZ ((1 << 14) - 1)
#define BUF_SZ (HEADER_LEN + PREFIX_SZ + BODY_MAX)
#define BUF_SZ 0x3fff
...

--
Eric Sosman
es*****@acm-dot-org.invalid

Jan 5 '06 #10
#define BUF_SZ #16383
strcat(ConnectionString,"Content Length:" BUF_SZ "\n");
1.Since ANSI Cocatenates automatically strings
e.g
if u write
printf("Vishnu " "Tiwari");
output:
Vishnu Tiwari
2.
#define var #name
replaces var by "name"
thus three after preprocessor execution it looks to compiler
strcat( ConnectionString,"Content Length:" "16383" "\n");
after joining of strings it becomes
strcat( ConnectionString,"Content Length:16383\n");
Tha same as u wished if u change value of BUF_SZ it will be reflected
in the program.
Hope it will help u .
else contact me at
er*******@gmail.com

Jan 6 '06 #11
Eric Sosman wrote:
kaikai wrote:
[snip]
no need to call snprintf at run time, the compiler could do that. try this

#define BUF_SZ 16383
#define TO_STR(a) _TO_STR(a)
#define _TO_STR(a) #a
strcat(ConnectString, "Content-Length: " TO_STR(BUF_SZ) "\n");


This could be made conforming by changing the name of
_TO_STR to avoid using the reserved identifier. However, it's
probably better to construct the string at run-time with
sprintf() or some such than to depend on the lexical form of
the BUF_SZ definition. Consider what happens when somebody
rewrites BUF_SZ in a more mnemonic way, e.g.


Well, that's a problem after he does rewrite the macro. I mean he will
confuse himself by doing that, but not by the TO_STR macro.

#define BUF_SZ ((1 << 14) - 1)
#define BUF_SZ (HEADER_LEN + PREFIX_SZ + BODY_MAX)
#define BUF_SZ 0x3fff
...

btw. Compilers will generate warnings on the duplicated definitions of
macro, right ?
--
Eric Sosman
es*****@acm-dot-org.invalid

kaikai

Jan 9 '06 #12
kaikai wrote:
Eric Sosman wrote:

kaikai wrote:


[snip]
no need to call snprintf at run time, the compiler could do that. try this

#define BUF_SZ 16383
#define TO_STR(a) _TO_STR(a)
#define _TO_STR(a) #a
strcat(ConnectString, "Content-Length: " TO_STR(BUF_SZ) "\n");


This could be made conforming by changing the name of
_TO_STR to avoid using the reserved identifier. However, it's
probably better to construct the string at run-time with
sprintf() or some such than to depend on the lexical form of
the BUF_SZ definition. Consider what happens when somebody
rewrites BUF_SZ in a more mnemonic way, e.g.

Well, that's a problem after he does rewrite the macro. I mean he will
confuse himself by doing that, but not by the TO_STR macro.


My point is that stringizing the macro gives you the
literal text of the macro definition. If what you want is
the numeric value that the definition produces, stringizing
is not the most reliable way to get it: You're depending on
the macro being written in exactly the numeric form required,
but the macro may have actually been written in some other
form. (There are often excellent reasons for doing this;
you can probably find an example by examining the INT_MIN
definition in your implementation's <limits.h>.)
#define BUF_SZ ((1 << 14) - 1)
#define BUF_SZ (HEADER_LEN + PREFIX_SZ + BODY_MAX)
#define BUF_SZ 0x3fff
...


btw. Compilers will generate warnings on the duplicated definitions of
macro, right ?


Yes: If all these definitions appear in one translation
unit (and aren't #undef'ed, suppressed with #if, or otherwise
made invisible), a diagnostic is required. I was just trying
to show some plausible ways the BUF_SZ macro might be written
(and that would make trouble when stringized), not to suggest
that it would be written in all these ways at once.

--
Eric Sosman
es*****@acm-dot-org.invalid
Jan 9 '06 #13
Eric Sosman wrote:
kaikai wrote:
Eric Sosman wrote:
[snip]
Yes: If all these definitions appear in one translation
unit (and aren't #undef'ed, suppressed with #if, or otherwise
made invisible), a diagnostic is required. I was just trying
to show some plausible ways the BUF_SZ macro might be written
(and that would make trouble when stringized), not to suggest
that it would be written in all these ways at once.

O, IC. I misunderstand you. At this point, run-time generated strings
would be better, you're right.
--
Eric Sosman
es*****@acm-dot-org.invalid


kaikai

Jan 11 '06 #14

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

Similar topics

12
by: Charles Law | last post by:
Hi guys A bit of curve ball here ... I have a document (Word) that contains a series of instructions in sections and subsections (and...
8
by: John Ratliff | last post by:
Let's say I had a program which uses some constants. Would it be "better" to declare them like this: #ifndef __MY_HDR_FILE #define __MY_HDR_FILE...
0
by: winthux | last post by:
I'm trying to use the library in c++ by using but it requires some specific structures and macros: #define gg_common_head(x) \ int fd; /*...
8
by: mailursubbu | last post by:
Hi, Will it be possible to #define a char pointer... It means if write some thing like #define CHAR_PTR (char *) // I know this wont work ...
6
by: John Dann | last post by:
I'm trying (in VB2005) to interact with a third-party DLL written (I think) in C++. One of the functions requires me to pass a structure ...
23
by: anon.asdf | last post by:
Hello! In the following code-snippet, is it possible to initialize each element of arr, with STRUCT_INIT? struct mystruct { int a; char b;...
9
by: lombardm | last post by:
I am trying to decipher/translate some code that I have no experience with. I am trying to find out how the checksum is computed for a Megellan...
5
by: Canned | last post by:
Hi, I'm trying to write a class that can convert ascii to binary and vice versa. I write my class based on this function I've found on internet ...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

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.