I believe the answer to Q1 is incorrect. The interviewer and interviewee both state that the code will run into a segmentation fault when trying to evaluate ptr->a. Because the if statement is an AND and the first expression is evaluated to false, the second (ptr->a) would not be evaluated, and thus not throw an segmentation fault. The output of the run would be the print("reached outside if ") and it will return 0.
Eric, you are absolutely correct about the case of AND expression. Actually, there are 2 cases being described in the question: Case 1: Where the question is // if(ptr->a) In this case, the answer will be segmentation fault Case 2: Where the question is // if(ptr && ptr->) ------- Discussed at time 3:57 In this case, ptr->a wouldn't be evaluated as the condition fails at ptr itself and there will be no segmentation fault. The confusion is probably because the image of the question stays the same throughout the discussion of Q1.
This video tells us exactly why we need to stop using C. It's scary to imagine all the security flaws the interviewee has been writing before and after this interview :)
it was good but he said to clear the basics, so i am sure the interviewee had studies from books you tube and had same understanding as me, but how can he improve, if he revise from same sources he will again get confused so what are these sources from where he can get his basics right
The candidate needs preparation. He is often confused. I guess he won't be able to make it if he gives the interview immediately after this. The questions are very good. It is like a masterclass in pointers.
Behnam Heydarshahi, below are the code snippets as requested by you: //---Before Correction:---// void fun(int *p) { int q = 10; p = &q; } int main() { int r = 20; int *p = &r; fun(p); printf("%d", *p); return 0; } //---After Correction:---// void fun(int **pptr) { static int q = 10; *pptr = &q; } int main() { int r = 20; int *p = &r; fun(&p); printf("%d", *p); return 0; }
Q4) I think it's illegal to access memory pointed by p once function fun() is called. We could get segmentation fault or garbage. Because: i.e. ideally once the function is done, the variable goes out of scope. However here somehow memory is still there and not changed by some other function/program so we get the correct answer. Let me know if my understanding is not correct. Please refer following: #include #include void fun(int **p){ int q=10; printf("q=%p, p=%p ", &q, *p); *p = &q; printf("q=%p, p=%p ", &q, *p); } int main(){ int r = 20; int *p = &r; printf("&r=%p, p=%p ", &r, p); fun(&p); printf("*p = %d ",*p); *p = 50; printf("Value &p=%p *p=%d r=%d ", p, *p, r); return 0; } I got the following output: &r=0x7fffd5428e5c, p=0x7fffd5428e5c q=0x7fffd5428e34, p=0x7fffd5428e5c q=0x7fffd5428e34, p=0x7fffd5428e34 *p = 10 Value &p=0x7fffd5428e34 *p=50 r=20
These all are C programming snippets?so if we want to build our career in embedded then we should know about C programming , isn't it,i have done my btech in ece and having good knowledge of core subjects like digital electronics and analog electronics,edc 🤔?
// Explanation: Inside fun(), q is a copy of the pointer p. // So if we change q to point something else then p remains uneffected. // If we want to change a local pointer of one function inside another function, // then we must pass pointer to the pointer. By passing the pointer to the pointer, // we can change pointer to point to something else. See the following program as an example. void fun(int **pptr) { static int q = 10; *pptr = &q; } int main() { int r = 20; int *p = &r; fun(&p); printf("%d", *p); return 0; } // In the above example, the function fun() expects a double pointer (pointer to a pointer to an integer). // fun() modifies the value at address pptr. The value at address pptr is pointer p as we pass adderess of p to fun(). // In fun(), value at pptr is changed to address of q. Therefore, pointer p of main() is changed to point to a new variable q.
@@adityagupta9719 The explanation is as mentioned above in the previous comment. When you use *p instead of **p, it just passes the value and not the address to the pointer. Hence, the output will be 20 in that case.
I believe the answer to Q1 is incorrect. The interviewer and interviewee both state that the code will run into a segmentation fault when trying to evaluate ptr->a.
Because the if statement is an AND and the first expression is evaluated to false, the second (ptr->a) would not be evaluated, and thus not throw an segmentation fault. The output of the run would be the print("reached outside if
") and it will return 0.
Eric, you are absolutely correct about the case of AND expression. Actually, there are 2 cases being described in the question:
Case 1: Where the question is // if(ptr->a)
In this case, the answer will be segmentation fault
Case 2: Where the question is // if(ptr && ptr->) ------- Discussed at time 3:57
In this case, ptr->a wouldn't be evaluated as the condition fails at ptr itself and there will be no segmentation fault.
The confusion is probably because the image of the question stays the same throughout the discussion of Q1.
Perspicacious mock interview. Very useful 🔝🔝
though generally the bad quality of sound and unsynchronised screen( especially Q3), it is insightful and helpful, thanks.
This is really useful
Really insightful mock test!
Good way of given a feedback
This video tells us exactly why we need to stop using C. It's scary to imagine all the security flaws the interviewee has been writing before and after this interview :)
Is this guy a fresher or an experienced candidate?
it was good but he said to clear the basics, so i am sure the interviewee had studies from books you tube and had same understanding as me, but how can he improve, if he revise from same sources he will again get confused so what are these sources from where he can get his basics right
The candidate needs preparation. He is often confused. I guess he won't be able to make it if he gives the interview immediately after this.
The questions are very good. It is like a masterclass in pointers.
Could you please comment the code of question 4 (changing pointer value through a function cell) before and after correction?
Behnam Heydarshahi, below are the code snippets as requested by you:
//---Before Correction:---//
void fun(int *p)
{
int q = 10;
p = &q;
}
int main()
{
int r = 20;
int *p = &r;
fun(p);
printf("%d", *p);
return 0;
}
//---After Correction:---//
void fun(int **pptr)
{
static int q = 10;
*pptr = &q;
}
int main()
{
int r = 20;
int *p = &r;
fun(&p);
printf("%d", *p);
return 0;
}
@@preplaced printf("%d",*p); // output will address of r.
Right?
Q4)
I think it's illegal to access memory pointed by p once function fun() is called. We could get segmentation fault or garbage. Because:
i.e. ideally once the function is done, the variable goes out of scope. However here somehow memory is still there and not changed by some other function/program so we get the correct answer.
Let me know if my understanding is not correct. Please refer following:
#include
#include
void fun(int **p){
int q=10;
printf("q=%p, p=%p
", &q, *p);
*p = &q;
printf("q=%p, p=%p
", &q, *p);
}
int main(){
int r = 20;
int *p = &r;
printf("&r=%p, p=%p
", &r, p);
fun(&p);
printf("*p = %d
",*p);
*p = 50;
printf("Value &p=%p *p=%d r=%d
", p, *p, r);
return 0;
}
I got the following output:
&r=0x7fffd5428e5c, p=0x7fffd5428e5c
q=0x7fffd5428e34, p=0x7fffd5428e5c
q=0x7fffd5428e34, p=0x7fffd5428e34
*p = 10
Value &p=0x7fffd5428e34 *p=50 r=20
These all are C programming snippets?so if we want to build our career in embedded then we should know about C programming , isn't it,i have done my btech in ece and having good knowledge of core subjects like digital electronics and analog electronics,edc 🤔?
Yes
Embedded C and knowledge on uC is must
I'm I the only one or for problem 5 "10" is the output. I tried executing the code too, it gives 10. Can someone explain?
// Explanation: Inside fun(), q is a copy of the pointer p.
// So if we change q to point something else then p remains uneffected.
// If we want to change a local pointer of one function inside another function,
// then we must pass pointer to the pointer. By passing the pointer to the pointer,
// we can change pointer to point to something else. See the following program as an example.
void fun(int **pptr)
{
static int q = 10;
*pptr = &q;
}
int main()
{
int r = 20;
int *p = &r;
fun(&p);
printf("%d", *p);
return 0;
}
// In the above example, the function fun() expects a double pointer (pointer to a pointer to an integer).
// fun() modifies the value at address pptr. The value at address pptr is pointer p as we pass adderess of p to fun().
// In fun(), value at pptr is changed to address of q. Therefore, pointer p of main() is changed to point to a new variable q.
@@preplaced so i guess 10 is the correct
@@preplaced can someone explain what happens in Q4 if we write *p in parameter instead of **p
@@adityagupta9719 , Yes, the output will be 10 when we use the double pointer. And 20 when double pointer is not used.
@@adityagupta9719 The explanation is as mentioned above in the previous comment. When you use *p instead of **p, it just passes the value and not the address to the pointer. Hence, the output will be 20 in that case.