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
What are preprocessor directives in c?
What is the difference between #include
Difference between Shallow copy and Deep copy?
Why main is not a keyword in c?
why do some people write if(0 == x) instead of if(x == 0)?
Explain the use of bit fieild.
Explain how can you tell whether two strings are the same?
What are pointers in C? Give an example where to illustrate their significance.
What are valid operations on pointers?
Tell us two differences between new () and malloc ()?
How do we declare variables in c?
Can a pointer be null?
Write a program to identify if a given binary tree is balanced or not.
What is LINKED LIST? How can you access the last element in a linked list?
How do we print only part of a string in c?