user1661781
user1661781

Reputation: 327

Incrementing the lvalue of a pointer to char?

My question is simple:

Say you have this:

char *chrptr = "junk";

Imagine at the time, *chrptr would point at j. Is there a way to increment the current character j to the next character which, in this case would be k? I don't want to go to the next character, I want to increment the current-pointed to character.

I'm new to C, and what I tried: (*chrptr)++ doesn't work.

Upvotes: 1

Views: 1674

Answers (4)

Kos
Kos

Reputation: 72241

char *chrptr = "junk";

This code isn't really good. String literals like "junk" are pointers to read-only memory. You're not supposed to ever modify them. So either say:

const char* chrptr = "junk"; // pointer to a const; no modifications

or (in your case):

char chrArray[] = "junk";

This creates a 5-element array of characters on the stack, initializes it with "junk" (plus the null terminator). Now you're free to modify it:

chrArray[0] ++;
(*chrArray) ++;

(Some leftover remarks)

I'm new to c, but things such as (*chrptr)++ don't work.

They do work, but differently. Let me sum this up:

  • chrptr is a value of type pointer-to-char.
  • *chrptr is a value of type char, because the pointer has been dereferenced.

Both of these happen to be "l-values" (= actual objects), so you can modify them:

  • ++ chrptr increments the pointer by one (= advances the pointer one object forward)
  • ++ *chrptr increments the char by one (= changes 'a' into 'b').

(Before, ++*chrptr didn't work for you because the string was in read-only section of memory. If you had pointed to it using const char* not char*, you'd get a helpful compile error instead of unexpected runtime behaviour.)


Also note:

In case of a simple statement, ++*chrptr; is equivalent to (*chrptr)++;. The operator order is important (dereference, then increment).

OTOH, *chrptr++ is increment-then-dereference because of the operator precedence. If not sure about precedence, add parentheses.

Upvotes: 2

iabdalkader
iabdalkader

Reputation: 17312

Based on your edit, it seems like you want to modify the string, you can't do that with a constant string, actually you can but that behaviour is undefined. So you need to change the definition to a non-constant array of characters:

//This initializes a 5 element array including the null byte at the end
char chrptr[] = "junk";
//now you can modify that
chrptr[0]++;
//or equivalently 
chrptr[0] +=1;
chrptr[0] = chrptr[0]+1;

Upvotes: 3

yogi
yogi

Reputation: 231

See this (*chrptr)++; works fine but this will increment the first character of the array pointed by chrptr. In your case you are using the statement like

char *chrptr = "junk";

Here chrptr is pointing to a const char array so on using (*chrptr)++; it will increment not reflect the changes in the string.

But if you use the code like this way (http://codepad.org/FJMB1ryv) :--

#include<iostream>

using namespace std;

int main()
{
char a[]="yogendra";
char *y=a;//(char*)"junk";
(*y)++;
cout<<y<<endl;
return 0;
}

you (*y)++ will work.Hoping this will help you. Good luck:)

Upvotes: 1

squiguy
squiguy

Reputation: 33370

You do not need to dereference the pointer again using the dereference operator *.

You simply need to do chrptr++ to advance the character pointer in memory.

Update

You can wrap a while loop around to search for the character.

char *chrptr = "junk";
char search = 'k';

while (*chrptr) {
  if (*chrptr == search)
    printf("%c\n", *chrptr);
  chtptr++;
}

Upvotes: 1

Related Questions