Strange string operator= behaviour in C++ [duplicate]
This question already has an answer here:
What does the comma operator , do?
9 answers
How does the Comma Operator work
9 answers
While debugging a program that other people wrote I encountered a strange string assignment. At first I was surprised that it even compiles. Here is an example, which compiles without warnings on Linux (Ubuntu, CentOS).
#include <string>
#include <stdio.h>
#include <string.h>
using namespace std;
int main ()
{
string a;
char b[40];
a = "Constant value", strncpy (b, a.c_str (), sizeof (b));
printf ("a = %sn", a.c_str ());
printf ("b = %sn", b);
a = "Constant value";
strncpy (b, a.c_str (), sizeof (b));
printf ("a = %sn", a.c_str ());
printf ("b = %sn", b);
}
Can someone explain, what exactly does the first string assignment in the example, and where can I find any reference describing this behaviour? As you can see a
is assigned a constant string, but after that there is a comma (,
) and strncpy
function call, which returns char *
. Why is comma accepted there? According to output it does not make a difference if I use ;
or ,
.
c++ string
marked as duplicate by πάντα ῥεῖ
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 '18 at 7:24
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
What does the comma operator , do?
9 answers
How does the Comma Operator work
9 answers
While debugging a program that other people wrote I encountered a strange string assignment. At first I was surprised that it even compiles. Here is an example, which compiles without warnings on Linux (Ubuntu, CentOS).
#include <string>
#include <stdio.h>
#include <string.h>
using namespace std;
int main ()
{
string a;
char b[40];
a = "Constant value", strncpy (b, a.c_str (), sizeof (b));
printf ("a = %sn", a.c_str ());
printf ("b = %sn", b);
a = "Constant value";
strncpy (b, a.c_str (), sizeof (b));
printf ("a = %sn", a.c_str ());
printf ("b = %sn", b);
}
Can someone explain, what exactly does the first string assignment in the example, and where can I find any reference describing this behaviour? As you can see a
is assigned a constant string, but after that there is a comma (,
) and strncpy
function call, which returns char *
. Why is comma accepted there? According to output it does not make a difference if I use ;
or ,
.
c++ string
marked as duplicate by πάντα ῥεῖ
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 '18 at 7:24
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
5
What does the comma operator , do?
– Shafik Yaghmour
Nov 14 '18 at 6:32
4
"How does the comma operator work?"
– WhozCraig
Nov 14 '18 at 6:32
Note thatstrncpy
, carelessly used as it is here, isn't any "safer" than a plainstrcpy
; it just moves the crash on an over-length string to some other place. If you're going to usestrncpy
(which you should only rarely do; it's not intended as a general string copy), read and digest its documentation.
– Pete Becker
Nov 14 '18 at 14:53
add a comment |
This question already has an answer here:
What does the comma operator , do?
9 answers
How does the Comma Operator work
9 answers
While debugging a program that other people wrote I encountered a strange string assignment. At first I was surprised that it even compiles. Here is an example, which compiles without warnings on Linux (Ubuntu, CentOS).
#include <string>
#include <stdio.h>
#include <string.h>
using namespace std;
int main ()
{
string a;
char b[40];
a = "Constant value", strncpy (b, a.c_str (), sizeof (b));
printf ("a = %sn", a.c_str ());
printf ("b = %sn", b);
a = "Constant value";
strncpy (b, a.c_str (), sizeof (b));
printf ("a = %sn", a.c_str ());
printf ("b = %sn", b);
}
Can someone explain, what exactly does the first string assignment in the example, and where can I find any reference describing this behaviour? As you can see a
is assigned a constant string, but after that there is a comma (,
) and strncpy
function call, which returns char *
. Why is comma accepted there? According to output it does not make a difference if I use ;
or ,
.
c++ string
This question already has an answer here:
What does the comma operator , do?
9 answers
How does the Comma Operator work
9 answers
While debugging a program that other people wrote I encountered a strange string assignment. At first I was surprised that it even compiles. Here is an example, which compiles without warnings on Linux (Ubuntu, CentOS).
#include <string>
#include <stdio.h>
#include <string.h>
using namespace std;
int main ()
{
string a;
char b[40];
a = "Constant value", strncpy (b, a.c_str (), sizeof (b));
printf ("a = %sn", a.c_str ());
printf ("b = %sn", b);
a = "Constant value";
strncpy (b, a.c_str (), sizeof (b));
printf ("a = %sn", a.c_str ());
printf ("b = %sn", b);
}
Can someone explain, what exactly does the first string assignment in the example, and where can I find any reference describing this behaviour? As you can see a
is assigned a constant string, but after that there is a comma (,
) and strncpy
function call, which returns char *
. Why is comma accepted there? According to output it does not make a difference if I use ;
or ,
.
This question already has an answer here:
What does the comma operator , do?
9 answers
How does the Comma Operator work
9 answers
c++ string
c++ string
edited Nov 14 '18 at 6:32
nobody
asked Nov 14 '18 at 6:29
nobodynobody
16616
16616
marked as duplicate by πάντα ῥεῖ
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 '18 at 7:24
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by πάντα ῥεῖ
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 '18 at 7:24
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
5
What does the comma operator , do?
– Shafik Yaghmour
Nov 14 '18 at 6:32
4
"How does the comma operator work?"
– WhozCraig
Nov 14 '18 at 6:32
Note thatstrncpy
, carelessly used as it is here, isn't any "safer" than a plainstrcpy
; it just moves the crash on an over-length string to some other place. If you're going to usestrncpy
(which you should only rarely do; it's not intended as a general string copy), read and digest its documentation.
– Pete Becker
Nov 14 '18 at 14:53
add a comment |
5
What does the comma operator , do?
– Shafik Yaghmour
Nov 14 '18 at 6:32
4
"How does the comma operator work?"
– WhozCraig
Nov 14 '18 at 6:32
Note thatstrncpy
, carelessly used as it is here, isn't any "safer" than a plainstrcpy
; it just moves the crash on an over-length string to some other place. If you're going to usestrncpy
(which you should only rarely do; it's not intended as a general string copy), read and digest its documentation.
– Pete Becker
Nov 14 '18 at 14:53
5
5
What does the comma operator , do?
– Shafik Yaghmour
Nov 14 '18 at 6:32
What does the comma operator , do?
– Shafik Yaghmour
Nov 14 '18 at 6:32
4
4
"How does the comma operator work?"
– WhozCraig
Nov 14 '18 at 6:32
"How does the comma operator work?"
– WhozCraig
Nov 14 '18 at 6:32
Note that
strncpy
, carelessly used as it is here, isn't any "safer" than a plain strcpy
; it just moves the crash on an over-length string to some other place. If you're going to use strncpy
(which you should only rarely do; it's not intended as a general string copy), read and digest its documentation.– Pete Becker
Nov 14 '18 at 14:53
Note that
strncpy
, carelessly used as it is here, isn't any "safer" than a plain strcpy
; it just moves the crash on an over-length string to some other place. If you're going to use strncpy
(which you should only rarely do; it's not intended as a general string copy), read and digest its documentation.– Pete Becker
Nov 14 '18 at 14:53
add a comment |
1 Answer
1
active
oldest
votes
"," represents an operator. The order of "," is from left to right, for example, the value of (A, B, C) is C.
";" represents the end of a sentence. The execution sequence of the sentence has not changed, so the result is the same.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
"," represents an operator. The order of "," is from left to right, for example, the value of (A, B, C) is C.
";" represents the end of a sentence. The execution sequence of the sentence has not changed, so the result is the same.
add a comment |
"," represents an operator. The order of "," is from left to right, for example, the value of (A, B, C) is C.
";" represents the end of a sentence. The execution sequence of the sentence has not changed, so the result is the same.
add a comment |
"," represents an operator. The order of "," is from left to right, for example, the value of (A, B, C) is C.
";" represents the end of a sentence. The execution sequence of the sentence has not changed, so the result is the same.
"," represents an operator. The order of "," is from left to right, for example, the value of (A, B, C) is C.
";" represents the end of a sentence. The execution sequence of the sentence has not changed, so the result is the same.
answered Nov 14 '18 at 7:07
Drake Wu - MSFTDrake Wu - MSFT
2425
2425
add a comment |
add a comment |
5
What does the comma operator , do?
– Shafik Yaghmour
Nov 14 '18 at 6:32
4
"How does the comma operator work?"
– WhozCraig
Nov 14 '18 at 6:32
Note that
strncpy
, carelessly used as it is here, isn't any "safer" than a plainstrcpy
; it just moves the crash on an over-length string to some other place. If you're going to usestrncpy
(which you should only rarely do; it's not intended as a general string copy), read and digest its documentation.– Pete Becker
Nov 14 '18 at 14:53