2nd Sep 2020, 5:40 AM
Saumya Mishra
Saumya Mishra - avatar
9 Réponses
+ 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).
2nd Sep 2020, 5:53 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 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.
2nd Sep 2020, 5:56 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 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.
2nd Sep 2020, 6:00 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 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
2nd Sep 2020, 6:03 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 6
2nd Sep 2020, 6:05 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 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.
2nd Sep 2020, 5:48 AM
Arsenic
Arsenic - avatar
+ 1
Arsenic okay
2nd Sep 2020, 5:50 AM
Saumya Mishra
Saumya Mishra - avatar
+ 1
ཞıɬıƙą ɱıʂɧཞą thanks for info😁
2nd Sep 2020, 6:03 AM
Saumya Mishra
Saumya Mishra - avatar
2nd Sep 2020, 6:10 AM
Saumya Mishra
Saumya Mishra - avatar