Is it possible to create pointers in C without a specific datatype -
so started programming project, , found out make easier if make pointer without declaring char*
, int*
, on , specify size somewhere else. searched around couldn't find useful.
right idea make char*
, unite them bit shifting , or operations if isn't string. if low level c, language lets me this, prefer not use assembly because isn't cross platform enough.
edit: forget mention should allocate memory @ runtime, , not waste memory want allocate memory depending on size. example want allocate 1 byte char sized values , 2 bytes int sized values , on. size predefined in file, not known until file read should read in runtime rather compiling time.
there generic pointer type in c reason: void *
.
you can convert data pointer type implicitly , void *
:
char foo[] = "bar"; void *ptr = foo; // ... char *str = ptr;
but aware still have know correct type. accessing object through pointer of wrong type undefined behavior.
there 2 things can't void *
:
you can't pointer arithmetics on (this includes indexing).
void
incomplete type, doesn't have known size. compilers allow arithmetics onvoid *
assuming size ofchar
, extension , not portable.you can't dereference
void *
.void
"nothing", wouldn't meaningful.
Comments
Post a Comment