हैलो मैं सी में सामान्य डेटा प्रकार के रूप में शून्य * का उपयोग करने की कोशिश कर रहा हूं। मैं जो चाहता हूं वह एक तंत्र है जिसका उपयोग मैं कुछ भी स्टोर कर सकता हूं और कुछ भी प्राप्त कर सकता हूं। मैंने कुछ कोड लिखा, लेकिन यह पिछले मामले में असफल रहा है। क्या कोई भी कोड को देख सकता है, अगर आपके पास कोई अन्य विचार है तो कृपया मुझे बताएं।सी में सामान्य डेटा प्रकार [void *]
मुझे पता है कि मैं स्टोर करने की कोशिश कर रहा हूं, इसलिए उस समय मुझे डेटा प्रकार पता है, लेकिन प्रतिशोध के दौरान मुझे केवल शुरुआती पता और आकार पता है।
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
static void store(void *destination, void *source, size_t size) {
memcpy ((char*)destination, source, size);
}
static void retrieve (void *destination, void *source, size_t size) {
memcpy (destination, (char*)source, size);
}
void *storage_char_ptr = (void*) malloc (sizeof(char*));
void *storage_int_ptr = (void*) malloc (sizeof(int*));
void *storage_int = (void*) malloc (sizeof(int));
int main() {
int int_in = 65;
void *int_out_ptr;
int *ptr = (int*) malloc (sizeof(int));
memcpy (ptr, &int_in, sizeof(int));
store (storage_int_ptr, &ptr, sizeof(int*));
retrieve (&int_out_ptr, storage_int_ptr, sizeof(int*));
assert (int_in == *(int*)int_out_ptr);
char *char_in = "HelloWorld!!";
void *char_out;
store (storage_char_ptr, &char_in, sizeof(char*));
retrieve (&char_out, storage_char_ptr, sizeof(char*));
assert (strcmp (char_in, (char*)char_out) == 0);
char_in = _strdup("HelloWorld!!");
store (storage_char_ptr, &char_in, sizeof(char*));
retrieve (&char_out, storage_char_ptr, sizeof(char*));
assert (strcmp (char_in, (char*)char_out) == 0);
/* This is where it is failing */
int_in = 55;
void* int_out;
store (storage_int, &int_in, sizeof(int));
retrieve (&int_out, storage_int, sizeof(int));
assert (55 == *(int*)int_out);
}
यह कैसे विफल रहा है? – MByD