Reputation: 121
If I wanted to iterate over a string, in this case 'test', I could use pointer arithmetic like so to get to the next character:
char *c = "test";
*c++;
putchar(*c);
This would print the second letter 'e'.
But, if I wanted to go to the third character, why can't I say:
char *c = "test";
c = *c + 2; //*c = *c + 2; also doesn't work
putchar(*c); //Exception thrown: write access violation.
?
To my knowledge, the only way to get to the third character using pointer arithmetic is to cast it like so:
char *c = "test";
c = (char *) c + 2;
putchar(*c);
Can someone please explain to me why that is?
Upvotes: 0
Views: 370
Reputation: 652
Lets understand the code below:
char * c = "test";
c = *c + 2; //*c = *c + 2; also doesn't work
putchar(*c);//Exception thrown: write access violation.
First statement says that you have an character array whose starting address is stored in variable c. Char are stored as ASCII value in memory.
Second statement says take value at address in location pointed to by c (ASCII of 'e' is 0x65) add 2 to it and save it as new address. Variable c now contains 0x67.
Third statement says print value as address stored in c (which is now 0x67). As we do not have access to this memory location an exception is thrown.
You would want to increment memory location pointed to by c and print the content. For that you would need to do:
char * c = "test";
c = c + 2; //Moves the pointer by 2 bytes
putchar(*c); //Print the character at that memory location
Upvotes: 0
Reputation: 48010
Your first code fragment was
char * c = "test";
*c++;
putchar(*c);
and as you noted, it prints e
. But it's important to note that in the second line, the *
is completely superfluous. You've incremented c
, and fetched the pointed-to-character -- and then thrown the pointed-to character away.
It would have worked exactly the same -- and been more clear, with no confusing, extra operators -- if you had instead said
char * c = "test";
c++;
putchar(*c);
Next you wondered why you couldn't increment the pointer by 2. But of course you can increment the pointer by two -- you could either say
char * c = "test";
c++;
c++;
putchar(*c);
or
char * c = "test";
c += 2;
putchar(*c);
or
char * c = "test";
c = c + 2;
putchar(*c);
But you wrote
c = *c + 2;
That didn't work, and no surprise -- again, you have an unnecessary *
, and here, it's not so harmless. Here, you unnecessarily fetch the character that c
points to, but instead of throwing it away, you add 2 to it, and then you try to assign the character to your original pointer variable. Your compiler probably complained about this -- assigning a character to a pointer is meaningless. It's as if you had said
c = 'x';
It looks like you may be unsure about what *
means in C. It is not just a marker that says "this variable is a pointer". It is an operator that actively fetches the value pointed to by the pointer.
Whenever you're working with pointers, you have to keep clear in your mind the very important distinction between the value of the pointer versus the value pointed to by the pointer.
Going back to your original example, when you said
char * c = "test";
the value of the pointer c
is "pointer to the first character of the string", and the value pointed to by the pointer is "the character t
".
If you say
c++
you increment the value of the pointer. Now it points at the second character of the string, and the value pointed to is the character e
.
If c
is a pointer, then any time I mention c
by itself in an expression, I am referring to the value of the pointer. But any time I stick the operator *
in front of it, I am referring to the value pointed to by the pointer.
Upvotes: 2
Reputation: 4788
In your first example, c++;
simply adds one to c
, no dereferencing, so why are you trying to dereference when adding 2? Simply add 2 to the pointer value:
char * c = "test";
c = c + 2; //*c = *c + 2; also doesn't work
putchar(*c);//Exception thrown: write access violation.
Upvotes: 3
Reputation: 19874
c
here is a pointer which holds the address of a memory location.
*c
is called de-referencing the pointer which will give the value stored in the memory location pointed by c
.
So
c = *c + 2
will be
c = (value stored at c) + 2;
which will not give the memory location like
c = c + 2;
Upvotes: 5