Showing posts with label C Programming - Pointers. Show all posts
Showing posts with label C Programming - Pointers. Show all posts

Tuesday, March 22, 2016

Pointer Arithmetic

You are well aware about arithmetic of other variable like int, float, double etc. and you must think about whether those can be applied to a pointer variable or not. No, you can’t apply arithmetic operation like addition, subtraction, multiplications etc. between two pointer variables. The operations given below give you a Compiler Error.
int i=10;
int j=20;
int *pi = &i, *pj = &j;
int *res = *pi + *pj /* Compilation Error */
res = *pi - *pj /* Compilation Error */
res = *pi * *pj /* Compilation Error */
Only thing you can do is to increment and decrement operation to a pointer variable. This is mostly used by C programmers when dealing with arrays or strings.
char s[] = "a4academics.com";
char* ptr = s;

printf(“%s\n”,ptr);
printf(“%c\n”,*ptr);
printf(“%c\n”,*(ptr + 3));
ptr+=5;
printf(“%c\n”,ptr);
ptr--;
printf(“%c\n”,ptr);

The above gives following output:
a4academics.com
a
c
d
a

Monday, March 21, 2016

Void Pointers

You must have noticed that a pointer always holds an address of the same data type, like an integer, pointer always holds the address of integer type of variable, float of a float, double of a double etc.
But there is a special type of pointer called void pointer also known as generic pointer. This type of pointer can hold the memory address of any kind of data type or any object even functions too! This is declared like below:
void *void_ptr = &i;
But the only thing you need to remember is that you can’t dereference a pointer directly. You have to typecast it before you use it. Like if you want to print the value of the above declared void pointer, the memory address it contains point to is:
printf(“%d”,*(int *)void_ptr);
This is very useful when you want a pointer to point to data of different data types at different times.

Sunday, March 20, 2016

Dereferencing Pointers

As you seen earlier, "*" operator is used to declare the pointer type of the variable. "*" is also used to dereference the pointer also.
int j = *p;
It sets the value of "j" variable to the value contained at the memory address, the pointer p points to i.e. 10. You can also set a value to the memory address that a pointer holds like below.
*p = 20;
By this, the memory address that "p" pointer holds i.e. 0x2000 contains value 20 which is same as the value of i and *j, interesting right! That’s the beauty of the pointer, as you change the value that contains an address of a variable; it also changes the variable itself.

Thursday, August 7, 2014

C Programming - Pointers

1. 
What is (void*)0?
A.Representation of NULL pointer
B.Representation of void pointer
C.Error
D.None of above

2. 
Can you combine the following two statements into one?
char *p;
p = (char*) malloc(100);
A.char p = *malloc(100);
B.char *p = (char) malloc(100);
C.char *p = (char*)malloc(100);
D.char *p = (char *)(malloc*)(100);

3. 
In which header file is the NULL macro defined?
A.stdio.hB.stddef.h
C.stdio.h and stddef.hD.math.h


4. 
How many bytes are occupied by near, far and huge pointers (DOS)?
A.near=2 far=4 huge=4B.near=4 far=8 huge=8
C.near=2 far=4 huge=8D.near=4 far=4 huge=8