Computer Science, asked by abdulrahmanaltameme, 7 months ago


What is the output of the code snippet?

void fn(int *p)
{
static int val = 100;
p = &val;
}
main()
{
int i=10;
printf("i=%d\n", i);
fn(&i);
printf("i=%d\n", i);
}

A)
i=100 i=10

B)
i=10 i=100

C)
None of the above

D)
i=100 i=100

E)
i=10 i=10

Answers

Answered by AdityaChhimpa
0

Answer:

The output(with “\n” for each print) would be

So the line

char *s[] = { “knowledge”, “is”, “power” }; creates a 2d array of characters.

and in the line p = s; you are making a double pointer point to that memory(to the start of the memory).

I think there are 4 properties of C(in simple terms) that is worth mentioning here.

Incrementing p(double ptr) would take you to the next word.

Incrementing *p(single ptr) would take you to the next letter.

An increment operator(++) gets higher precedence over a dereference operator(*)

A pre increment operator(++p) increments before returning the value and a post increment operator(p++) increments after returning the value.

I am not going into the details of the above 3.

Now keep all the above 3 in mind. Here we go..

p is pointing to “knowledge”

++*p ==> increment *p ==> move pointer by 1 and hence “nowledge”(Point 2 above)

*p++ ==> will not do (*p)++, but will do p++(because of point 3. So p now points to “is”) and then apply “*” operator on it. But remember this is a post increment operator you have used. So the line will not return the new value though it is now actually pointing to “is”.(Point 4 above). Since the old value is returned, it would still print “nowledge”

++*p ==> like in point 2 above, it will move pointer by 1 and hence “s” (point 2 above)

Hope I was clear enough

if helpful mark me brainlist

Similar questions