Skip to content

Commit 2359af8

Browse files
committed
Void pointers
1 parent 512e3d0 commit 2359af8

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <stdio.h>
2+
3+
int main(void)
4+
{
5+
int myArray[]={10,43,65};
6+
7+
void *this_pointer=&myArray[1]; //this points to the second element of the array thus 43
8+
9+
this_pointer+=sizeof(int); // pointer arithmetic this offsets with the size of one int thus -> 65
10+
11+
printf("%d\n",*(int *) this_pointer); //should show 65
12+
13+
return 0;
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <stdio.h>
2+
3+
int main(void)
4+
{
5+
void *void_pointer=NULL;
6+
int *int_pointer=NULL;
7+
float *float_pointer=NULL;
8+
char *char_pointer=NULL;
9+
10+
printf("Size of the void * is %d.\n",sizeof(void_pointer));
11+
printf("Size of the int * is %d.\n",sizeof(int_pointer));
12+
printf("Size of the float * is %d.\n",sizeof(float_pointer));
13+
printf("Size of the char * is %d.\n",sizeof(char_pointer));
14+
15+
return 0;
16+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <stdio.h>
2+
3+
int main(void)
4+
{
5+
int a=10;
6+
int pi=3.14;
7+
int myChar='G';
8+
9+
10+
void *void_pointer=NULL;
11+
12+
void_pointer=&a;
13+
printf("The value stored in 'void_pointer' is %d.\n",*(int *)void_pointer);
14+
15+
void_pointer=&pi;
16+
printf("The value stored in 'void_pointer' is %.2f.\n",*(float *)void_pointer);
17+
18+
void_pointer=&myChar;
19+
printf("The value stored in 'void_pointer' is %c.\n",*(char *)void_pointer);
20+
21+
return 0;
22+
}

0 commit comments

Comments
 (0)