473,986 Members | 28,627 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strings macro

I'd like to have a couple of arrays of strings where the first strings
are the same in all arrays.
I tried this:

#define COMMON_STRINGS "alfa", "bravo", "charlie"

char *firstStrArr[] = {COMMON_STRINGS , "delta", "echo", "foxtrot"};
char *secondStrArr[] = {COMMON_STRINGS , "golf", "hotel", "india"};
char *thirdStrArr[] = {COMMON_STRINGS , "juliett", "kilo", "london"};
It works fine when I compile it with gcc, but I always feel little bit
shaky when it comes to strings and preprocessing, so I wonder if this is
correct and portable?

Thanks

/Krister
Feb 13 '08 #1
3 1481
Kalle Anka wrote:
I'd like to have a couple of arrays of strings where the first strings
are the same in all arrays.
I tried this:

#define COMMON_STRINGS "alfa", "bravo", "charlie"

char *firstStrArr[] = {COMMON_STRINGS , "delta", "echo", "foxtrot"};
char *secondStrArr[] = {COMMON_STRINGS , "golf", "hotel", "india"};
char *thirdStrArr[] = {COMMON_STRINGS , "juliett", "kilo", "london"};
It works fine when I compile it with gcc, but I always feel little bit
shaky when it comes to strings and preprocessing, so I wonder if this is
correct and portable?
Should be, there's nothing tricky going on there.

--
Ian Collins.
Feb 13 '08 #2
On Feb 13, 6:43*am, Kalle Anka <kalle.a...@ank eborg.sewrote:
I'd like to have a couple of arrays of strings where the first
strings are the same in all arrays.
I tried this:

#define COMMON_STRINGS "alfa", "bravo", "charlie"

char *firstStrArr[] = {COMMON_STRINGS , "delta", "echo", "foxtrot"};
char *secondStrArr[] = {COMMON_STRINGS , "golf", "hotel", "india"};
char *thirdStrArr[] = {COMMON_STRINGS , "juliett", "kilo", "london"};

It works fine when I compile it with gcc, but I always feel
little bit shaky when it comes to strings and preprocessing, so
I wonder if this is correct and portable?
It might be better to define the arrays as const.

--
Martin
Feb 13 '08 #3
On Wed, 13 Feb 2008 07:43:51 +0100, Kalle Anka wrote:
I'd like to have a couple of arrays of strings where the first strings
are the same in all arrays.
I tried this:

#define COMMON_STRINGS "alfa", "bravo", "charlie"

char *firstStrArr[] = {COMMON_STRINGS , "delta", "echo", "foxtrot"};
char *secondStrArr[] = {COMMON_STRINGS , "golf", "hotel", "india"};
char *thirdStrArr[] = {COMMON_STRINGS , "juliett", "kilo", "london"};
It works fine when I compile it with gcc, but I always feel little bit
shaky when it comes to strings and preprocessing, so I wonder if this is
correct and portable?

Thanks

/Krister
I've used such macros; they seem fine to me. However being clumsy I once
managed to miss the comma after a COMMON_STRINGS invocation. This does not
lead to a syntax error, but to constant string concatenation, and was time
consuming to track down. Since the horse had fled, I bolted the stable
door by using the ugly:
#define COMMON_STRINGS ("alfa"), ("bravo"), ("charlie")
which does yield a syntax error if you miss the comma after an invocation.

Feb 13 '08 #4

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

Similar topics

20
2858
by: Dead RAM | last post by:
Hey people, i'll try to keep this short ;) Here is what I want to type (or at least close too)... #define VER_BUILD 1 #define STR_VER_BUILD "VER_BUILD" But what happends is the preprocessor see the quots in STR_VER_BUILD and replaces that text with "VER_BUILD"... I need it to see the VER_BUILD and replace it with 1, and only after doing
6
1805
by: Buck Rogers | last post by:
Hi guys, newbie here. I am trying to write a program which counts the number of characters in two strings, then prints the number, and states which string is longer. For some reason, my program gives an incorrect number of characters in each string(1 character less), and I have no idea why. Can somebody pls explain this?
11
5973
by: Grumble | last post by:
Hello, I have the following structure: struct foo { char *format; /* format string to be used with printf() */ int nparm; /* number of %d specifiers in the format string */ /* 0 <= nparm <= 4 */ };
11
4377
by: arekkusu | last post by:
Hello, I have the following problem: when declaring hex data in C, you typically do something like: const char = {0xde, 0xad, 0xbe, 0xef, 0x01, 0x02, 0x03, 0x04 ... }; This is quite verbose and makes the source hard to read. An alternative is something like const char = "deadbeef01020304";
16
4025
by: agay | last post by:
Hi, I would like to get feedback on a "switching on strings" utility: http://shum.huji.ac.il/~agay/sos Thanks a. agay
14
1467
by: Ben | last post by:
I have an int variable (always <100) that I want to convert to a two character string, e.g. if myint = 1, mystr = "01" if myint = 81, mystr = "81" At the moment I can't figure out how to do this cleanly. Then I wish to push a bunch of these strings into an array, for example:
3
1664
by: kenkahn | last post by:
Given the following simple c program #include <stdio.h> enum ATTRTYPE { CFG_ATTR = 3000 }; int main() { #define xx(id) printf("ATTR=%s\n",#id);
1
1334
by: George2 | last post by:
Hello everyone, I have a number of strings in an array, "FILE1", "FILE2", "FILE3", ... , "FILEN" I want to add the common prefix to all the string, like (for example, the common prefix is "FOO")
10
2166
by: abir | last post by:
Hi, This is not strictly a C++ language question. Is there any way to form a string from an identifier with the preprocessor with some operations ? i.e to say to make a string from an identifier ID i use #ID. Thats ok, but what if i want to alter it a little. like if i have an identifier myID , and i want to generate an string MYID (or any other form) from it.
0
10391
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
10203
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
10135
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8515
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
6471
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
6622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
5215
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
3812
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.