Reputation: 167
The following code gives a seg fault at second line:
char *tester = "hello";
char a = (*tester)++; // breaks here
printf("Char is %c\n", a);
The following code works:
char *tester = "hello";
a = *tester;
a++;
printf("Char is %c\n", a);
// prints i
Why can't it be done in one operation?
Upvotes: 7
Views: 29309
Reputation: 1
char *tester = "hello";
you are trying to create a pointer directly to a string here. which will result to unexpected behaviour(because its a read only space)
instead, what I would suggest you is to use character array.
Upvotes: 0
Reputation: 673
I think none of the answers fully answered op's question: op wanted to dereference 'h'
to 'i'
, and he also wanted to know the reason that segmentFault
came from his first code snippet
Combined above answers/comments, I list a full answer here:
A. op's first confusion comes from the concept difference between char s[]
and char *s
:
char *s="hello";
is to place "hello", the string literal, in the read-only memory and make s
as a pointer to this string literal, making any writing operation illegal. While if define :
char s[]="hello";
is to put string literal in read-only memory and copy that string to another allocated memory on the stack, so that you can read/write directly on the stack memory (still, you are not touching read-only memory).
So dereference to char *test
will given you a constant char, which is the first letter of a string literal located on read-only memory, that's the reason trying to char a = (*tester)++
will fail, because this is a pointer operation on read-only memory, the behavior is undefined.
B. why the second code snippet works ?
writing a = *tester;
means you declare another variable char a
, then on stack memory, allocate some space for a char
and initialize its value to be 'h' ;
This is equivalently to have below:
char a;
a = 'h';
Of course you can increment a
.
Here is the example to the explicitly print out the address : run link
#include <stdio.h>
int main()
{
char * tester = "hello";
printf("tester first letter address=%p\n", tester); // print out address should be something like 0x400648
printf("tester second letter address=%p\n", (tester+1)); // print out address should be something like 0x400649
char a;
a = *tester;
printf("a address=%p\n", &a); // print out address should be something like 0x7ffe0bc6aab7
a++;
printf("a address=%p\n", &a); // print out address should be something like 0x7ffe0bc6aab7
return 0;
}
Upvotes: 0
Reputation: 421
I guess you are trying to do something like this..
const char *tmp = "Hello world. This is a great test";
int count = strlen(tmp);
while(count>0)
{
printf("%s\n",tmp);
tmp++;
count--;
}
Upvotes: 1
Reputation: 1080
It can be, you're just incrementing the wrong thing. (*tester)++
increments the data to which tester
is pointing. I believe what you wish to do is *(++tester)
which will increment the pointer and then dereference it.
Upvotes: 12
Reputation: 16107
char a = (*tester)++;
does not increase the pointer as you assume; it increases the dereferenced pointer instead, that is, the value a pointer points to.
You are actually trying to make changes to a string literal, which may resides in the read-only memory, causing undefined behaviour.
See n1570, 6.4.5 String literals:
7 It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.
To increase the pointer, use char a = *(tester++);
, or just char a = *tester++;
thanks to the operator precedence.
Upvotes: 2
Reputation: 136
If you want to increment the character, you can add 1 to it instead of using the increment operator:
char *tester = "hello";
char a = (*tester) + 1; // instead of (*tester)++
printf("Char is %c\n", a);
Hope this is what you are searching for.
Explanation
In another answer, zenith commented an explanation why it is not possible to use the increment operator:
String literals like "hello" are initialized in a read-only memory. It is not possible to increment its value. That is why you have to copy the value first, if you want to change it. char a = (*tester) + 1;
takes the value of the first character, adds 1 to it and saves it to a
.
Upvotes: 3
Reputation: 1601
char *tester = "hello";
Here tester is pointing to a string constant. It is stored in a read-only memory. Any write modifications causes undefined behavior.
As @Taelsin pointed, it seems you want to increment the pointer itself, and not what it is pointing to. You need *(++tester).
After OP's clarification:
In case you want to increment the H to I, then (*tester + 1) would do the job.
Upvotes: 6