Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Void | Pointers
/
C Basics

bookVoid

メニューを表示するにはスワイプしてください

We've previously encountered void when we looked at functions that don't return anything or just display text on the screen.

Keep in mind that we can't declare a variable of the void type. However, we can create a void* pointer that points to a memory location.

Note

If you've worked through the pointer exercises in the section, you'll know that all pointers occupy 8 bytes. A void* pointer is no different.

A void* pointer isn't tied to any specific data type. This flexibility allows us to store the address of any data type in it.

However, there's a catch: we can't dereference a void* pointer.

Note

This limitation stems from the same reason we can't declare variables of the void type.

Perform an explicit type conversion!

Main.c

Main.c

copy
123456789101112131415161718192021
#include <stdio.h> int main() { char c = 'F'; int i = 100; double d = 3.15; void* pV; pV = &c; printf("%c \n", *((char*)pV)); pV = &i; printf("%d \n", *((int*)pV)); pV = &d; printf("%.2f \n", *((double*)pV)); return 0; }

Congratulations on completing the basic course in C programming! If you're eager to dive deeper into this fascinating and logical programming language, I highly recommend delving into topics like macros, sorting algorithms, and data structures. Additionally, familiarizing yourself with one of the Linux distributions is invaluable for any seasoned C programmer.

Best of luck!

question mark

How do you retrieve the value of a void pointer?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 6.  6

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 6.  6
some-alt