Reputation:
In C I have:
filedata->string_table + (X)->sh_name
Now I want to add the following string next to it: "new"
I tried:
filedata->string_table + (X)->sh_name + "new"
but this won't compile and I get:
readelf.c:6807:83: error: invalid operands to binary + (have ‘char *’ and ‘char *’)
filedata->string_table + (X)->sh_name : filedata->string_table + (X)->sh_name+"new")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
How to fix this?
Upvotes: 0
Views: 2173
Reputation: 123458
Remember that C doesn't have an actual string type - strings are stored in arrays of char
, and C doesn't define the +
or =
operators for array types.
You can append strings to a character buffer using strcat
or strncat
:
char foo[] = "This is ";
char bar[] = "a test";
char target[ sizeof foo + sizeof bar ];
strcpy( target, foo ); // copies contents of foo to target buffer
strcat( target, bar ); // appends contents of bar to target buffer
The target buffer must be large enough to accommodate the final string including the terminator. In this example, we took the sizes (not lengths!) of foo
and bar
which are 9 and 7 respectively (including the string terminators). Note that this would not work if they were declared as
char *foo = "This is ";
char *bar = "a test";
because sizeof foo
would only give us the size of the pointer object, not the size of the string to which it points. In that case you'd have to get the size at runtime using strlen
. If you're using a version that supports VLAs (C99 or later), you could simply do
char *foo = "This is ";
char *bar = "a test";
char target[ strlen( foo ) + strlen( bar ) + 1];
If not, you'd have to allocate your buffer dynamically:
char *target = malloc( strlen( foo ) + strlen( bar ) + 1 );
and you'd have to remember to free
it when you're done.
If you have a mix of string and non-string types, you can use sprintf
:
char *name = "blah";
int number = 42;
sprintf( target, "%s-%d", name, number ); // writes "blah-42" to target buffer
Again, the target buffer must be large enough to store the resulting string plus the terminator.
Upvotes: 1
Reputation: 67476
(X)->sh_name+"new"
+
in C does not concatenate the strings. It tries to add pointer to pointer which is invalid.
You need to use function strcat or strncat
Sorry These functions affect the strings but I want a new one –
it is so simple:
char *strcatAlloc(const char * restrict s1, const char * restrict s2)
{
size_t s1len;
char *newstring;
if(s1 && s2)
{
s1len = strlen(s1);
newstring = malloc(s1len + strlen(s2) + 1);
if(newstring)
{
strcpy(newstring, s1);
strcpy(newstring + s1len, s2);
}
}
return newstring;
}
int main(void)
{
// you should check if the function fails. Omitted for the example clarity
printf("`%s`\n", strcatAlloc("hello ", "world"));
printf("`%s`\n", strcatAlloc("", "world"));
printf("`%s`\n", strcatAlloc("hello ", ""));
printf("`%s`\n", strcatAlloc("", ""));
//you should free allocated space
}
or version where you can pass your buffer (it will allocate memory if that buffer is NULL)
char *strcatAlloc(const char * restrict s1, const char * restrict s2, char *buff)
{
size_t s1len;
if(s1 && s2)
{
s1len = strlen(s1);
if(!buff) buff = malloc(s1len + strlen(s2) + 1);
if(buff)
{
strcpy(buff, s1);
strcpy(buff + s1len, s2);
}
}
return buff;
}
int main(void)
{
char s[100];
// you should check if the function fails. Omitted for the example
printf("`%s`\n", strcatAlloc("hello ", "world", NULL));
printf("`%s`\n", strcatAlloc("", "world", NULL));
printf("`%s`\n", strcatAlloc("hello ", "", s)); //you can pass your own large enough buffer
printf("`%s`\n", strcatAlloc("", "", NULL));
//you should free allocaated space
}
Upvotes: 1
Reputation: 420
You cannot concatenate strings
or char
using the operator +
. Use strncat()
or snprintf()
.
Upvotes: 1
Reputation: 75062
Use proper way to concatenate strings.
If you want to add the result to existing buffer, use strcat()
to concatenate strings.
char buffer[1024000]; /* enough size */
strcpy(buffer, filedata->string_table + (X)->sh_name);
strcat(buffer, "new");
You also can use snprintf()
to put the result of concatenation in a buffer.
char buffer[1024000]; /* enough size */
snprintf(buffer, sizeof(buffer), "%s%s",
filedata->string_table + (X)->sh_name, "new");
To print the result to the standard output, use two %s
format specifier to print the two strings in a row.
printf("%s%s",
filedata->string_table + (X)->sh_name, "new");
If the string "new"
is fixed, also you can do like this:
printf("%snew",
filedata->string_table + (X)->sh_name);
Upvotes: 1