main() { char *p = “ayqm”; printf(“%c”,++*(p++)); }
how will it work?please explain..
Try it, it won't bite you. ;)
To learn to be a problem solver, you have to explore the world of code - roses and thorns, as well.
If I am not mistaken, It will probably crash the program.
1.)The space for the pointer p was not allocated from the system. This means it does not belong to you.
2.)Declaring main with (or without void), is undefined behavior, you should use int main().
You are incrementing the value of a char, so you are changing the encoded value. For example a may be represented as 100 for computers b as 101 and c as 102. So if you increment 3 times you may get c (it depends if they are in order. 103 for example may represent something completely different). You can express it as ++(++(++p)). If p is a, then this statement should be c. But if you add 3 to a, the compiler will probably convert a to integer so you will get number outputted to the screen.
You can also use typecasting (variable type). For example if you try to output (char)101, you should get e (or it may be another character).
First the post-increment p++ has no effect because it happens after the function.
Second ”,++*(p)); // got rid of post-increment but still invalid memory write and system crash.
If you want to print the second character and, for some reason, do not want to code the standard p[1], then move the pre-increment so it associates with with p. In other words ”,*(++p)
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.