Confused about string constant and string variable [duplicate]
This question already has an answer here:
What is the difference between char s and char *s?
12 answers
char m = "hello";
char *pm = "hello";
Why can't the *pm be modified?
I see someone say:
char pointer is a copy of address,
char array is a copy of content, which is "hello".
my question is this:
- why is
char m = "hello";a copy of the content. - m itself is also the first address of the array, why does the constant string "hello" assign them to different effects?
- Is
char m = 'h','e','l','l','0';a Syntactic sugar tochar m = "hello";
c arrays string
marked as duplicate by Deduplicator, gsamaras
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 10 '18 at 13:06
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 is the difference between char s and char *s?
12 answers
char m = "hello";
char *pm = "hello";
Why can't the *pm be modified?
I see someone say:
char pointer is a copy of address,
char array is a copy of content, which is "hello".
my question is this:
- why is
char m = "hello";a copy of the content. - m itself is also the first address of the array, why does the constant string "hello" assign them to different effects?
- Is
char m = 'h','e','l','l','0';a Syntactic sugar tochar m = "hello";
c arrays string
marked as duplicate by Deduplicator, gsamaras
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 10 '18 at 13:06
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.
2
When searching for answers, did you run across this: What is the difference between char s and char *s?
– WhozCraig
Nov 10 '18 at 12:49
add a comment |
This question already has an answer here:
What is the difference between char s and char *s?
12 answers
char m = "hello";
char *pm = "hello";
Why can't the *pm be modified?
I see someone say:
char pointer is a copy of address,
char array is a copy of content, which is "hello".
my question is this:
- why is
char m = "hello";a copy of the content. - m itself is also the first address of the array, why does the constant string "hello" assign them to different effects?
- Is
char m = 'h','e','l','l','0';a Syntactic sugar tochar m = "hello";
c arrays string
This question already has an answer here:
What is the difference between char s and char *s?
12 answers
char m = "hello";
char *pm = "hello";
Why can't the *pm be modified?
I see someone say:
char pointer is a copy of address,
char array is a copy of content, which is "hello".
my question is this:
- why is
char m = "hello";a copy of the content. - m itself is also the first address of the array, why does the constant string "hello" assign them to different effects?
- Is
char m = 'h','e','l','l','0';a Syntactic sugar tochar m = "hello";
This question already has an answer here:
What is the difference between char s and char *s?
12 answers
c arrays string
c arrays string
asked Nov 10 '18 at 12:24
Tuxzx
71
71
marked as duplicate by Deduplicator, gsamaras
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 10 '18 at 13:06
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 Deduplicator, gsamaras
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 10 '18 at 13:06
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.
2
When searching for answers, did you run across this: What is the difference between char s and char *s?
– WhozCraig
Nov 10 '18 at 12:49
add a comment |
2
When searching for answers, did you run across this: What is the difference between char s and char *s?
– WhozCraig
Nov 10 '18 at 12:49
2
2
When searching for answers, did you run across this: What is the difference between char s and char *s?
– WhozCraig
Nov 10 '18 at 12:49
When searching for answers, did you run across this: What is the difference between char s and char *s?
– WhozCraig
Nov 10 '18 at 12:49
add a comment |
2 Answers
2
active
oldest
votes
char m = "hello"; says:
- Define an array of
charnamedm. - Use the string literal
"hello"to initialize the array. This is somewhat different from other initializations; there is a rule for initializing character arrays from string literals that initializes the array elements from the characters in the string literal. - Set the size of the array from the size of the string literal (including the terminating null character).
- The final result is an array
mwhich you may modify.
char *pm = "hello"; says:
- Define a pointer to
charnamedpm. - Use the string literal
"hello"to initialize the pointer. Since it is not a character array being initialized, the rule for character arrays does not apply. Instead,"hello"is an expression. Since a string literal is itself an array, the rule that arrays are converted to pointers applies. So"hello"is converted to a pointer to its first character. Thenpmis initialized to the value of this pointer. - The final result is that
pmpoints to the array of a string literal, which you may not modify.
In regard to your question:
m itself is also the first address of the array, why does the constant string "hello" assign them to different effects?
First, the name of an array is not the address of the first element of the array. In many circumstances, it effectively acts like that, but there are differences. When an array is used in an expression, it is automatically converted to a pointer to its first element—but not if it is the operand of sizeof or unary &. (And also not if it is a string literal being used to initialize an array.) So, in sizeof m, m is not a pointer to its first element. It is the entire array, and sizeof m produces the size of the entire array, not of the pointer.
Second, using "hello" as the initializer has different effects because of the rule about using string literals to initialize arrays. Specifically, the rule is in C 2018 6.7.9 14, which says:
An array of character type may be initialized by a character string literal or UTF–8 string literal, optionally enclosed in braces. Successive bytes of the string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array.
Third, the string literal is not assigned in these definitions. It is used to initialize the object. Assignment and initialization are different and have different rules. Assignments are expressions and behave much like other expressions. Initializations are part of declarations and have some special syntax and semantics that assignments do not. That is why some source code behaves different in initializations than it does in assignments.
In regard to this question:
Is
char m = 'h','e','l','l','0';a Syntactic sugar tochar m = "hello";
It is the other way around; we would say char m = "hello"; is the sweeter version, whereas char m = 'h','e','l','l','o',0; is raw. But, yes, they have the same effect. (Note I corrected two errors: There should be an 'o' in the list, and the final element should be 0, not '0'. '0' is the character for the digit zero, while 0 is zero, which serves as the null character.)
The compiler behavior when just using the name of the array is a little confusing, especially in relation to 'sizeof', which often is confused with a function, but actually is an operator. So, using '&m' or 'sizeof m' or, as is often seen, 'sizeof(m)' are quite different from using an array's name in any other context.
– GermanNerd
Nov 10 '18 at 13:19
>(And also not if it is a string literal being used to initialize an array.)< Could you elaborate on that? A string literal is just that, it is neither an array, nor has it a name, right?
– GermanNerd
Nov 10 '18 at 13:25
>But, yes, they have the same effect.< I disagree on that. They CAN have the same effect, if used like you did. But using the initialization list syntax basically treats the char array as any other array, in this case an integer array. The string literal initialization is different in that respect; you always have the null-terminator, and you cannot set any elements of the array to negative numbers within the initialization.
– GermanNerd
Nov 10 '18 at 13:30
@GermanNerd: C 2018 6.4.5 tells us about string literals. It says that, in translation phase 7 (the main part of compiling), the characters of a string literal are used to initialize an array, including a terminating null. Then 6.5.1 4 tells us that a “A string literal is a primary expression. It is an lvalue with type as detailed in 6.4.5.” So, in an expression, a string literal is an array.
– Eric Postpischil
Nov 10 '18 at 13:31
@GermanNerd: Re “the initialization list syntax basically treats the char array as any other array”: You are saying that if the array had a different type, the initialization list could have a different effect. True, but I did not write aboutAnyArbitraryType m = 'h',…; I wrote aboutchar m = 'h',….
– Eric Postpischil
Nov 10 '18 at 13:33
|
show 14 more comments
why is char m = "hello"; a copy of the content.
This is the array initialization syntax. The compiler will allocate enough new memory for the character array and then copy the contents of the string literal (not the same as a string constant) into it.
m itself is also the first address of the array, why does the constant string >"hello" assign them to different effects?
Using just the array name to get the equivalent of &m[0] is just a compiler convenience. We also say that the array, when used just by it's name, 'decays' into a pointer to the first element.
When you use char *pm = "Hello"; that is not an initialization, but an assignment. You assign the address of the string constant "Hello" to the pointer pm. The string constant is stored in a different memory segment, usually read-only.
Is char m = 'h','e','l','l','0'; a Syntactic sugar to char m = "hello";
No. The first version will NOT include the terminating null character.
1
Sorry, but there were no assignments, only initializations.
– Deduplicator
Nov 10 '18 at 12:57
@Deduplicator char *pm = "Hello" - well you might say that is is an initialization, but for primitive types that is nothing more than an assignment. The point here is that the array initialization does more than simply assign a single value to the new variable.
– GermanNerd
Nov 10 '18 at 13:02
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
char m = "hello"; says:
- Define an array of
charnamedm. - Use the string literal
"hello"to initialize the array. This is somewhat different from other initializations; there is a rule for initializing character arrays from string literals that initializes the array elements from the characters in the string literal. - Set the size of the array from the size of the string literal (including the terminating null character).
- The final result is an array
mwhich you may modify.
char *pm = "hello"; says:
- Define a pointer to
charnamedpm. - Use the string literal
"hello"to initialize the pointer. Since it is not a character array being initialized, the rule for character arrays does not apply. Instead,"hello"is an expression. Since a string literal is itself an array, the rule that arrays are converted to pointers applies. So"hello"is converted to a pointer to its first character. Thenpmis initialized to the value of this pointer. - The final result is that
pmpoints to the array of a string literal, which you may not modify.
In regard to your question:
m itself is also the first address of the array, why does the constant string "hello" assign them to different effects?
First, the name of an array is not the address of the first element of the array. In many circumstances, it effectively acts like that, but there are differences. When an array is used in an expression, it is automatically converted to a pointer to its first element—but not if it is the operand of sizeof or unary &. (And also not if it is a string literal being used to initialize an array.) So, in sizeof m, m is not a pointer to its first element. It is the entire array, and sizeof m produces the size of the entire array, not of the pointer.
Second, using "hello" as the initializer has different effects because of the rule about using string literals to initialize arrays. Specifically, the rule is in C 2018 6.7.9 14, which says:
An array of character type may be initialized by a character string literal or UTF–8 string literal, optionally enclosed in braces. Successive bytes of the string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array.
Third, the string literal is not assigned in these definitions. It is used to initialize the object. Assignment and initialization are different and have different rules. Assignments are expressions and behave much like other expressions. Initializations are part of declarations and have some special syntax and semantics that assignments do not. That is why some source code behaves different in initializations than it does in assignments.
In regard to this question:
Is
char m = 'h','e','l','l','0';a Syntactic sugar tochar m = "hello";
It is the other way around; we would say char m = "hello"; is the sweeter version, whereas char m = 'h','e','l','l','o',0; is raw. But, yes, they have the same effect. (Note I corrected two errors: There should be an 'o' in the list, and the final element should be 0, not '0'. '0' is the character for the digit zero, while 0 is zero, which serves as the null character.)
The compiler behavior when just using the name of the array is a little confusing, especially in relation to 'sizeof', which often is confused with a function, but actually is an operator. So, using '&m' or 'sizeof m' or, as is often seen, 'sizeof(m)' are quite different from using an array's name in any other context.
– GermanNerd
Nov 10 '18 at 13:19
>(And also not if it is a string literal being used to initialize an array.)< Could you elaborate on that? A string literal is just that, it is neither an array, nor has it a name, right?
– GermanNerd
Nov 10 '18 at 13:25
>But, yes, they have the same effect.< I disagree on that. They CAN have the same effect, if used like you did. But using the initialization list syntax basically treats the char array as any other array, in this case an integer array. The string literal initialization is different in that respect; you always have the null-terminator, and you cannot set any elements of the array to negative numbers within the initialization.
– GermanNerd
Nov 10 '18 at 13:30
@GermanNerd: C 2018 6.4.5 tells us about string literals. It says that, in translation phase 7 (the main part of compiling), the characters of a string literal are used to initialize an array, including a terminating null. Then 6.5.1 4 tells us that a “A string literal is a primary expression. It is an lvalue with type as detailed in 6.4.5.” So, in an expression, a string literal is an array.
– Eric Postpischil
Nov 10 '18 at 13:31
@GermanNerd: Re “the initialization list syntax basically treats the char array as any other array”: You are saying that if the array had a different type, the initialization list could have a different effect. True, but I did not write aboutAnyArbitraryType m = 'h',…; I wrote aboutchar m = 'h',….
– Eric Postpischil
Nov 10 '18 at 13:33
|
show 14 more comments
char m = "hello"; says:
- Define an array of
charnamedm. - Use the string literal
"hello"to initialize the array. This is somewhat different from other initializations; there is a rule for initializing character arrays from string literals that initializes the array elements from the characters in the string literal. - Set the size of the array from the size of the string literal (including the terminating null character).
- The final result is an array
mwhich you may modify.
char *pm = "hello"; says:
- Define a pointer to
charnamedpm. - Use the string literal
"hello"to initialize the pointer. Since it is not a character array being initialized, the rule for character arrays does not apply. Instead,"hello"is an expression. Since a string literal is itself an array, the rule that arrays are converted to pointers applies. So"hello"is converted to a pointer to its first character. Thenpmis initialized to the value of this pointer. - The final result is that
pmpoints to the array of a string literal, which you may not modify.
In regard to your question:
m itself is also the first address of the array, why does the constant string "hello" assign them to different effects?
First, the name of an array is not the address of the first element of the array. In many circumstances, it effectively acts like that, but there are differences. When an array is used in an expression, it is automatically converted to a pointer to its first element—but not if it is the operand of sizeof or unary &. (And also not if it is a string literal being used to initialize an array.) So, in sizeof m, m is not a pointer to its first element. It is the entire array, and sizeof m produces the size of the entire array, not of the pointer.
Second, using "hello" as the initializer has different effects because of the rule about using string literals to initialize arrays. Specifically, the rule is in C 2018 6.7.9 14, which says:
An array of character type may be initialized by a character string literal or UTF–8 string literal, optionally enclosed in braces. Successive bytes of the string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array.
Third, the string literal is not assigned in these definitions. It is used to initialize the object. Assignment and initialization are different and have different rules. Assignments are expressions and behave much like other expressions. Initializations are part of declarations and have some special syntax and semantics that assignments do not. That is why some source code behaves different in initializations than it does in assignments.
In regard to this question:
Is
char m = 'h','e','l','l','0';a Syntactic sugar tochar m = "hello";
It is the other way around; we would say char m = "hello"; is the sweeter version, whereas char m = 'h','e','l','l','o',0; is raw. But, yes, they have the same effect. (Note I corrected two errors: There should be an 'o' in the list, and the final element should be 0, not '0'. '0' is the character for the digit zero, while 0 is zero, which serves as the null character.)
The compiler behavior when just using the name of the array is a little confusing, especially in relation to 'sizeof', which often is confused with a function, but actually is an operator. So, using '&m' or 'sizeof m' or, as is often seen, 'sizeof(m)' are quite different from using an array's name in any other context.
– GermanNerd
Nov 10 '18 at 13:19
>(And also not if it is a string literal being used to initialize an array.)< Could you elaborate on that? A string literal is just that, it is neither an array, nor has it a name, right?
– GermanNerd
Nov 10 '18 at 13:25
>But, yes, they have the same effect.< I disagree on that. They CAN have the same effect, if used like you did. But using the initialization list syntax basically treats the char array as any other array, in this case an integer array. The string literal initialization is different in that respect; you always have the null-terminator, and you cannot set any elements of the array to negative numbers within the initialization.
– GermanNerd
Nov 10 '18 at 13:30
@GermanNerd: C 2018 6.4.5 tells us about string literals. It says that, in translation phase 7 (the main part of compiling), the characters of a string literal are used to initialize an array, including a terminating null. Then 6.5.1 4 tells us that a “A string literal is a primary expression. It is an lvalue with type as detailed in 6.4.5.” So, in an expression, a string literal is an array.
– Eric Postpischil
Nov 10 '18 at 13:31
@GermanNerd: Re “the initialization list syntax basically treats the char array as any other array”: You are saying that if the array had a different type, the initialization list could have a different effect. True, but I did not write aboutAnyArbitraryType m = 'h',…; I wrote aboutchar m = 'h',….
– Eric Postpischil
Nov 10 '18 at 13:33
|
show 14 more comments
char m = "hello"; says:
- Define an array of
charnamedm. - Use the string literal
"hello"to initialize the array. This is somewhat different from other initializations; there is a rule for initializing character arrays from string literals that initializes the array elements from the characters in the string literal. - Set the size of the array from the size of the string literal (including the terminating null character).
- The final result is an array
mwhich you may modify.
char *pm = "hello"; says:
- Define a pointer to
charnamedpm. - Use the string literal
"hello"to initialize the pointer. Since it is not a character array being initialized, the rule for character arrays does not apply. Instead,"hello"is an expression. Since a string literal is itself an array, the rule that arrays are converted to pointers applies. So"hello"is converted to a pointer to its first character. Thenpmis initialized to the value of this pointer. - The final result is that
pmpoints to the array of a string literal, which you may not modify.
In regard to your question:
m itself is also the first address of the array, why does the constant string "hello" assign them to different effects?
First, the name of an array is not the address of the first element of the array. In many circumstances, it effectively acts like that, but there are differences. When an array is used in an expression, it is automatically converted to a pointer to its first element—but not if it is the operand of sizeof or unary &. (And also not if it is a string literal being used to initialize an array.) So, in sizeof m, m is not a pointer to its first element. It is the entire array, and sizeof m produces the size of the entire array, not of the pointer.
Second, using "hello" as the initializer has different effects because of the rule about using string literals to initialize arrays. Specifically, the rule is in C 2018 6.7.9 14, which says:
An array of character type may be initialized by a character string literal or UTF–8 string literal, optionally enclosed in braces. Successive bytes of the string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array.
Third, the string literal is not assigned in these definitions. It is used to initialize the object. Assignment and initialization are different and have different rules. Assignments are expressions and behave much like other expressions. Initializations are part of declarations and have some special syntax and semantics that assignments do not. That is why some source code behaves different in initializations than it does in assignments.
In regard to this question:
Is
char m = 'h','e','l','l','0';a Syntactic sugar tochar m = "hello";
It is the other way around; we would say char m = "hello"; is the sweeter version, whereas char m = 'h','e','l','l','o',0; is raw. But, yes, they have the same effect. (Note I corrected two errors: There should be an 'o' in the list, and the final element should be 0, not '0'. '0' is the character for the digit zero, while 0 is zero, which serves as the null character.)
char m = "hello"; says:
- Define an array of
charnamedm. - Use the string literal
"hello"to initialize the array. This is somewhat different from other initializations; there is a rule for initializing character arrays from string literals that initializes the array elements from the characters in the string literal. - Set the size of the array from the size of the string literal (including the terminating null character).
- The final result is an array
mwhich you may modify.
char *pm = "hello"; says:
- Define a pointer to
charnamedpm. - Use the string literal
"hello"to initialize the pointer. Since it is not a character array being initialized, the rule for character arrays does not apply. Instead,"hello"is an expression. Since a string literal is itself an array, the rule that arrays are converted to pointers applies. So"hello"is converted to a pointer to its first character. Thenpmis initialized to the value of this pointer. - The final result is that
pmpoints to the array of a string literal, which you may not modify.
In regard to your question:
m itself is also the first address of the array, why does the constant string "hello" assign them to different effects?
First, the name of an array is not the address of the first element of the array. In many circumstances, it effectively acts like that, but there are differences. When an array is used in an expression, it is automatically converted to a pointer to its first element—but not if it is the operand of sizeof or unary &. (And also not if it is a string literal being used to initialize an array.) So, in sizeof m, m is not a pointer to its first element. It is the entire array, and sizeof m produces the size of the entire array, not of the pointer.
Second, using "hello" as the initializer has different effects because of the rule about using string literals to initialize arrays. Specifically, the rule is in C 2018 6.7.9 14, which says:
An array of character type may be initialized by a character string literal or UTF–8 string literal, optionally enclosed in braces. Successive bytes of the string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array.
Third, the string literal is not assigned in these definitions. It is used to initialize the object. Assignment and initialization are different and have different rules. Assignments are expressions and behave much like other expressions. Initializations are part of declarations and have some special syntax and semantics that assignments do not. That is why some source code behaves different in initializations than it does in assignments.
In regard to this question:
Is
char m = 'h','e','l','l','0';a Syntactic sugar tochar m = "hello";
It is the other way around; we would say char m = "hello"; is the sweeter version, whereas char m = 'h','e','l','l','o',0; is raw. But, yes, they have the same effect. (Note I corrected two errors: There should be an 'o' in the list, and the final element should be 0, not '0'. '0' is the character for the digit zero, while 0 is zero, which serves as the null character.)
edited Nov 10 '18 at 13:20
answered Nov 10 '18 at 13:04
Eric Postpischil
71.7k877156
71.7k877156
The compiler behavior when just using the name of the array is a little confusing, especially in relation to 'sizeof', which often is confused with a function, but actually is an operator. So, using '&m' or 'sizeof m' or, as is often seen, 'sizeof(m)' are quite different from using an array's name in any other context.
– GermanNerd
Nov 10 '18 at 13:19
>(And also not if it is a string literal being used to initialize an array.)< Could you elaborate on that? A string literal is just that, it is neither an array, nor has it a name, right?
– GermanNerd
Nov 10 '18 at 13:25
>But, yes, they have the same effect.< I disagree on that. They CAN have the same effect, if used like you did. But using the initialization list syntax basically treats the char array as any other array, in this case an integer array. The string literal initialization is different in that respect; you always have the null-terminator, and you cannot set any elements of the array to negative numbers within the initialization.
– GermanNerd
Nov 10 '18 at 13:30
@GermanNerd: C 2018 6.4.5 tells us about string literals. It says that, in translation phase 7 (the main part of compiling), the characters of a string literal are used to initialize an array, including a terminating null. Then 6.5.1 4 tells us that a “A string literal is a primary expression. It is an lvalue with type as detailed in 6.4.5.” So, in an expression, a string literal is an array.
– Eric Postpischil
Nov 10 '18 at 13:31
@GermanNerd: Re “the initialization list syntax basically treats the char array as any other array”: You are saying that if the array had a different type, the initialization list could have a different effect. True, but I did not write aboutAnyArbitraryType m = 'h',…; I wrote aboutchar m = 'h',….
– Eric Postpischil
Nov 10 '18 at 13:33
|
show 14 more comments
The compiler behavior when just using the name of the array is a little confusing, especially in relation to 'sizeof', which often is confused with a function, but actually is an operator. So, using '&m' or 'sizeof m' or, as is often seen, 'sizeof(m)' are quite different from using an array's name in any other context.
– GermanNerd
Nov 10 '18 at 13:19
>(And also not if it is a string literal being used to initialize an array.)< Could you elaborate on that? A string literal is just that, it is neither an array, nor has it a name, right?
– GermanNerd
Nov 10 '18 at 13:25
>But, yes, they have the same effect.< I disagree on that. They CAN have the same effect, if used like you did. But using the initialization list syntax basically treats the char array as any other array, in this case an integer array. The string literal initialization is different in that respect; you always have the null-terminator, and you cannot set any elements of the array to negative numbers within the initialization.
– GermanNerd
Nov 10 '18 at 13:30
@GermanNerd: C 2018 6.4.5 tells us about string literals. It says that, in translation phase 7 (the main part of compiling), the characters of a string literal are used to initialize an array, including a terminating null. Then 6.5.1 4 tells us that a “A string literal is a primary expression. It is an lvalue with type as detailed in 6.4.5.” So, in an expression, a string literal is an array.
– Eric Postpischil
Nov 10 '18 at 13:31
@GermanNerd: Re “the initialization list syntax basically treats the char array as any other array”: You are saying that if the array had a different type, the initialization list could have a different effect. True, but I did not write aboutAnyArbitraryType m = 'h',…; I wrote aboutchar m = 'h',….
– Eric Postpischil
Nov 10 '18 at 13:33
The compiler behavior when just using the name of the array is a little confusing, especially in relation to 'sizeof', which often is confused with a function, but actually is an operator. So, using '&m' or 'sizeof m' or, as is often seen, 'sizeof(m)' are quite different from using an array's name in any other context.
– GermanNerd
Nov 10 '18 at 13:19
The compiler behavior when just using the name of the array is a little confusing, especially in relation to 'sizeof', which often is confused with a function, but actually is an operator. So, using '&m' or 'sizeof m' or, as is often seen, 'sizeof(m)' are quite different from using an array's name in any other context.
– GermanNerd
Nov 10 '18 at 13:19
>(And also not if it is a string literal being used to initialize an array.)< Could you elaborate on that? A string literal is just that, it is neither an array, nor has it a name, right?
– GermanNerd
Nov 10 '18 at 13:25
>(And also not if it is a string literal being used to initialize an array.)< Could you elaborate on that? A string literal is just that, it is neither an array, nor has it a name, right?
– GermanNerd
Nov 10 '18 at 13:25
>But, yes, they have the same effect.< I disagree on that. They CAN have the same effect, if used like you did. But using the initialization list syntax basically treats the char array as any other array, in this case an integer array. The string literal initialization is different in that respect; you always have the null-terminator, and you cannot set any elements of the array to negative numbers within the initialization.
– GermanNerd
Nov 10 '18 at 13:30
>But, yes, they have the same effect.< I disagree on that. They CAN have the same effect, if used like you did. But using the initialization list syntax basically treats the char array as any other array, in this case an integer array. The string literal initialization is different in that respect; you always have the null-terminator, and you cannot set any elements of the array to negative numbers within the initialization.
– GermanNerd
Nov 10 '18 at 13:30
@GermanNerd: C 2018 6.4.5 tells us about string literals. It says that, in translation phase 7 (the main part of compiling), the characters of a string literal are used to initialize an array, including a terminating null. Then 6.5.1 4 tells us that a “A string literal is a primary expression. It is an lvalue with type as detailed in 6.4.5.” So, in an expression, a string literal is an array.
– Eric Postpischil
Nov 10 '18 at 13:31
@GermanNerd: C 2018 6.4.5 tells us about string literals. It says that, in translation phase 7 (the main part of compiling), the characters of a string literal are used to initialize an array, including a terminating null. Then 6.5.1 4 tells us that a “A string literal is a primary expression. It is an lvalue with type as detailed in 6.4.5.” So, in an expression, a string literal is an array.
– Eric Postpischil
Nov 10 '18 at 13:31
@GermanNerd: Re “the initialization list syntax basically treats the char array as any other array”: You are saying that if the array had a different type, the initialization list could have a different effect. True, but I did not write about
AnyArbitraryType m = 'h',…; I wrote about char m = 'h',….– Eric Postpischil
Nov 10 '18 at 13:33
@GermanNerd: Re “the initialization list syntax basically treats the char array as any other array”: You are saying that if the array had a different type, the initialization list could have a different effect. True, but I did not write about
AnyArbitraryType m = 'h',…; I wrote about char m = 'h',….– Eric Postpischil
Nov 10 '18 at 13:33
|
show 14 more comments
why is char m = "hello"; a copy of the content.
This is the array initialization syntax. The compiler will allocate enough new memory for the character array and then copy the contents of the string literal (not the same as a string constant) into it.
m itself is also the first address of the array, why does the constant string >"hello" assign them to different effects?
Using just the array name to get the equivalent of &m[0] is just a compiler convenience. We also say that the array, when used just by it's name, 'decays' into a pointer to the first element.
When you use char *pm = "Hello"; that is not an initialization, but an assignment. You assign the address of the string constant "Hello" to the pointer pm. The string constant is stored in a different memory segment, usually read-only.
Is char m = 'h','e','l','l','0'; a Syntactic sugar to char m = "hello";
No. The first version will NOT include the terminating null character.
1
Sorry, but there were no assignments, only initializations.
– Deduplicator
Nov 10 '18 at 12:57
@Deduplicator char *pm = "Hello" - well you might say that is is an initialization, but for primitive types that is nothing more than an assignment. The point here is that the array initialization does more than simply assign a single value to the new variable.
– GermanNerd
Nov 10 '18 at 13:02
add a comment |
why is char m = "hello"; a copy of the content.
This is the array initialization syntax. The compiler will allocate enough new memory for the character array and then copy the contents of the string literal (not the same as a string constant) into it.
m itself is also the first address of the array, why does the constant string >"hello" assign them to different effects?
Using just the array name to get the equivalent of &m[0] is just a compiler convenience. We also say that the array, when used just by it's name, 'decays' into a pointer to the first element.
When you use char *pm = "Hello"; that is not an initialization, but an assignment. You assign the address of the string constant "Hello" to the pointer pm. The string constant is stored in a different memory segment, usually read-only.
Is char m = 'h','e','l','l','0'; a Syntactic sugar to char m = "hello";
No. The first version will NOT include the terminating null character.
1
Sorry, but there were no assignments, only initializations.
– Deduplicator
Nov 10 '18 at 12:57
@Deduplicator char *pm = "Hello" - well you might say that is is an initialization, but for primitive types that is nothing more than an assignment. The point here is that the array initialization does more than simply assign a single value to the new variable.
– GermanNerd
Nov 10 '18 at 13:02
add a comment |
why is char m = "hello"; a copy of the content.
This is the array initialization syntax. The compiler will allocate enough new memory for the character array and then copy the contents of the string literal (not the same as a string constant) into it.
m itself is also the first address of the array, why does the constant string >"hello" assign them to different effects?
Using just the array name to get the equivalent of &m[0] is just a compiler convenience. We also say that the array, when used just by it's name, 'decays' into a pointer to the first element.
When you use char *pm = "Hello"; that is not an initialization, but an assignment. You assign the address of the string constant "Hello" to the pointer pm. The string constant is stored in a different memory segment, usually read-only.
Is char m = 'h','e','l','l','0'; a Syntactic sugar to char m = "hello";
No. The first version will NOT include the terminating null character.
why is char m = "hello"; a copy of the content.
This is the array initialization syntax. The compiler will allocate enough new memory for the character array and then copy the contents of the string literal (not the same as a string constant) into it.
m itself is also the first address of the array, why does the constant string >"hello" assign them to different effects?
Using just the array name to get the equivalent of &m[0] is just a compiler convenience. We also say that the array, when used just by it's name, 'decays' into a pointer to the first element.
When you use char *pm = "Hello"; that is not an initialization, but an assignment. You assign the address of the string constant "Hello" to the pointer pm. The string constant is stored in a different memory segment, usually read-only.
Is char m = 'h','e','l','l','0'; a Syntactic sugar to char m = "hello";
No. The first version will NOT include the terminating null character.
edited Nov 10 '18 at 17:25
answered Nov 10 '18 at 12:53
GermanNerd
462111
462111
1
Sorry, but there were no assignments, only initializations.
– Deduplicator
Nov 10 '18 at 12:57
@Deduplicator char *pm = "Hello" - well you might say that is is an initialization, but for primitive types that is nothing more than an assignment. The point here is that the array initialization does more than simply assign a single value to the new variable.
– GermanNerd
Nov 10 '18 at 13:02
add a comment |
1
Sorry, but there were no assignments, only initializations.
– Deduplicator
Nov 10 '18 at 12:57
@Deduplicator char *pm = "Hello" - well you might say that is is an initialization, but for primitive types that is nothing more than an assignment. The point here is that the array initialization does more than simply assign a single value to the new variable.
– GermanNerd
Nov 10 '18 at 13:02
1
1
Sorry, but there were no assignments, only initializations.
– Deduplicator
Nov 10 '18 at 12:57
Sorry, but there were no assignments, only initializations.
– Deduplicator
Nov 10 '18 at 12:57
@Deduplicator char *pm = "Hello" - well you might say that is is an initialization, but for primitive types that is nothing more than an assignment. The point here is that the array initialization does more than simply assign a single value to the new variable.
– GermanNerd
Nov 10 '18 at 13:02
@Deduplicator char *pm = "Hello" - well you might say that is is an initialization, but for primitive types that is nothing more than an assignment. The point here is that the array initialization does more than simply assign a single value to the new variable.
– GermanNerd
Nov 10 '18 at 13:02
add a comment |
2
When searching for answers, did you run across this: What is the difference between char s and char *s?
– WhozCraig
Nov 10 '18 at 12:49