define a string

  • Follow


How to define a string?

Usually we can

#define MAX 30

However if I'd like to define a string const
can I?
#define S "Hello" 

seems not working

Thanks

0
Reply junciu (72) 6/9/2005 4:20:16 PM

In article <1118334015.994183.206830@o13g2000cwo.googlegroups.com>,
QQ <junciu@yahoo.com> wrote:
>How to define a string?

>Usually we can

>#define MAX 30

>However if I'd like to define a string const
>can I?
>#define S "Hello" 

>seems not working

What difficulty are you having with it? Can you present a -short- program
showing what you are trying to do but which isn't working?
-- 
   Any sufficiently advanced bug is indistinguishable from a feature.
   -- Rich Kulawiec
0
Reply roberson2 (8067) 6/9/2005 4:34:08 PM


QQ wrote:
> How to define a string?
> 
> Usually we can
> 
> #define MAX 30
> 
> However if I'd like to define a string const
> can I?
> #define S "Hello" 
> 
> seems not working
> 
> Thanks
> 

Just remember that the #define is a substitution
that takes place before the translation phase.

So, when using the #define, try getting the syntax
correct first:

/* First try with this syntax */
const char * S = "Hello";

/* Then move it to a #define: */
#define S (const char *)("Hello")

{Although literal strings are constants.}

-- 
Thomas Matthews

C++ newsgroup welcome message:
          http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq:   http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
          http://www.comeaucomputing.com/learn/faq/
Other sites:
     http://www.josuttis.com  -- C++ STL Library book
     http://www.sgi.com/tech/stl -- Standard Template Library

0
Reply Thomas_MatthewsSpamBotsSuck (408) 6/9/2005 6:53:07 PM

QQ wrote:
> How to define a string?
> 
> Usually we can
> 
> #define MAX 30
> 
> However if I'd like to define a string const
> can I?
> #define S "Hello" 
> 
> seems not working
> 
> Thanks
> 

#include <stdio.h>
#define S "Hello"
int main(void) {
    puts(S);
    return 0;
}

It's 'working' here.

-- 
Joe Wright
"Everything should be made as simple as possible, but not simpler."
                     --- Albert Einstein ---
0
Reply joewwright (1737) 6/10/2005 6:47:46 PM

3 Replies
32 Views

(page loaded in 0.091 seconds)


Reply: