Suraj Menon
Suraj Menon

Reputation: 1594

Ambiguous behavior while incrementing pointer

#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 ?

Upvotes: 1

Views: 208

Answers (4)

vmp
vmp

Reputation: 2420

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.

Upvotes: 0

Anirudha
Anirudha

Reputation: 32797

(*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

Upvotes: 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

Upvotes: 0

Cam
Cam

Reputation: 15234

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).

Upvotes: 5

Related Questions