Can it cause problems to pass the address to an array instead of the array?
up vote
6
down vote
favorite
I ran into this code:
char str[600];
scanf("%s", &str);
Of course, this emits this warning:
a.c:6:17: warning: format specifies type 'char *' but the argument has type
'char (*)[600]' [-Wformat]
scanf("%s", &str);
~~ ^~~~~~~
I know that the correct way is to remove the &
and type scanf("%s", str)
instead. But it does work, so my question is if this could cause any problems. Is it UB? When I switched str
to a pointer instead of an array it (obviously) did not work. But can this cause any problem when using an array?
(I had a discussion about an answer here, where the answerer did not want to change his answer to the correct way)
c arrays
add a comment |
up vote
6
down vote
favorite
I ran into this code:
char str[600];
scanf("%s", &str);
Of course, this emits this warning:
a.c:6:17: warning: format specifies type 'char *' but the argument has type
'char (*)[600]' [-Wformat]
scanf("%s", &str);
~~ ^~~~~~~
I know that the correct way is to remove the &
and type scanf("%s", str)
instead. But it does work, so my question is if this could cause any problems. Is it UB? When I switched str
to a pointer instead of an array it (obviously) did not work. But can this cause any problem when using an array?
(I had a discussion about an answer here, where the answerer did not want to change his answer to the correct way)
c arrays
add a comment |
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I ran into this code:
char str[600];
scanf("%s", &str);
Of course, this emits this warning:
a.c:6:17: warning: format specifies type 'char *' but the argument has type
'char (*)[600]' [-Wformat]
scanf("%s", &str);
~~ ^~~~~~~
I know that the correct way is to remove the &
and type scanf("%s", str)
instead. But it does work, so my question is if this could cause any problems. Is it UB? When I switched str
to a pointer instead of an array it (obviously) did not work. But can this cause any problem when using an array?
(I had a discussion about an answer here, where the answerer did not want to change his answer to the correct way)
c arrays
I ran into this code:
char str[600];
scanf("%s", &str);
Of course, this emits this warning:
a.c:6:17: warning: format specifies type 'char *' but the argument has type
'char (*)[600]' [-Wformat]
scanf("%s", &str);
~~ ^~~~~~~
I know that the correct way is to remove the &
and type scanf("%s", str)
instead. But it does work, so my question is if this could cause any problems. Is it UB? When I switched str
to a pointer instead of an array it (obviously) did not work. But can this cause any problem when using an array?
(I had a discussion about an answer here, where the answerer did not want to change his answer to the correct way)
c arrays
c arrays
asked 6 hours ago
Broman
6,068102241
6,068102241
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
4
down vote
Yes, the code is undefined behaviour. The argument corresponding to %s
must have the type char *
. This is described in C17 7.21.6.2/12 under the s
specifier:
[...] the corresponding argument shall be a pointer to the initial element of a character array large enough to accept the sequence and a terminating null character, which will be added automatically.
which says fairly clearly that the pointer should have pointer-to-character type, and not point to the whole array.
Undefined behaviour means that anything can happen. It might behave as if you omitted the &
, or it might format your hard drive.
Given that it is extremely easy to avoid undefined behaviour in this case, I don't really see any reason to engage in arguments about whether it is OK to rely on the behaviour of undefined behaviour in this situation.
7.19.6.1? Do you mean 7.21.6.1 The fprintf function? And why doesn't 6.2.7 Compatible type and composite type "save" the posted code from UB? The address of the array is the address of the first element, which has to be compatible with the type of a pointer to the same type as an element of the array? I tend to agree with your answer that's it UB because 7.21.6.1 says it's UB, but I can see how an argument via compatible types and array decay can be made. And that's way, way, waaay down the language lawyer rabbit hole...
– Andrew Henle
4 hours ago
@AndrewHenle Nice catch. I was to quick there.
– Broman
4 hours ago
@AndrewHenle -- "The address of the array is the address of the first element, which has to be compatible with the type of a pointer to the same type as an element of the array?": I am not sure what you are trying to say here, but it sounds like you are saying that a pointer to an array and a pointer to the first element of that array are compatible types. I can't see any way that the Standard supports this, given that two types have compatible type if their types are the same (plus a few rules that don't appear to apply here).
– David Bowling
4 hours ago
The standard says this aboutfscanf
: If this object does not have an appropriate type, or if the result of the conversion cannot be represented in the object, the behavior is undefined.
– Broman
3 hours ago
@AndrewHenle this is the scanf function,, not the printf function.char *
andchar (*)[600]
are not compatible types (that term is defined by C17 6.2.7)
– M.M
3 hours ago
|
show 10 more comments
up vote
2
down vote
Using &str
instead of str
didn't cause any problems in this case because the addresses of those two are the same. See this past question for an explanation. But as you note, the type of &str
is different, and the compiler throws up a warning, and the actual behavior will depend on architecture and implementation.
New contributor
It might not cause problems in common architectures - but strictly speaking, it's not allowed. And one thing that might break it: there's no guarantee that different pointer types all use the same representation for the same address. So here the types in questionchar*
andchar (*)[600]
might have different sizes or value representations.
– aschepler
4 hours ago
@aschepler Fair enough. I suppose any behavior not defined in the spec is going to be implementation- or architecture-specific. Better to write it the correct way to begin with.
– Paul
4 hours ago
@Paul -- "any behavior not defined in the spec": It isn't that the behavior is left undefined, so much as that the Standard says explicitly that the behavior is undefined. Passing the wrong types to functions is not a gray area.
– David Bowling
4 hours ago
Yes, though the difference between a behavior not being defined and being explicitly undefined doesn't seem that different. I'll update my answer to say that it "didn't" cause any problems in this case rather than it "wouldn't" cause any problems.
– Paul
4 hours ago
1
@Paul -- compiler writers may take advantage of the fact that some constructs lead to explicit undefined behavior (and hence should never show up in valid C programs) to make some optimizations. I would say that this is a significant difference compared with undefined-by-omission behaviors, and certainly with the many implementation-defined behaviors given in the Standard.
– David Bowling
4 hours ago
|
show 1 more comment
up vote
-1
down vote
in C the name of an array is also its address (points to the beginning of the array).
New contributor
1
In the question,str
ischar[600]
and&str
ischar (*)[600]
(the latter is mentioned in the warning message).
– anatolyg
5 hours ago
This isn't true. Arrays are objects in C (in the C sense), and array identifiers refer to array objects. Arrays do decay to pointers to their first elements in most expressions (but not in all expressions). In particular, withchar arr = "abc"; size_t arr_sz = sizeof arr;
the identifierarr
not only refers to an array, it does not decay to a pointer in thesizeof
expression.
– David Bowling
4 hours ago
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
Yes, the code is undefined behaviour. The argument corresponding to %s
must have the type char *
. This is described in C17 7.21.6.2/12 under the s
specifier:
[...] the corresponding argument shall be a pointer to the initial element of a character array large enough to accept the sequence and a terminating null character, which will be added automatically.
which says fairly clearly that the pointer should have pointer-to-character type, and not point to the whole array.
Undefined behaviour means that anything can happen. It might behave as if you omitted the &
, or it might format your hard drive.
Given that it is extremely easy to avoid undefined behaviour in this case, I don't really see any reason to engage in arguments about whether it is OK to rely on the behaviour of undefined behaviour in this situation.
7.19.6.1? Do you mean 7.21.6.1 The fprintf function? And why doesn't 6.2.7 Compatible type and composite type "save" the posted code from UB? The address of the array is the address of the first element, which has to be compatible with the type of a pointer to the same type as an element of the array? I tend to agree with your answer that's it UB because 7.21.6.1 says it's UB, but I can see how an argument via compatible types and array decay can be made. And that's way, way, waaay down the language lawyer rabbit hole...
– Andrew Henle
4 hours ago
@AndrewHenle Nice catch. I was to quick there.
– Broman
4 hours ago
@AndrewHenle -- "The address of the array is the address of the first element, which has to be compatible with the type of a pointer to the same type as an element of the array?": I am not sure what you are trying to say here, but it sounds like you are saying that a pointer to an array and a pointer to the first element of that array are compatible types. I can't see any way that the Standard supports this, given that two types have compatible type if their types are the same (plus a few rules that don't appear to apply here).
– David Bowling
4 hours ago
The standard says this aboutfscanf
: If this object does not have an appropriate type, or if the result of the conversion cannot be represented in the object, the behavior is undefined.
– Broman
3 hours ago
@AndrewHenle this is the scanf function,, not the printf function.char *
andchar (*)[600]
are not compatible types (that term is defined by C17 6.2.7)
– M.M
3 hours ago
|
show 10 more comments
up vote
4
down vote
Yes, the code is undefined behaviour. The argument corresponding to %s
must have the type char *
. This is described in C17 7.21.6.2/12 under the s
specifier:
[...] the corresponding argument shall be a pointer to the initial element of a character array large enough to accept the sequence and a terminating null character, which will be added automatically.
which says fairly clearly that the pointer should have pointer-to-character type, and not point to the whole array.
Undefined behaviour means that anything can happen. It might behave as if you omitted the &
, or it might format your hard drive.
Given that it is extremely easy to avoid undefined behaviour in this case, I don't really see any reason to engage in arguments about whether it is OK to rely on the behaviour of undefined behaviour in this situation.
7.19.6.1? Do you mean 7.21.6.1 The fprintf function? And why doesn't 6.2.7 Compatible type and composite type "save" the posted code from UB? The address of the array is the address of the first element, which has to be compatible with the type of a pointer to the same type as an element of the array? I tend to agree with your answer that's it UB because 7.21.6.1 says it's UB, but I can see how an argument via compatible types and array decay can be made. And that's way, way, waaay down the language lawyer rabbit hole...
– Andrew Henle
4 hours ago
@AndrewHenle Nice catch. I was to quick there.
– Broman
4 hours ago
@AndrewHenle -- "The address of the array is the address of the first element, which has to be compatible with the type of a pointer to the same type as an element of the array?": I am not sure what you are trying to say here, but it sounds like you are saying that a pointer to an array and a pointer to the first element of that array are compatible types. I can't see any way that the Standard supports this, given that two types have compatible type if their types are the same (plus a few rules that don't appear to apply here).
– David Bowling
4 hours ago
The standard says this aboutfscanf
: If this object does not have an appropriate type, or if the result of the conversion cannot be represented in the object, the behavior is undefined.
– Broman
3 hours ago
@AndrewHenle this is the scanf function,, not the printf function.char *
andchar (*)[600]
are not compatible types (that term is defined by C17 6.2.7)
– M.M
3 hours ago
|
show 10 more comments
up vote
4
down vote
up vote
4
down vote
Yes, the code is undefined behaviour. The argument corresponding to %s
must have the type char *
. This is described in C17 7.21.6.2/12 under the s
specifier:
[...] the corresponding argument shall be a pointer to the initial element of a character array large enough to accept the sequence and a terminating null character, which will be added automatically.
which says fairly clearly that the pointer should have pointer-to-character type, and not point to the whole array.
Undefined behaviour means that anything can happen. It might behave as if you omitted the &
, or it might format your hard drive.
Given that it is extremely easy to avoid undefined behaviour in this case, I don't really see any reason to engage in arguments about whether it is OK to rely on the behaviour of undefined behaviour in this situation.
Yes, the code is undefined behaviour. The argument corresponding to %s
must have the type char *
. This is described in C17 7.21.6.2/12 under the s
specifier:
[...] the corresponding argument shall be a pointer to the initial element of a character array large enough to accept the sequence and a terminating null character, which will be added automatically.
which says fairly clearly that the pointer should have pointer-to-character type, and not point to the whole array.
Undefined behaviour means that anything can happen. It might behave as if you omitted the &
, or it might format your hard drive.
Given that it is extremely easy to avoid undefined behaviour in this case, I don't really see any reason to engage in arguments about whether it is OK to rely on the behaviour of undefined behaviour in this situation.
edited 24 mins ago
answered 5 hours ago
M.M
103k11108229
103k11108229
7.19.6.1? Do you mean 7.21.6.1 The fprintf function? And why doesn't 6.2.7 Compatible type and composite type "save" the posted code from UB? The address of the array is the address of the first element, which has to be compatible with the type of a pointer to the same type as an element of the array? I tend to agree with your answer that's it UB because 7.21.6.1 says it's UB, but I can see how an argument via compatible types and array decay can be made. And that's way, way, waaay down the language lawyer rabbit hole...
– Andrew Henle
4 hours ago
@AndrewHenle Nice catch. I was to quick there.
– Broman
4 hours ago
@AndrewHenle -- "The address of the array is the address of the first element, which has to be compatible with the type of a pointer to the same type as an element of the array?": I am not sure what you are trying to say here, but it sounds like you are saying that a pointer to an array and a pointer to the first element of that array are compatible types. I can't see any way that the Standard supports this, given that two types have compatible type if their types are the same (plus a few rules that don't appear to apply here).
– David Bowling
4 hours ago
The standard says this aboutfscanf
: If this object does not have an appropriate type, or if the result of the conversion cannot be represented in the object, the behavior is undefined.
– Broman
3 hours ago
@AndrewHenle this is the scanf function,, not the printf function.char *
andchar (*)[600]
are not compatible types (that term is defined by C17 6.2.7)
– M.M
3 hours ago
|
show 10 more comments
7.19.6.1? Do you mean 7.21.6.1 The fprintf function? And why doesn't 6.2.7 Compatible type and composite type "save" the posted code from UB? The address of the array is the address of the first element, which has to be compatible with the type of a pointer to the same type as an element of the array? I tend to agree with your answer that's it UB because 7.21.6.1 says it's UB, but I can see how an argument via compatible types and array decay can be made. And that's way, way, waaay down the language lawyer rabbit hole...
– Andrew Henle
4 hours ago
@AndrewHenle Nice catch. I was to quick there.
– Broman
4 hours ago
@AndrewHenle -- "The address of the array is the address of the first element, which has to be compatible with the type of a pointer to the same type as an element of the array?": I am not sure what you are trying to say here, but it sounds like you are saying that a pointer to an array and a pointer to the first element of that array are compatible types. I can't see any way that the Standard supports this, given that two types have compatible type if their types are the same (plus a few rules that don't appear to apply here).
– David Bowling
4 hours ago
The standard says this aboutfscanf
: If this object does not have an appropriate type, or if the result of the conversion cannot be represented in the object, the behavior is undefined.
– Broman
3 hours ago
@AndrewHenle this is the scanf function,, not the printf function.char *
andchar (*)[600]
are not compatible types (that term is defined by C17 6.2.7)
– M.M
3 hours ago
7.19.6.1? Do you mean 7.21.6.1 The fprintf function? And why doesn't 6.2.7 Compatible type and composite type "save" the posted code from UB? The address of the array is the address of the first element, which has to be compatible with the type of a pointer to the same type as an element of the array? I tend to agree with your answer that's it UB because 7.21.6.1 says it's UB, but I can see how an argument via compatible types and array decay can be made. And that's way, way, waaay down the language lawyer rabbit hole...
– Andrew Henle
4 hours ago
7.19.6.1? Do you mean 7.21.6.1 The fprintf function? And why doesn't 6.2.7 Compatible type and composite type "save" the posted code from UB? The address of the array is the address of the first element, which has to be compatible with the type of a pointer to the same type as an element of the array? I tend to agree with your answer that's it UB because 7.21.6.1 says it's UB, but I can see how an argument via compatible types and array decay can be made. And that's way, way, waaay down the language lawyer rabbit hole...
– Andrew Henle
4 hours ago
@AndrewHenle Nice catch. I was to quick there.
– Broman
4 hours ago
@AndrewHenle Nice catch. I was to quick there.
– Broman
4 hours ago
@AndrewHenle -- "The address of the array is the address of the first element, which has to be compatible with the type of a pointer to the same type as an element of the array?": I am not sure what you are trying to say here, but it sounds like you are saying that a pointer to an array and a pointer to the first element of that array are compatible types. I can't see any way that the Standard supports this, given that two types have compatible type if their types are the same (plus a few rules that don't appear to apply here).
– David Bowling
4 hours ago
@AndrewHenle -- "The address of the array is the address of the first element, which has to be compatible with the type of a pointer to the same type as an element of the array?": I am not sure what you are trying to say here, but it sounds like you are saying that a pointer to an array and a pointer to the first element of that array are compatible types. I can't see any way that the Standard supports this, given that two types have compatible type if their types are the same (plus a few rules that don't appear to apply here).
– David Bowling
4 hours ago
The standard says this about
fscanf
: If this object does not have an appropriate type, or if the result of the conversion cannot be represented in the object, the behavior is undefined.– Broman
3 hours ago
The standard says this about
fscanf
: If this object does not have an appropriate type, or if the result of the conversion cannot be represented in the object, the behavior is undefined.– Broman
3 hours ago
@AndrewHenle this is the scanf function,, not the printf function.
char *
and char (*)[600]
are not compatible types (that term is defined by C17 6.2.7)– M.M
3 hours ago
@AndrewHenle this is the scanf function,, not the printf function.
char *
and char (*)[600]
are not compatible types (that term is defined by C17 6.2.7)– M.M
3 hours ago
|
show 10 more comments
up vote
2
down vote
Using &str
instead of str
didn't cause any problems in this case because the addresses of those two are the same. See this past question for an explanation. But as you note, the type of &str
is different, and the compiler throws up a warning, and the actual behavior will depend on architecture and implementation.
New contributor
It might not cause problems in common architectures - but strictly speaking, it's not allowed. And one thing that might break it: there's no guarantee that different pointer types all use the same representation for the same address. So here the types in questionchar*
andchar (*)[600]
might have different sizes or value representations.
– aschepler
4 hours ago
@aschepler Fair enough. I suppose any behavior not defined in the spec is going to be implementation- or architecture-specific. Better to write it the correct way to begin with.
– Paul
4 hours ago
@Paul -- "any behavior not defined in the spec": It isn't that the behavior is left undefined, so much as that the Standard says explicitly that the behavior is undefined. Passing the wrong types to functions is not a gray area.
– David Bowling
4 hours ago
Yes, though the difference between a behavior not being defined and being explicitly undefined doesn't seem that different. I'll update my answer to say that it "didn't" cause any problems in this case rather than it "wouldn't" cause any problems.
– Paul
4 hours ago
1
@Paul -- compiler writers may take advantage of the fact that some constructs lead to explicit undefined behavior (and hence should never show up in valid C programs) to make some optimizations. I would say that this is a significant difference compared with undefined-by-omission behaviors, and certainly with the many implementation-defined behaviors given in the Standard.
– David Bowling
4 hours ago
|
show 1 more comment
up vote
2
down vote
Using &str
instead of str
didn't cause any problems in this case because the addresses of those two are the same. See this past question for an explanation. But as you note, the type of &str
is different, and the compiler throws up a warning, and the actual behavior will depend on architecture and implementation.
New contributor
It might not cause problems in common architectures - but strictly speaking, it's not allowed. And one thing that might break it: there's no guarantee that different pointer types all use the same representation for the same address. So here the types in questionchar*
andchar (*)[600]
might have different sizes or value representations.
– aschepler
4 hours ago
@aschepler Fair enough. I suppose any behavior not defined in the spec is going to be implementation- or architecture-specific. Better to write it the correct way to begin with.
– Paul
4 hours ago
@Paul -- "any behavior not defined in the spec": It isn't that the behavior is left undefined, so much as that the Standard says explicitly that the behavior is undefined. Passing the wrong types to functions is not a gray area.
– David Bowling
4 hours ago
Yes, though the difference between a behavior not being defined and being explicitly undefined doesn't seem that different. I'll update my answer to say that it "didn't" cause any problems in this case rather than it "wouldn't" cause any problems.
– Paul
4 hours ago
1
@Paul -- compiler writers may take advantage of the fact that some constructs lead to explicit undefined behavior (and hence should never show up in valid C programs) to make some optimizations. I would say that this is a significant difference compared with undefined-by-omission behaviors, and certainly with the many implementation-defined behaviors given in the Standard.
– David Bowling
4 hours ago
|
show 1 more comment
up vote
2
down vote
up vote
2
down vote
Using &str
instead of str
didn't cause any problems in this case because the addresses of those two are the same. See this past question for an explanation. But as you note, the type of &str
is different, and the compiler throws up a warning, and the actual behavior will depend on architecture and implementation.
New contributor
Using &str
instead of str
didn't cause any problems in this case because the addresses of those two are the same. See this past question for an explanation. But as you note, the type of &str
is different, and the compiler throws up a warning, and the actual behavior will depend on architecture and implementation.
New contributor
edited 4 hours ago
New contributor
answered 6 hours ago
Paul
3206
3206
New contributor
New contributor
It might not cause problems in common architectures - but strictly speaking, it's not allowed. And one thing that might break it: there's no guarantee that different pointer types all use the same representation for the same address. So here the types in questionchar*
andchar (*)[600]
might have different sizes or value representations.
– aschepler
4 hours ago
@aschepler Fair enough. I suppose any behavior not defined in the spec is going to be implementation- or architecture-specific. Better to write it the correct way to begin with.
– Paul
4 hours ago
@Paul -- "any behavior not defined in the spec": It isn't that the behavior is left undefined, so much as that the Standard says explicitly that the behavior is undefined. Passing the wrong types to functions is not a gray area.
– David Bowling
4 hours ago
Yes, though the difference between a behavior not being defined and being explicitly undefined doesn't seem that different. I'll update my answer to say that it "didn't" cause any problems in this case rather than it "wouldn't" cause any problems.
– Paul
4 hours ago
1
@Paul -- compiler writers may take advantage of the fact that some constructs lead to explicit undefined behavior (and hence should never show up in valid C programs) to make some optimizations. I would say that this is a significant difference compared with undefined-by-omission behaviors, and certainly with the many implementation-defined behaviors given in the Standard.
– David Bowling
4 hours ago
|
show 1 more comment
It might not cause problems in common architectures - but strictly speaking, it's not allowed. And one thing that might break it: there's no guarantee that different pointer types all use the same representation for the same address. So here the types in questionchar*
andchar (*)[600]
might have different sizes or value representations.
– aschepler
4 hours ago
@aschepler Fair enough. I suppose any behavior not defined in the spec is going to be implementation- or architecture-specific. Better to write it the correct way to begin with.
– Paul
4 hours ago
@Paul -- "any behavior not defined in the spec": It isn't that the behavior is left undefined, so much as that the Standard says explicitly that the behavior is undefined. Passing the wrong types to functions is not a gray area.
– David Bowling
4 hours ago
Yes, though the difference between a behavior not being defined and being explicitly undefined doesn't seem that different. I'll update my answer to say that it "didn't" cause any problems in this case rather than it "wouldn't" cause any problems.
– Paul
4 hours ago
1
@Paul -- compiler writers may take advantage of the fact that some constructs lead to explicit undefined behavior (and hence should never show up in valid C programs) to make some optimizations. I would say that this is a significant difference compared with undefined-by-omission behaviors, and certainly with the many implementation-defined behaviors given in the Standard.
– David Bowling
4 hours ago
It might not cause problems in common architectures - but strictly speaking, it's not allowed. And one thing that might break it: there's no guarantee that different pointer types all use the same representation for the same address. So here the types in question
char*
and char (*)[600]
might have different sizes or value representations.– aschepler
4 hours ago
It might not cause problems in common architectures - but strictly speaking, it's not allowed. And one thing that might break it: there's no guarantee that different pointer types all use the same representation for the same address. So here the types in question
char*
and char (*)[600]
might have different sizes or value representations.– aschepler
4 hours ago
@aschepler Fair enough. I suppose any behavior not defined in the spec is going to be implementation- or architecture-specific. Better to write it the correct way to begin with.
– Paul
4 hours ago
@aschepler Fair enough. I suppose any behavior not defined in the spec is going to be implementation- or architecture-specific. Better to write it the correct way to begin with.
– Paul
4 hours ago
@Paul -- "any behavior not defined in the spec": It isn't that the behavior is left undefined, so much as that the Standard says explicitly that the behavior is undefined. Passing the wrong types to functions is not a gray area.
– David Bowling
4 hours ago
@Paul -- "any behavior not defined in the spec": It isn't that the behavior is left undefined, so much as that the Standard says explicitly that the behavior is undefined. Passing the wrong types to functions is not a gray area.
– David Bowling
4 hours ago
Yes, though the difference between a behavior not being defined and being explicitly undefined doesn't seem that different. I'll update my answer to say that it "didn't" cause any problems in this case rather than it "wouldn't" cause any problems.
– Paul
4 hours ago
Yes, though the difference between a behavior not being defined and being explicitly undefined doesn't seem that different. I'll update my answer to say that it "didn't" cause any problems in this case rather than it "wouldn't" cause any problems.
– Paul
4 hours ago
1
1
@Paul -- compiler writers may take advantage of the fact that some constructs lead to explicit undefined behavior (and hence should never show up in valid C programs) to make some optimizations. I would say that this is a significant difference compared with undefined-by-omission behaviors, and certainly with the many implementation-defined behaviors given in the Standard.
– David Bowling
4 hours ago
@Paul -- compiler writers may take advantage of the fact that some constructs lead to explicit undefined behavior (and hence should never show up in valid C programs) to make some optimizations. I would say that this is a significant difference compared with undefined-by-omission behaviors, and certainly with the many implementation-defined behaviors given in the Standard.
– David Bowling
4 hours ago
|
show 1 more comment
up vote
-1
down vote
in C the name of an array is also its address (points to the beginning of the array).
New contributor
1
In the question,str
ischar[600]
and&str
ischar (*)[600]
(the latter is mentioned in the warning message).
– anatolyg
5 hours ago
This isn't true. Arrays are objects in C (in the C sense), and array identifiers refer to array objects. Arrays do decay to pointers to their first elements in most expressions (but not in all expressions). In particular, withchar arr = "abc"; size_t arr_sz = sizeof arr;
the identifierarr
not only refers to an array, it does not decay to a pointer in thesizeof
expression.
– David Bowling
4 hours ago
add a comment |
up vote
-1
down vote
in C the name of an array is also its address (points to the beginning of the array).
New contributor
1
In the question,str
ischar[600]
and&str
ischar (*)[600]
(the latter is mentioned in the warning message).
– anatolyg
5 hours ago
This isn't true. Arrays are objects in C (in the C sense), and array identifiers refer to array objects. Arrays do decay to pointers to their first elements in most expressions (but not in all expressions). In particular, withchar arr = "abc"; size_t arr_sz = sizeof arr;
the identifierarr
not only refers to an array, it does not decay to a pointer in thesizeof
expression.
– David Bowling
4 hours ago
add a comment |
up vote
-1
down vote
up vote
-1
down vote
in C the name of an array is also its address (points to the beginning of the array).
New contributor
in C the name of an array is also its address (points to the beginning of the array).
New contributor
edited 6 hours ago
New contributor
answered 6 hours ago
XsOuLp
194
194
New contributor
New contributor
1
In the question,str
ischar[600]
and&str
ischar (*)[600]
(the latter is mentioned in the warning message).
– anatolyg
5 hours ago
This isn't true. Arrays are objects in C (in the C sense), and array identifiers refer to array objects. Arrays do decay to pointers to their first elements in most expressions (but not in all expressions). In particular, withchar arr = "abc"; size_t arr_sz = sizeof arr;
the identifierarr
not only refers to an array, it does not decay to a pointer in thesizeof
expression.
– David Bowling
4 hours ago
add a comment |
1
In the question,str
ischar[600]
and&str
ischar (*)[600]
(the latter is mentioned in the warning message).
– anatolyg
5 hours ago
This isn't true. Arrays are objects in C (in the C sense), and array identifiers refer to array objects. Arrays do decay to pointers to their first elements in most expressions (but not in all expressions). In particular, withchar arr = "abc"; size_t arr_sz = sizeof arr;
the identifierarr
not only refers to an array, it does not decay to a pointer in thesizeof
expression.
– David Bowling
4 hours ago
1
1
In the question,
str
is char[600]
and &str
is char (*)[600]
(the latter is mentioned in the warning message).– anatolyg
5 hours ago
In the question,
str
is char[600]
and &str
is char (*)[600]
(the latter is mentioned in the warning message).– anatolyg
5 hours ago
This isn't true. Arrays are objects in C (in the C sense), and array identifiers refer to array objects. Arrays do decay to pointers to their first elements in most expressions (but not in all expressions). In particular, with
char arr = "abc"; size_t arr_sz = sizeof arr;
the identifier arr
not only refers to an array, it does not decay to a pointer in the sizeof
expression.– David Bowling
4 hours ago
This isn't true. Arrays are objects in C (in the C sense), and array identifiers refer to array objects. Arrays do decay to pointers to their first elements in most expressions (but not in all expressions). In particular, with
char arr = "abc"; size_t arr_sz = sizeof arr;
the identifier arr
not only refers to an array, it does not decay to a pointer in the sizeof
expression.– David Bowling
4 hours ago
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472293%2fcan-it-cause-problems-to-pass-the-address-to-an-array-instead-of-the-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown