#define value

The code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#define ARTSHORT(obj) \
	char *stuff = ""; \
	char *art = (obj)->article; \
	char *des = (obj)->description; \
	char buf[500]; \
	snprintf(buf, sizeof(buf), "%s", stuff); \
	strcat(buf, art); \
	strcat(buf, " "); \
	strcat(buf, des);

void someFunction()
{
   printOut("data: %s", ARTSHORT(obj));
}


The errors:
1
2
3
// error C2062: type 'char' unexpected
// error C2065: 'stuff' : undeclared identifier
// error C2143: syntax error : missing ';' before ')' 


What I'm trying to do:
I'm trying to concatenate two strings and "return" the result using this #define statement.

Is what I'm trying to do just not possible with a #define? I know I could easily create a returning function to do this, but using a #define is the preferred method for my circumstance.

Thanks in advance for any help.
Well... on second thought, I'll just do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
char *ARTSHORT(struct obj_data *obj)
{
	char *stuff = "";
	char *art = (obj)->article;
	char *des = (obj)->description;
	char buf[500];
	snprintf(buf, sizeof(buf), "%s", stuff);
	strcat(buf, art);
	strcat(buf, " ");
	strcat(buf, des);

	return _strdup(buf);
}

void someFunction()
{
   printOut("data: %s", ARTSHORT(obj));
}


It works flawlessly. However, if one of you C/C++ coding guru's could tell my why my #define didn't work, I'd love to learn. :) Thanks.
Define replaces the actual call with the code you have defined. All the code you defined cannot be put into the middle of a printOut() function call.

edit: To concat two strings use

string one = string(two) + string(three);
Last edited on
edit: To concat two strings use

string one = string(two) + string(three);


1
2
3
4
5
6
7
8
9
10
char *foo(struct obj_data *obj)
{
	char *result = ((string)obj->article + (string)" " + (string)obj->description));
	return(result);
}

// error C2065: 'string' : undeclared identifier
// error C2146: syntax error : missing ')' before identifier 'obj'
// error C2059: syntax error : ')'
// error C2059: syntax error : ')' 


the "string" keyword is not recognized.

And this code gets these errors:
1
2
3
4
5
6
7
8
char *foo(struct obj_data *obj)
{
	char *result = (obj->article + " " + obj->description));
	return(result);
}

// error C2110: '+' : cannot add two pointers
// error C2059: syntax error : ')' 


I thank you for the suggested concatenation method, but I'll just stick to what I've already coded. It's working. :)
Are you #including <string>? Also you need to use std::string or using namespace std;. If you are using C++ you should use std::strings instead of C-style strings (char arrays).
1
2
3
4
5
6
7
8
9
10
11
12
#include <string>

using std::string;

int main() {
 string one = "some";
 string two = "one";

 string both = one + two;

 return 0;
}


Note: YOUR code is prone to buffer overflows and crashes.
Last edited on
Yay, no more errors. However...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string>
using std::string;

string foo() {
	string one = "hello";
	string two = " ";
	string three = "world";

	string both = one + two + three;

	return both;
}

printOut("data: %s", foo());


Here's what I get on screen: item: (null)
If your printOut acts as printf, "%s" is for C strings, foo returns a C++ string.
Just use the c_str() string member function:

printOut("data: %s", foo().c_str() );
Last edited on

Bazzy said...
If your printOut acts as printf, "%s" is for C strings, foo returns a C++ string.
Just use the c_str() string member function:

printOut("data: %s", foo().c_str() );



That did it. Many thanks to everyone who helped. :)
if you want define a new funtion ,you can use:
#define struck datatype {what you want define} data;

when you call you can do :
C++
temp.data.?? waht you want on it
Topic archived. No new replies allowed.