1 1 vote Please tell me about all the variations of these above types of questions that can be asked? Programming in C programming-in-c + – Na462 1.8k views answer comment Share Follow Print See all 11 Comments 11 11 Comments reply pankaj_vir commented May 4, 2018 reply Follow flag https://www.geeksforgeeks.org/const-qualifier-in-c/ 1 1 replyShare srestha commented May 4, 2018 reply Follow flag it will give error preincrement or post increment operator will not work on constant pointer 2 2 replyShare ankitgupta.1729 commented May 4, 2018 reply Follow flag @srestha , could u please tell me ,what should be output for this code :- #include<stdio.h> int main() { const int *p =(int*) 10; printf("%d",(*p)); } 0 0 replyShare gauravkc commented May 4, 2018 reply Follow flag p is not initialized. I guess the garbage value that p has will be treated as address and the program will try to store 10 there which will result in an error. Run-time error? 0 0 replyShare ankitgupta.1729 commented May 4, 2018 reply Follow flag it is showing segmentation fault(core dumped) on gcc 0 0 replyShare srestha commented May 4, 2018 i edited by srestha May 4, 2018 reply Follow flag @ankit here u r trying to store the value of general pointer into a constant int pointer right? it is trying to get some address, but getting another integer pointer value So, will give runtime error https://stackoverflow.com/questions/21476869/constant-pointer-vs-pointer-to-constant 0 0 replyShare srestha commented May 4, 2018 reply Follow flag @ankit @gaurav can u explain tis code plz https://ideone.com/7rt9cz 0 0 replyShare ankitgupta.1729 commented May 4, 2018 reply Follow flag @srestha , I don't know why it is showing segmentation fault on my machine and online gdb compiller.. is it accessing memory area of any other process ? I think , in your code , a=(int *) 10 , a= (int)10 , a=10 all are same and will give the same output..we use int* in case of pointer variable but here we r not using any pointer variable . so it will not affect anything 0 0 replyShare Arunav Khare commented May 4, 2018 reply Follow flag * is a de-referencing operator. *anyPtr would mean "go to the address pointed by anyPtr and then print value in it" In your case, a=(int *) 10 Address 10 in memory has nothing or is in your program's bounds. So, when you are getting segfault. 2 2 replyShare srestha commented May 4, 2018 reply Follow flag yea, rt I think it is finding some hex value(as address are always in hex) and getting an integer value that is why giving seg fault 0 0 replyShare gauravkc commented May 4, 2018 reply Follow flag I didn't get it @Arunav Khare .. The code srestha sent has no error. Are you getting confused between the two questions? Cause you are associating a line from her code to result of the code in the question @srestha .. The code u sent https://ideone.com/7rt9cz is not yet clear to me. I experimented a lot with that first line.. the code works fine even if u replace int* with float*, double* etc.. int a=(double*) 10; works but float a=(double*) 10; doesn't .. I guess it's ambiguous. @ankitgupta.1729 in the given question p will be assigned value 10.. When *p is printed, it causes segmentation fault as it's outside address space of it 1 1 replyShare Please log in or register to add a comment.
1 1 vote Refer these two link, these two posts are enough to understand about the pointers(some basic are required). Feel free to ask if you don't get something. https://stackoverflow.com/questions/5727/what-are-the-barriers-to-understanding-pointers-and-what-can-be-done-to-overcome?rq=1 https://stackoverflow.com/questions/18481740/pointer-expressions-ptr-ptr-and-ptr (examples like you asked in your question). bhuv answered May 5, 2018 bhuv comment Share Follow 0 reply Please log in or register to add a comment.