what is the difference between these initializations?
Char a[]=”string”;
Char *p=”literal”;
Does *p++ increment p, or what it points to?

Answer Posted / koti

Actually char a[ ]="string" in this scenario string constant is stored in read-only memory section and also stack section.in this case you can modify the string constant.that modifications are happened in stack section .so here
*a ++ men's
1 ) a is pointing to base address of string constant .
2 ) *a men's inside content that is 's'.
3 ) *a ++ men's incrementing the asci value of 's'. After that you can print this array like
Printf("%s",a);
O/P : ttring.
Coming to the *p ="literal" this scenario
1 ) *p is stored in stack section why because it is auto variable.
2 ) "literal" this string constant is stored in read-only memory section.
3 ) P is pointing to string constant Base addres
Here *p++ men's you are training to change read only memory section contact. so it is an error why because
You can't modified the read-only memory content.

Main difference is using arrays string constant is stored in both stack and read-only memory section.
Using pointers string constant is stored in read-only memory section only .
Thank you.

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

where are auto variables stored? What are the characteristics of an auto variable?

848


How we can insert comments in a c program?

855


What is cohesion in c?

748


If one class contains another class as a member, in what order are the two class constructors called a) Constructor for the member class is called first b) Constructor for the member class is called second c) Only one of the constructors is called d) all of the above

838


What is the auto keyword good for?

832


What are local variables c?

760


How are Structure passing and returning implemented by the complier?

926


Can you write the algorithm for Queue?

1760


What are runtime error?

852


Do you have any idea about the use of "auto" keyword?

870


What is a stream water?

901


How many types of operator or there in c?

843


Write a program to reverse a given number in c?

784


Are there constructors in c?

780


Define the scope of static variables.

834