Question:
C programming question?
2006-09-01 12:37:46 UTC
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
Seven answers:
steven p
2006-09-01 12:41:57 UTC
haha cheater.



ya D) is looking good.
Bax
2006-09-01 19:44:20 UTC
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.
rockiee80
2006-09-01 19:39:50 UTC
Genrates Error
John J
2006-09-01 20:34:58 UTC
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.
TJ
2006-09-01 19:47:14 UTC
The answer is b.



I just wrote a little code to do it, although I should have been smart and just looked at K&R.
rt11guru
2006-09-01 19:43:35 UTC
b.

p is a pointer to char.

The name of the array a is also a pointer to char.
bee
2006-09-01 19:43:27 UTC
b. p will point to a which contains "abc".


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...