Monday 25 March 2013

What is wild pointer in c?


What is wild pointer in c?  

Explanation:
A pointer in c which has not been initialized is known as wild pointer.

Example:
What will be output of following c program?
int main(){
int *ptr;
printf("%u\n",ptr);
printf("%d",*ptr);
return 0;
}

Output: Any address
Garbage value
Here ptr is wild pointer because it has not been initialized. There is difference between the NULL pointer and wild pointer. Null pointer points the base address of segment while wild pointer doesn’t point any specific memory location.

Blog Author: Vijay Kumar


What is dangling pointer in c?


What is dangling pointer in c? 

Explanation:
Dangling pointer:

If any pointer is pointing the memory address of any variable but after some variable has deleted from that memory location while pointer is still pointing such memory location. Such pointer is known as dangling pointer and this problem is known as dangling pointer problem.

Initially:

Later:


Blog Author: Vijay Kumar


Is a default case necessary in a switch statement?


Is a default case necessary in a switch statement?
No, but it is not a bad idea to put default statements in switch statements for error- or logic-checking purposes



Blog Author: Vijay Kumar


When is a switch statement better than multiple if statements?


When is a switch statement better than multiple if statements?
A switch statement is generally best to use when you have more than two conditional expressions based on a single variable of numeric type


Blog Author: Vijay Kumar


Remember Points


A pointer in c which has not been initialized is known as wild pointer.

If any pointer is pointing the memory address of any variable but after some variable has deleted from that memory location while pointer is still pointing such memory location. Such pointer is known as dangling pointer and this problem is known as dangling pointer problem.

Blog Author: Vijay Kumar


Can we specify variable field width in a scanf() format string? If possible how?


Can we specify variable field width in a scanf() format string? If possible how?
Ans: All field widths are variable with scanf(). You can specify a maximum field width for a given
field by placing an integer value between the ‘%’ and the field type specifier. (e.g. %64s). Such a specifier will still accept a narrower field width.
The one exception is %#c (where # is an integer). This reads EXACTLY # characters, and it is the
only way to specify a fixed field width with scanf().

Blog Author: Vijay Kumar