LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-14-2013, 10:34 AM   #1
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
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.
 
Old 03-14-2013, 10:46 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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:
Code:
"ABC" + (char)26
which is perfectly legal

Last edited by NevemTeve; 03-14-2013 at 10:53 AM.
 
Old 03-14-2013, 11:17 AM   #3
#root
Member
 
Registered: Mar 2013
Location: in my house
Distribution: Ubuntu,Backtrack,Fedora
Posts: 38

Rep: Reputation: 5
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"
 
Old 03-14-2013, 01:01 PM   #4
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Here's what I would do:

Code:
char string[20] = {'a', 'b', 'c', 26};
printf("%s\n", string);
Use strcat to concatenate strings.
 
Old 03-14-2013, 01:52 PM   #5
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,281

Rep: Reputation: 320Reputation: 320Reputation: 320Reputation: 320
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.

Last edited by eSelix; 03-14-2013 at 01:58 PM.
 
Old 03-15-2013, 01:27 AM   #6
bloody
Member
 
Registered: Feb 2013
Location: Berlin
Distribution: Gentoo, Debian
Posts: 172

Rep: Reputation: 25
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.
 
Old 03-15-2013, 03:46 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
I got a warning too.

what were you thinking with that construct??

;-)
 
Old 03-15-2013, 11:21 AM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Anisha Kaul View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] C++ Operator Overloading Within an Already Overloaded Operator mirlin510 Programming 8 04-17-2011 12:02 PM
[SOLVED] SEGMENTATION FAULT using gcc 4.4.4 -O2 , works with gcc 4.1.0 -O2 or gcc 4.4.4 -O1 amir1981 Programming 36 07-26-2010 06:07 PM
(C++) operator = overloading with gcc 4.0.1 Omni Programming 7 10-16-2007 06:30 AM
C++ operator += uman Programming 1 02-20-2005 04:37 PM
File name manipulation with tr, concatination troubles goofyheadedpunk Programming 9 07-06-2004 02:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration