1
#include<stdio.h>
main()
{

        char c = 'R';
        printf("%c\n",c);
        c++;
        printf("%c\n",c);
        char *ptr ="Ramco Systems";
        printf("%c\n",(*ptr));
        (*ptr)++;
        printf("%d\n",(*ptr));

}

The output of the first, second ,3rd printf are 'R', 'S' & 'R' (as expected). However the line "(*ptr)++;" gives runtime error. Can someone explain why ?

3
  • It doesn't look like you are incrementing the pointer. You are dereferencing the pointer and incrementing the value. Commented Oct 21, 2012 at 16:57
  • I guess what you wanted to use was *ptr++ :) Commented Oct 21, 2012 at 17:03
  • 1
    I believe the aim was to increment R to next ansi character, so there is no typo in the code - just the memory addressed by the pointer is non-modifiable. Commented Oct 21, 2012 at 17:09

4 Answers 4

5

The reason is because the memory pointed to be ptr was set at compiletime and is non-modifiable.

So accessing the first character via *ptr is fine and returns R, but attempting to increment the first character yields a runtime error because you are not allowed to modify strings that you provide at compiletime.

To expand on Seg Fault's comment below, a better way to write your code would have been:

const char *ptr ="Ramco Systems"; //pointer to const char
(*ptr)++; // yields compiletime error because *ptr is a const char

Notice how in this new code, the declared pointer type is more accurate and as a result the compiler is able to give a compiletime error on the second line (much better than a runtime error).

Sign up to request clarification or add additional context in comments.

3 Comments

And if you enable enough warnings then your compiler should also have told you about the problem at the point where ptr is defined and initialised (because you are assigning a pointer to a read-only area to a pointer-to-mutable).
string literals in c are not constant..it is just not allowed to be chaged..you are mixing c and c++ here
@Fake.It.Til.U.Make.It That is correct, but the way I suggested is still a better way to write the code because it correctly indicates the intention and facilitates compiletime errors instead of runtime.
0

char *ptr = "Ramco Systm"

The above statement becomes pointer to constant and you are trying to modifying the content of it which is not valid but if you want compiler to throw an error while compile time you can use/write like below: const char * ptr = "Ramco System"

The above statement are behave in same manner. The only difference is that when you write (*ptr)++, you will get an compile time error some thing like "Modifying read only location". on the hand

1 Comment

If you want to increment the address that you can do it,something like ptr++ and this is valid also
0

(*ptr)++ should be *(ptr++)

ptr points to the first character

(*ptr) returns the actual value

(*ptr)++ is an error cause (*ptr) would return an rvalue (R in your case) which cant be assigned nor incremented..Also ptr points to a constant(not const of c++) char which cant be changed!

You should first increment ptr and then dereference it i.e *(ptr++)

Misunderstanding

string literals like "xyz" are always treated as char in C but cant be modified.They are NOT declared as const or are constant as claimed by others.It is just not allowed to change or is undefined

In C++ string literals are represented as const char which cant be chaged

3 Comments

(*ptr)++ is not equal to R++. So, that part of explanation is wrong. (*ptr) here has lvalue but because of being string literal and read only, one cannot modify it.
R++ was confusing. 'R' doesn't have a lvalue. Rest is fine.
No, what you are getting is wrong again. I said R++ explanation here is wrong. (*ptr)++ is not equal to R++. Correct third statement would be (*ptr)++ tries to modify a read only string literal which isnt right.
0

Maybe you are trying to do something like this?

  #include<stdio.h>
  main()
  {

    char c = 'R';
    printf("%c\n",c);
    c++;
    printf("%c\n",c);
    char aux[] = "Ramco Systems";
    char *p = aux;
    printf("%c\n",(*ptr));
    (*ptr)++;
    printf("%d\n",(*ptr));

}

But remember, the following line you dereference then increment:

    (*ptr)++;

So it will behave as follows: "To what letter am I pointing? Ok, change this letter to the next letter (letter with ascii code 1 unit higher)" In this case, changing 'R' by 'S'.

If what you want is to to point to 'a', you should do:

  ptr++;

by doing this you are incrementing the address that ptr points to by one, passing to point to the next character.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.