#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main()

{
char *p1="Name";

 const char *p2="Name";
char const *p3="Name";
char *const p4="Name";
const char *const p5="Name";

++p1;
++*p1;
++p2;
++*p2;
++p3;
++*p3;
++p4;
++*p4;
++p5;
++*p5;


 }

While Compiling above code certain Compilation error came.

The errors are:
const.c: In function âmainâ:
const.c:17: error: increment of read-only location
const.c:19: error: increment of read-only location
const.c:20: error: increment of read-only variable âp4â
const.c:22: error: increment of read-only variable âp5â
const.c:23: error: increment of read-only location
Compilation error is expected but why in 19?
Is const char *pointer and char cons *pointer Same?
Please explain in detail Why the error is in Line 19?

You can't increment constant values. That's why they are const

    char *p1="Name";                // Non-const pointer pointing to non-const char.
    const char *p2="Name";          // Non-const pointer pointing to constant char.
    char const *p3="Name";          // Alternative syntax for p2.
    char *const p4="Name";          // Const pointer pointing to non-const char.
    const char *const p5="Name";    // Const pointer pointing to const char.

    ++p1;   // Fine:  p1 isn't a const pointer.
    ++*p1;  // Fine:  p1 points to chars who aren't const.
    ++p2;   // Fine:  p2 isn't a const pointer.
    ++*p2;  // Error: p2 points to a const char and you're trying to increment it here.
    ++p3;   // Fine:  p3 isn't a const pointer. (p2 and p3 are basically declared the same way but in a different syntax)
    ++*p3;  // Error: p3 points to a const char and you're trying to increment it here.
    ++p4;   // Error: p4 points to non-const data, but you're trying to modify the pointer itself here, which is const.
    ++*p4;  // Fine:  p4 itself is const but the data is points to is non-const. Dereferencing and then incrementing is fine as a result.
    ++p5;   // Error: p5 is a const pointer as well as it pointing to const data.
    ++*p5;  // Error: p5 is a const pointer as well as it pointing to const data.

Added some comments with each statement that should answer your question.

Uhm, I mean, here's a hint! It has to do with asteriks and the position of the const keyword! More precisely you should look up the difference between "const char" and "char const". Ignore the above it would be an answer so it's evil and totally stupid of me to post. I accept downvotes for this flaw, the more the merrier.

Ignore the above it would be an answer so it's evil and totally stupid of me to post.

No offense, but this statement makes you seem childish and bitter. Surely you can tell the difference between explaining how const works and posting a complete working program for an obvious homework question, right?

No offense, but this statement makes you seem childish and bitter.

What I find interesting that rather than posting the statement

Ignore the above it would be an answer so it's evil and totally stupid of me to post

and making himself look... well... you know... he just didn't delete the explanation himself and not post it.

and making himself look... well... you know... he just didn't delete the explanation himself and not post it.

Sarcasm. Do.. you.. understand.. it?

Appearently you fail to grasp anything so I make it clear: In case it wasn't obvious in the other thread I don't agree with not posting solutions so this was formulated in that specific way as a slight ridicule towards it.

Feel free to private message me in case you're ever having troubles reading.

Appearently you fail to grasp anything so I make it clear: In case it wasn't obvious in the other thread I don't agree with not posting solutions so this was formulated in that specific way as a slight ridicule towards it.

Ahhh, I see now. It's Mr. "I Know More About These Forums Than Everyone Else".

Problem with your logic is:
1) you are expecting us to remember one post you made elsewhere out of hundreds we read daily
2) sarcasm requires knowledge of the one making sarcastic remarks. All I understand is from what you posted above.
3) on a forum, a smily generally hints at sarcasm. No smily, you must be simply daft.
4) whether you agree or not, you are not to post working answers. Period.
5) better reread the member rules. They are important here, whether you agree with them or not.

1) you are expecting us to remember one post you made elsewhere out of hundreds we read daily

You've made the reference to a previous post in another thread yourself. In that case, yes. I do expect you to remember it.

2) sarcasm requires knowledge of the one making sarcastic remarks. All I understand is from what you posted above.

Knowledge about recent events also aid and you were present in those.

3) on a forum, a smily generally hints at sarcasm. No smily, you must be simply daft.

As someone who dislikes smileys I can tell you there's more than that. I don't see writers using smileys in their books (thanks god) and they seem to get their intent across just fine.

The combination of the first two lines ending with '!' and the statement following it which you even quotes should be way obvious. If you're incapable of detecting it then I consider it your issue. But sure, I can omit sarcasm from my posts from now on. Posting a sneer trying to ridicule a post while you are in fact the one not understanding the post at all has the opposite desired effect for you, though. I guess I'll view it as karma.

4) whether you agree or not, you are not to post working answers. Period.

The community rules don't seem to mention this. I'll determine on a per-post basis how I could help a user best, thanks.

5) better reread the member rules. They are important here, whether you agree with them or not.

I'd like to recommend you the same. In particular:

Do not post insults or personal attacks aimed at another member
Do not use offensive or obscene language

You, and a couple of other moderators seem to not even follow your own rules yet you expect others to? If you (or anyone else) attempts to provoke me in a thread you can expect a response.

commented: If you have a problem with the way things are supposed to be done, keep it out of the responses and make your points in the Feedback Forum. Stop cluttering up other people's threads with your rants. -3

Thanks for your replies.
I added the entire program so that the answers can be to the point and the discussion thread doesn't unnecessarily get too long...
Anyways thanks u all guys..

char

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.