2

Possible Duplicate:
Change string literal in C through pointer?

Here is a code sample

void main()  
{

    char *i="prady";  printf("%c ",++*i); 
}

Can anyone tell me why this code is giving a segmentation fault in gcc when I guess it should give 'q'. When I am using only *i++ it giving me the result but incase of pre-increment only it's giving me a segmentation fault.

Also tell me why void main is not a proper way to write main() function.

Community
  • 1
  • 1
pradipta
  • 1,718
  • 2
  • 13
  • 24
  • 2
    String literals are read-only. Also, any operating system will expect a return value from your program. – chris Sep 28 '12 at 18:28
  • @chris :Thanks for your answer ,here by doing ++*i we are doing only increment of the value ,we are not incrementing the value what i points.I am confused can any one kindly tell me the what happening inside the ++*i.what I know it is a ponter to constant so we cant't do *(i+2)='o'; but here what happening how this value is related to i.also if I am doing *i + 1 it is not giving any error ,so can any one tell me actually what happening inside the ++*i . – pradipta Sep 28 '12 at 18:35
  • As in the answer, you first dereference the pointer to get the read-only character, and then you increment that read-only character. – chris Sep 28 '12 at 18:36
  • @chris, 1) `void` return is perfectly ok. 2) The OS ALWAYS gets a value from the program. In this case, that value will be zero. – ikegami Sep 28 '12 at 19:32
  • 1
    @ikegami, From the C11 standard (**5.1.2.2.1 Program Startup**): *The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int...*. Also, from the Undefined Behaviour section, *A program in a hosted environment does not define a function named main using one of the specified forms*. If it's running in a hosted environment, `void main()` is UB. – chris Sep 28 '12 at 19:35

3 Answers3

7

++*i means ++(*i). You're trying to modify the first character of a string literal, which is not permitted. As far as the C standard is concerned behavior is undefined, but this implementation has helpfully segfaulted to alert you to the problem.

*i++ means *(i++). You're modifying your pointer i, which is fine.

void main() is not a proper way to write a main function because the standard says that main returns int. The return value is used to indicate the success or failure of the program. Implementations can support other forms of main, but there are two that are required: int main(void) and int main(int argc, char *argv[]).

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
  • Thanks for your answer , can you give me the link for the standards. – pradipta Sep 28 '12 at 18:38
  • For the last published version of the C99 standard (incorporating corrections), consult http://stackoverflow.com/questions/4883212/html-version-of-c-standard-draft-n1256-pdf. There's also an older version of the standard (called C89 or C90) that gcc supports, and a newer version (C11) that it doesn't. – Steve Jessop Sep 28 '12 at 18:50
0
++*i

means that you pre-increment your pointer. for example

int *i

*i = 1;

imagine that i is a pointer to the address 0x8FF43FF0 if you compile ++*i before dereferencing i points to 0x8FF43F4

++(*i)

means, first dereference i, than increment

zeyorama
  • 444
  • 3
  • 12
  • 1
    -: Thanks for answer , but ++*i means not pre-increment as * and ++ has same precedence and their associativity is from right to left so first * then ++ occurs. – pradipta Sep 28 '12 at 18:46
0

Steve Jessop already told you why ++*i returns an error so I'm not going to tell you that again.

*i++ will return p, the first letter of the word, because the "++" operator returns the value first, and only after had it returned the value does it increment it. So if you want your program to print 'q' you would have to say printf("%c ",*i+1). Also, if you want your program to print the second character, try: printf("%c ", *(i+1)). The next letter in the alphabet after the third letter of your word will be printf("%c ", *(i+2)+1) and so on.

Why you should use int main instead of void? The value returned by the main function informs the operating system about how the program ended. o (as in return 0) tells the operating system that the program had executed correctly. you usually use non 0 codes when the program must end because of an error.

Ionut Hulub
  • 6,180
  • 5
  • 26
  • 55