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

Answers were Sorted based on User's Feedback



what is the difference between these initializations? Char a[]=”string”; Char *p=”literal..

Answer / bee

logically, both are treated as array of characters(i.e.
string) but....

1) a is an array of characters(a string)

2) p is a pointer to an array of characters
the statement char *p = "literal" is equivalent to
char j[] = "literal"
char *p = j;
3) *p++ can be seen as *(p++)....
this is so because '++' has higher recedence over '*'
operator. so, it increments address by 1 unit and prints
the corresponding value value

Is This Answer Correct ?    5 Yes 0 No

what is the difference between these initializations? Char a[]=”string”; Char *p=”literal..

Answer / gaurav

I am totally satisfied with your above explanation except
last one.
i.e. Char *p="literal";
So, i want to mention yes this will work.
Explanation: *p++.
Here we have post increment.
Postfix increment/decrement have high precedence, but the
actual increment or decrement of the operand is delayed (to
be accomplished sometime before the statement completes
execution).
value of printf("\nstr=%c\n",*p++) will be 'l', but before
complete execution of this statement p will point to string
"iteral" as p got incremented.

Is This Answer Correct ?    3 Yes 1 No

what is the difference between these initializations? Char a[]=”string”; Char *p=”literal..

Answer / vignesh1988i

surely there is some difference.....

here 'a' is represented as array in which string gets stored
in consecutive locations......

p is a pointer variable where string is initilized... so in
p the base address of "literal " will get stored......

*p++ increments 'p' , but pertaining to some conditions.....
++ has more precedence than * , so first it will increment
the address and correspondingly it will show the value as *
precedes..... so after the increment the p points to 'i'...



thank u

Is This Answer Correct ?    5 Yes 5 No

what is the difference between these initializations? Char a[]=”string”; Char *p=”literal..

Answer / 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

More C Interview Questions

how can i get this by using for loop? * ** * **** * ******

3 Answers   Excel,


what is ANSI and ISO

7 Answers   HCL,


What is the purpose of main( ) in c language?

0 Answers  


main() { int x, arr[8]={11,22,33,44,55,66,77,88}; x=(arr+2)[3]; printf(“%d”,x); }

8 Answers   Vector,


How can you call a function, given its name as a string?

0 Answers  






Read N characters in to an array . Use functions to do all problems and pass the address of array to function. 1. Print only the alphabets . If in upper case print in lower case vice versa.

1 Answers  


write a own function to compare two strings with out using stringcomparition function?

6 Answers   LG Soft, Sasken,


Explain pointers in c programming?

0 Answers  


Write an interactive c program that will encode or decode a line of text. To encode a line of text, proceed as follows: Convert each character, including blank spaces, to its ASCII equivalent. Generate a positive random integer. Add this integer to the ASCII equivalent of each character. The same random integer will be used for the entire line of text. Suppose that N1 represents the lowest permissible value in the ASCII code, and N2 represents the highest permissible value. If the number obtained in step 2 above exceeds N2, then subtract the largest possible multiple of N2 from this number, and add the remainder to N1. Hence the encoded number will always fall between N1 and N2, and will therefore always represent some ASCII character. Display the characters that correspond to the encoded ASCII values. The procedure is reversed when decoding a line of text. Be certain, however, that the same random number is used in decoding as was used in encoding.

1 Answers   Amazon, CSJM, HCL, Microsoft, TCS, Wipro,


Write a program which returns the first non repetitive character in the string?

0 Answers   Expedia,


Fifty minutes ago if it was four times as many mints past 3 o clock. how many minutes is it to six o'clock n how....?????

3 Answers   TCS,


What is a loop?

0 Answers  


Categories