What does GCC/C think of operator + when used for concatination?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
What does GCC/C think of operator + when used for concatination?
Am not currently at my Linux machine so can't post the exact program.
I had to attach the `ctrl z` character to a string for some
reason.
I foolishly tried to store the following in a char*:
Code:
"abc" + '(char)26'
The string "abc" did not print, instead some junk characters got
printed as a print output.
The surprising part here is that even after using -Wall, GCC did not
show any warnings/errors/segmentation faults.
What was GCC thinking?
P.S.
The char key attaching to the string is now solved but currently I
wish to know what's the deal with the + symbol in C/GCC.
openSUSE 11.4
Last edited by Aquarius_Girl; 03-14-2013 at 10:36 AM.
Exactly what is in the specification of C-language.
"abc" is a pointer of type 'const char *',
'(char)26' is an integer-literal, the value is not exactly clear (you cannot store 8 characters in a 32 bit integer, so it will be somehow truncated),
their sum is another pointer of type 'const char *'
Edit: it does give a warning:
test.c:7:23: warning: character constant too long for its type
so I suppose it was simply:
working of the + operator is based on a simple principle that
"As long as you don't see a string just add and as soon as you find a string concatinate"
Think about "char" as not a character (part of string) but rather as number/integer type. And a char* not a string, but another number. C treat it that way. When you use "+" on operands: char* (pointer to location in memory) and char (just number) then in result you will get pointer to memory this number bytes offset from orginal pointer. With string shorter than this second operand it will point to unallocated memory and occasionally give SEGFAULT.
In C++ you can create (or use some library) a "String" class, then you can assign to "+" another function, like concatenating other String, char and char* types.
Like eSelix said. Lets assume your string "abc" is stored at memory address #23456, then you add 26 to that string (which is a char *), that will result in a pointer with a value of 23482, pointing to random garbage far after the string and sometimes any attempt to address that string will cause a segfault (access violation on memory not owned by/allowed to the process).
C itself never uses '+' to concatenate anything. There is no such high-level luxury. In C, everything is a simple, low-level binary operation, always dealing with numbers (a pointer is also a number). In C++, many utility classes define such things (operator overloads, aka "use '+' for something completely different") which can then be utilized for actions like concatenation.
Am not currently at my Linux machine so can't post the exact program.
I had to attach the `ctrl z` character to a string for some
reason.
I foolishly tried to store the following in a char*:
Code:
"abc" + '(char)26'
The string "abc" did not print, instead some junk characters got
printed as a print output.
The surprising part here is that even after using -Wall, GCC did not
show any warnings/errors/segmentation faults.
What was GCC thinking?
P.S.
The char key attaching to the string is now solved but currently I
wish to know what's the deal with the + symbol in C/GCC.
openSUSE 11.4
It is "C", not C++.
For C++ you need 'g++' - not 'gcc'.
In "C", as others have already pointed out, you need 'strcat', 'strncat', etc.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.