Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Translating a DEFINE to char*

Reply
Thread Tools

Translating a DEFINE to char*

 
 
marcwentink@hotmail.com
Guest
Posts: n/a
 
      01-05-2006
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.

 
Reply With Quote
 
 
 
 
usenet@zevv.nl
Guest
Posts: n/a
 
      01-05-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Antonio Contreras
Guest
Posts: n/a
 
      01-05-2006
(E-Mail Removed) 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

 
Reply With Quote
 
iamgodk
Guest
Posts: n/a
 
      01-05-2006
Thu, 05 Jan 2006 00:38:32 -0800(E-Mail Removed)д

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


 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      01-05-2006
iamgodk wrote:
> 于 Thu, 05 Jan 2006 00:38:32 -0800,(E-Mail Removed)写到:


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

 
Reply With Quote
 
Chuck F.
Guest
Posts: n/a
 
      01-05-2006
(E-Mail Removed) 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/>
 
Reply With Quote
 
marcwentink@hotmail.com
Guest
Posts: n/a
 
      01-05-2006
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.

 
Reply With Quote
 
kaikai
Guest
Posts: n/a
 
      01-05-2006
<(E-Mail Removed)>
??????:(E-Mail Removed) 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


 
Reply With Quote
 
Targeur fou
Guest
Posts: n/a
 
      01-05-2006

(E-Mail Removed) 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

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      01-05-2006
kaikai wrote:

> <(E-Mail Removed)>
> ??????:(E-Mail Removed) 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
(E-Mail Removed)lid

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
About typedef -- define the function pointer or define function model? robin liu C Programming 3 04-21-2006 03:26 PM
#define _ and #define __ Brian Takita Ruby 0 01-23-2006 04:34 AM
ipv6 - ipv4 nat-pt just not translating Yamin Cisco 0 06-29-2004 08:36 PM
1720 stops translating outbound NAT Jon Cisco 6 06-16-2004 02:59 PM
How to define a define that defines some defines ? theotyflos C Programming 3 02-19-2004 05:07 PM



Advertisments