开发者

How to slice a string in multiple parts using a C macro?

开发者 https://www.devze.com 2023-02-03 02:39 出处:网络
I want to fill a multidimensional array using a macro so that the people using it think that they are using a function and passing only one string. The macro will use this string and at compile time c

I want to fill a multidimensional array using a macro so that the people using it think that they are using a function and passing only one string. The macro will use this string and at compile time convert it so that it would appear as a multidimensional array, like this:

make_array ("string1,{string2,{string3,{...,{stringN");

So the macro will replace this function to a multidimension开发者_开发问答al array and cut that string wherever it encounters,{. The code above will turn in something like this:

make_array = { "string1", "string2", "string3", ..., "stringN"};

I'm using GCC; how can I accomplish this?

Update: I thought I could exclude the quotes of the string using a macro, so I would have a string without a text and I could edit the string in macro but GCC does not accept the declaration of a macro to replace double quotes (like shown below).

#define macro_array ( "text") text

So the text will appear without double quotes and I could find the ,{ mark and cut it and use then stringify to turn the string back.


You can get a moderate approximation to what you are after with C99 and variable arguments in macros:

Source

#define make_array(name, dim1, dim2, ...) \
            static char name[dim1][dim2] = { __VA_ARGS__ }

make_array(mine, 2, 2, "abc", "def", "ghi", "jkl");

Output

$: gcc -E xx.c
# 1 "xx.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "xx.c"

static char mine[2][2] = { "abc", "def", "ghi", "jkl" };
$

However, you cannot readily split a string in the preprocessor as requested - it is simply the wrong tool for the job.


You cannot do string processing with macros.

I'm not sure I understand exactly what you want to do, but this is probably best achieved with a function.


Forget it. I see way too many questions along the lines of "How can I make my code look like this?" when the question for achieving good code should be "How can I make my code work like this?" What is the goal you're trying to achieve?

If you want to import external data that was formatted in the weird notation you specified, does the data vary at runtime or is it constant? In the former case you'll need a parser in your program and a good deal of dynamic allocation. In the latter case, you need to write a program that runs before compiling the main program which parses and converts the data into C. But if there's no legitimate reason for the data to be in this weird format to begin with, you should simply write it in C from the start rather than trying to force C to look like something else.


  1. Get rid of the quotes and use a variadic macro
  2. Write a function to split the string and call it from the macro
  3. Don't use macros for this, just make a static inline function. It is just as fast.


As Oli has already said, this kind of string processing is impossible with macros. Concatenation and replacement of other strings is about as much as you can do with macros.

I think the answer here is a question- why does the input string have to be of that format? Writing your required result does not require any more effort than it is to write your input, so why would you want to go through the pain of processing it?

0

精彩评论

暂无评论...
验证码 换一张
取 消