The Wayback Machine - https://web.archive.org/web/20240828110659/https://www.geeksforgeeks.org/how-does-void-differ-in-c-and-cpp/
Open In App

How does ‘void*’ differ in C and C++?

Last Updated : 28 Nov, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

C allows a void* pointer to be assigned to any pointer type without a cast, whereas in C++, it does not. We have to explicitly typecast the void* pointer in C++

For example, the following is valid in C but not C++: 

void* ptr;
int *i = ptr; // Implicit conversion from void* to int*

Similarly,

int *j = malloc(sizeof(int) * 5);  // Implicit conversion from void* to int* 

In order to make the above code compile in C++ as well, we have to use explicit casting, as shown below,

void* ptr;
int *i = (int *) ptr;
int *j = (int *) malloc(sizeof(int) * 5);


Previous Article
Next Article

Similar Reads

How Does Default Virtual Behavior Differ in C++ and Java?
Let us discuss how the default virtual behavior of methods is opposite in C++ and Java. It is very important to remember that in the C++ language class member methods are non-virtual by default. They can be made virtual by using virtual keywords. For example, Base::show() is non-virtual in following program and program prints "Base::show() called".
3 min read
Difference between Dangling pointer and Void pointer
Dangling pointer: A pointer pointing to a memory location that has been deleted (or freed) is called a dangling pointer. There are three different ways where Pointer acts as a dangling pointer: By deallocating memoryFunction CallWhen the variable goes out of scope Void pointer: Void pointer is a specific pointer type – void * – a pointer that point
2 min read
Difference between "int main()" and "int main(void)" in C/C++?
Note: This was true for older versions of C but has been changed in C11 (and newer versions). In newer versions, foo() is same as foo(void). Consider the following two definitions of main(). Definition 1: C/C++ Code int main() { /* */ return 0; } C/C++ Code int main() { /* */ return 0; } Definition 2: C/C++ Code int main(void) { /* */ return 0; } C
3 min read
Dangling, Void , Null and Wild Pointers in C
In C programming pointers are used to manipulate memory addresses, to store the address of some variable or memory location. But certain situations and characteristics related to pointers become challenging in terms of memory safety and program behavior these include Dangling (when pointing to deallocated memory), Void (pointing to some data locati
6 min read
Is it fine to write void main() or main() in C/C++?
In C/C++ the default return type of the main function is int, i.e. main() will return an integer value by default. The return value of the main tells the status of the execution of the program. The main() function returns 0 after the successful execution of the program otherwise it returns a non-zero value. Using void main in C/C++ is considered no
3 min read
Return From Void Functions in C++
Void functions are known as Non-Value Returning functions. They are "void" due to the fact that they are not supposed to return values. True, but not completely. We cannot return values but there is something we can surely return from void functions. Void functions do not have a return type, but they can do return values. Some of the cases are list
2 min read
void Pointer in C
A void pointer is a pointer that has no associated data type with it. A void pointer can hold an address of any type and can be typecasted to any type. Example of Void Pointer in C C/C++ Code // C Program to demonstrate that a void pointer // can hold the address of any type-castable type #include <stdio.h> int main() { int a = 10; char b = '
3 min read
void Pointer in C++
In C++, a void pointer is a pointer that is declared using the 'void' keyword (void*). It is different from regular pointers it is used to point to data of no specified data type. It can point to any type of data so it is also called a "Generic Pointer". Syntax of Void Pointer in C++void* ptr_name; As the type of data the void pointer is pointing t
7 min read
When Does Compiler Create Default and Copy Constructors in C++?
A constructor is a special type of member function of a class that initializes objects of a class. In C++, Constructor is automatically called when an object(instance of a class) is created. There are 3 types of constructors in C++ Default ConstructorCopy constructorParameterized ConstructorIn C++, the compiler creates a default constructor if we d
3 min read
What does main() return in C and C++?
C According to coding standards, a good return program must exit the main function with 0. Although we are using void main() in C, In which we have not suppose to write any kind of return statement but that doesn't mean that C code doesn't require 0 as exit code. Let's see one example to clear our thinking about need of return 0 statement in our co
3 min read
Does C support function overloading?
First of all, what is function overloading? Function overloading is a feature of a programming language that allows one to have many functions with same name but with different signatures. This feature is present in most of the Object Oriented Languages such as C++ and Java. But C doesn't support this feature not because of OOP, but rather because
2 min read
Does C++ compiler create default constructor when we write our own?
No, the C++ compiler doesn't create a default constructor when we initialize our own, the compiler by default creates a default constructor for every class; But, if we define our own constructor, the compiler doesn't create the default constructor. This is so because the default constructor does not take any argument and if two default constructors
1 min read
Why does sizeof(x++) not increment x in C?
According to C99 Standards, the sizeof() operator only takes into account the type of the operand, which may be an expression or the name of a type (i.e int, double, float etc) and not the value obtained on evaluating the expression. Hence, the operand inside the sizeof() operator is not evaluated. It is evaluated only if the type of the operand is
2 min read
How does Volatile qualifier of C works in Computing System
Prerequisite: Computing systems, Processing unit Processing Unit:Processing units also have some small memory called registers.The interface between the processor (processing unit) and memory should work on the same speed for better performance of the system.Memory: In memory, there are two types, SRAM and DRAM. SRAM is costly but fast and DRAM is
3 min read
Minimize flips required such that string does not any pair of consecutive 0s
Given a binary string S, the task is to find the minimum count of flips required to modify a string such that it does not contain any pair of consecutive 0s. Examples: Input: S = "10001"Output: 1Explanation: Flipping S[2] modifies S to "10101". Therefore, the required output is 1. Input: S = "100001"Output: 2Explanation: Flipping S[1] modifies S to
5 min read
Why does empty Structure has size 1 byte in C++ but 0 byte in C
A structure is a user-defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type. The ‘struct’ keyword is used to create a structure. The general syntax for creating a structure is as shown below: Syntax- struct structureName{ member1; member2; member3; . . . memberN; }
3 min read
C++ Error - Does not name a type
In C++, one of the most common errors that programmers encounter is the “does not name a type” error. The error usually occurs when the programmer writes a piece of code in which the compiler is not able to recognize the type or definition for it. In this article, we are going to explore the “does not name a type” error in C++ in detail, including
6 min read
How does C allocate memory of data items in a multidimensional array?
The data items in a multidimensional array are stored in the form of rows and columns. Also, the memory allocated for the multidimensional array is contiguous. So the elements in multidimensional arrays can be stored in linear storage using two methods i.e., row-major order or column-major order. Row major order: In row-major order, we store the el
4 min read
Why does the C++ STL not provide any "tree" containers?
What is STL in C++? The Standard Template Library (STL) is a set of C++ template classes to provide common programming data structures and functions. It is a library of container classes, algorithms, functions and iterators. It is a generalized library and so, its components are parameterized. Working knowledge of template classes is a prerequisite
3 min read
Why Does C Treat Array Parameters as Pointers?
In C, array parameters are treated as pointers mainly to, To increase the efficiency of codeTo save time It is inefficient to copy the array data in terms of both memory and time; and most of the time, when we pass an array our intention is to just refer to the array we are interested in, not to create a copy of the array. The following two definit
2 min read
How does free() know the size of memory to be deallocated?
Consider the following prototype of free() function which is used to free memory allocated using malloc() or calloc() or realloc(). void free(void *ptr); Note that the free function does not accept size as a parameter. How does free() function know how much memory to free given just a pointer? Following is the most common way to store size of memor
1 min read
Write a C program that does not terminate when Ctrl+C is pressed
Write a C program that doesn't terminate when Ctrl+C is pressed. It prints a message "Cannot be terminated using Ctrl+c" and continues execution. We can use signal handling in C for this. When Ctrl+C is pressed, SIGINT signal is generated, we can catch this signal and run our defined signal handler. C standard defines following 6 signals in signal.
2 min read
C program that does not suspend when Ctrl+Z is pressed
Write a C program that doesn’t terminate when Ctrl+Z is pressed. It prints a message “Cannot be suspended using Ctrl+Z” and continues execution. We can use Unix signal for this. When Ctrl+Z is pressed, SIGTSTP signal is generated. SIGTSTP signal is sent to a process by its controlling terminal to request it to stop (terminal stop). We can catch thi
1 min read
Why variable name does not start with numbers in C ?
In C, apart from keywords, everything in the C program is treated as Identifier. Identifiers can be the names given to variables, constants, functions, and user-defined data. A variable name can consist of alphabets (upper case, lower case), numbers (0-9), and _ (underscore) character. But the name of any variable must not start with a number. Now
2 min read
What does buffer flush means in C++ ?
A buffer flush is the transfer of computer data from a temporary storage area to the computer's permanent memory. For instance, if we make any changes in a file, the changes we see on one computer screen are stored temporarily in a buffer. Usually, a temporary file comes into existence when we open any word document and is automatically destroyed w
3 min read
How does Duff's Device work?
Duff’s device is a trick to express loop unrolling directly in C or C++ without extra code to treat the leftover partial loop.The trick is to use a switch statement where all but one of the cases labels are in the middle of a while loop. Further, all cases fall through to the end of the while loop. Despite the impression, it makes Duff’s device is
6 min read
Does overloading work with Inheritance?
If we have a function in base class and another function with the same name in derived class, can the base class function be called from derived class object? This is an interesting question and as an experiment, predict the output of the following C++ program: [GFGTABS] C++ #include <iostream> using namespace std; class Base { public: int f(
5 min read
Reduce Array and Maximize sum by deleting one occurrence of A[i] and all occurrences of A[i]+1 and A[i]-1
Given an array A[] having N positive integers, the task is to perform the following operations and maximize the sum obtained while reducing the array: Select an array element (say A[i]) and delete one occurrence of that element and add A[i] to the sum.Delete all the occurrences of A[i]-1 and A[i]+1.Perform these operations until the array is empty.
8 min read
Catching Base and Derived Classes as Exceptions in C++ and Java
An Exception is an unwanted error or hurdle that a program throws while compiling. There are various methods to handle an exception which is termed exceptional handling. Let's discuss what is Exception Handling and how we catch base and derived classes as an exception in C++: If both base and derived classes are caught as exceptions, then the catch
4 min read
Difference and Similarities between PHP and C
PHP is a server-side scripting language designed specifically for web development. It can be easily embedded in HTML files and HTML codes can also be written in a PHP file. The thing that differentiates PHP from a client-side language like HTML is, PHP codes are executed on the server whereas HTML codes are directly rendered on the browser. C is a
3 min read
Article Tags :
Practice Tags :