- 1
Why these code give different answers
https://code.sololearn.com/cenGQoL14t06/?ref=app https://code.sololearn.com/ceXpvgdt42v2/?ref=app
9 Antworten
+ 6
#include <iostream>
using namespace std;
int main() {
char a[]="hello";
char*p=a;
cout<<*(p+4);
return 0;
}
This code will print 5th character becoz array start from 0 so 4th character will be 0
*(p+1) means p is a intital address means *(1000+1) which represent first index here in cout u written cout<<*(p+4) means *(1000+4) which represent 5th index so Output is (o).
+ 6
Your this line
char *p="solo learn";
printf ("%s",(p+5));
p+ 5 means suppose base address of p is 100 then 100+5 means 5th char which is space after that it will print complete string . But if u will write printf("%s",*(p+5) ) this will give you erros .
char arr[]="health";
printf ("\n%s",arr+5); simple u defined health String in arr which is char type arr+5 which will represent particular index means if i suppose address of arr is 100 then 100+5 =105 which represent 6th index becoz array start from zero so 6th char will be print.
+ 6
char *p="hello";
is INVALID in standard C++. It is valid C, but not C++.
C++ allowed this for C compatibility, but deprecated, in C++98 and C++03. The special rule allowing it was removed in C++11, which cleaned up many things.
One valid C++ declaration is
⋮ const char *p="hello";
The type of the string literal "hello" is array `const char[6]`. That works as initializer for a `const char*` pointer because there is an automatic type DECAY that converts the array expression to pointer-to-first-item. There is a similar type decay for functions.
However, this needlessly discards the information about the length of the string that pointer `p` points to the first `char` in.
+ 6
A better declaration is therefore
⋮ const auto& s = "hello";
where the type of `s` is deduced as reference to an array `const char[6]`, i.e. with known length. It has 6 items, not 5 characters h, e, l, l, o, because there is an automatic nullvalue at the end. You can't avoid that terminating nullvalue in a string literal.
Where you want natural and easy access to the string length you can instead do
⋮ constexpr string_view s = "hello";
where `string_view` is `std::string_view` from the `<string_view>` header. Then you can write `s.length()` to get the length, instead of with the array `sizeof(s) - 1`.
---
⋮ printf("%s",*p);
This tells `printf` to assume that the argument is a pointer to nullterminated string. But the actual argument is a single `char`. So it has UNDEFINED BEHAVIOR.
The ease with which one can inadvertently create UB with `printf` is a main reason why C++ learners are adviced to use C++ iostreams, not C `printf`.
However, a modern compiler told to produce most all useful warnings (fo
+ 6
Saumya Mishra welcome
+ 4
What are trying to achive from theses codes ?🧐
They are giving different outputs because they have been programmed to do so.
If you want both of them to achieve same answer then just alter the values accordingly.
+ 1
Arsenic okay
+ 1
ཞıɬıƙą ɱıʂɧཞą thanks for info😁
0