Implementing doubly linked list using single pointer
Posted on August 8, 2013
(Last modified on August 7, 2020)
| 1 minutes
| Kinshuk Chandra
Pointer cause decay, references do not.
Posted on August 8, 2010
(Last modified on August 7, 2020)
| 1 minutes
| Kinshuk Chandra
Decay means loss of type information. In C and C++, an array name ‘decays’ to pointer to the first element. The information lost is basically the number of elements in the array. This difference should be manifested by sizeof() operator in function f() and g(). References keep that information with them. such as ‘constness’ of int. Function names are also said to ‘decay’ into pointer to the function if only function name is used.
[Read More]References and Constants
Posted on August 7, 2010
(Last modified on August 7, 2020)
| 3 minutes
| Kinshuk Chandra
What is a reference?
A reference is nothing more than an alias. Anything you do with a reference you are actually doing to the object you are referencing.
float x=3.14;
float& intref=x; // intref is a reference to x
float* ptr=&intref; // ptr holds the address of x
float y=2.58;
intref=y; // x is now equal to 2.58
Another simple example:
void func(float& fr)
{
fr=99.99;
}
int main()
{
[Read More]Using pointers to break into bytes
Posted on December 7, 2009
(Last modified on August 7, 2020)
| 8 minutes
| Kinshuk Chandra
Eg.1
main()
{
int i = 257;
int *iPtr = &i;
printf("%d %d”, *((char*)iPtr), *((char*)iPtr+1) );
}
Answer:
1 1
Explanation:
The integer value 257 is stored in the memory as, 00000001 00000001, so
the individual bytes are taken by casting it to char * and get printed.
Eg. 2
main()
{
int i = 258;
int *iPtr = &i;
printf("%d %d”, *((char*)iPtr), *((char*)iPtr+1) );
}
Answer:
2 1
Explanation:
[Read More]What is a void pointer? Why can't we perform arithmetic on a void * pointer?
Posted on December 5, 2009
(Last modified on August 7, 2020)
| 1 minutes
| Kinshuk Chandra
The void data type is used when no other data type is appropriate. A void pointer is a pointer that may point to any kind of object at all. It is used when a pointer must be specified but its type is unknown.
The compiler doesn’t know the size of the pointed-to objects incase of a void * pointer. Before performing arithmetic, convert the pointer either to char * or to the pointer type you’re trying to manipulate
[Read More]Program to check if the stack grows up or down
Posted on December 5, 2009
(Last modified on August 7, 2020)
| 1 minutes
| Kinshuk Chandra
#include
#include
void stack(int *local1);
int main()
{
int local1;
stack(&local1);
exit(0);
}
void stack(int *local1)
{
int local2;
printf("\nAddress of first local : [%u]", local1);
printf("\nAddress of second local : [%u]", &local2);
if(local1 < &local2)
{
printf("\nStack is growing downwards.\n”);
}
else
{
printf("\nStack is growing upwards.\n”);
}
printf("\n\n”);
}
What is a NULL pointer? How is it different from an unitialized pointer? How is a NULL pointer defined?
Posted on December 5, 2009
(Last modified on August 7, 2020)
| 2 minutes
| Kinshuk Chandra
A null pointer simply means “I am not allocated yet!” and “I am not pointing to anything yet!”.
The C language definition states that for every available pointer type, there is a special value which is called the null pointer. It is guaranteed to compare unequal to a pointer to any object or function.
A null pointer is very different from an uninitialized pointer. A null pointer does not point to any object or function; but an uninitialized pointer can point anywhere.
[Read More]How does free works?
Posted on December 5, 2009
(Last modified on August 7, 2020)
| 1 minutes
| Kinshuk Chandra
How it works exactly is compiler dependent. It may also depend on whether or not you are building with debugging information turned on.
free is going to release memory that you’ve allocated and return it to the heap for reallocation. Generally, the memory manager as a linked list of blocks of memory that it has allocated. It is going to search the list for your block of memory, remove the link from the allocated list and insert it in the list of free blocks of memory.
[Read More]Pointer indirection with care
Posted on December 5, 2009
(Last modified on August 7, 2020)
| 1 minutes
| Kinshuk Chandra
* and . operator
struct rec { int i; float f; char c; }; int main() { struct rec \*p; p=(struct rec \*) malloc (sizeof(struct rec)); (\*p).i=10; }
To initialise pointer to 10, we have to use parentheses, because . operator has higher precedence than * operator.
++ and *
Consider **\*p++** The postfix “++” operator has higher precedence than prefix “*” operator. Thus, *p++ is same as *(p++); it increments the pointer p, and returns the value which p pointed to before p was incremented.
[Read More]What's the difference between const char *p, char * const p and const char * const p?
Posted on December 5, 2009
(Last modified on August 7, 2020)
| 1 minutes
| Kinshuk Chandra
`const char *p - This is a pointer to a constant char. One cannot change the value
pointed at by p, but can change the pointer p itself.
*p = ‘A’ is illegal.
p = “Hello” is legal.
Note that even char const *p is the same!
const * char p - This is a constant pointer to (non-const) char. One cannot change
the pointer p, but can change the value pointed at by p.
[Read More]