Saturday, May 22, 2010

C programming question?

Given the following declarations:


char a[ ] = "abc", *p = "def";





What is the effect of the following statement?





p=a





a. It copies the string "abc" into string p





b. It changes p to point to string "abc"





c. It copies the first character of a ton the first character of p





d. It generates an error

C programming question?
haha cheater.





ya D) is looking good.
Reply:Genrates Error
Reply:b.


p is a pointer to char.


The name of the array a is also a pointer to char.
Reply:b. p will point to a which contains "abc".
Reply:The answer is b.





I just wrote a little code to do it, although I should have been smart and just looked at K%26amp;R.
Reply:p will then point to the first position of a, it won't point to the whole string but you can use pointer arithmatic to get the rest of the string. So - E. none of the above.
Reply:Usually it would be b. Because you replaced the adress the pointer points at. But i am not quite sure you can declare a pointer on a string constant (*p="def") - it would probably generate an error.

flower pots

No comments:

Post a Comment