Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C halps

Name: Anonymous 2008-03-19 17:18

How do I check what type of variable a variable is?

Something like;
if (typecheck(foo,int))
  {
  /* lol */
  }

Name: Anonymous 2008-03-19 17:20

I'm actually writing a variant of car, but for C, if it speeds up replies.

Name: Anonymous 2008-03-19 17:20

It is whatever you want it to be.

Name: Anonymous 2008-03-19 17:21

>>2
A variant of ... what?  A car?  An AUTO-mobile?

Name: Anonymous 2008-03-19 17:30

Do not try to introspect the types; that's impossible. Instead only try to recognize the truth: there are no types.

Name: Anonymous 2008-03-19 17:31

>>4
car
Quick, someone make the joke!

Name: Anonymous 2008-03-19 17:31

:t

Name: Anonymous 2008-03-19 17:37

>>1
I assume you only want to check pointers to memory (anything else seems stupid to me), in this case you can store the type of the memory in an extra byte:

int* pi = malloc(sizeof(int) + 1);
char* pc = pi;
*pc = 'i'; /* i for int */


Then just increment the pointer by one byte when accessing it (I'd use a temporary variable for this).

int* tpi = (((char*)pi)+1);

Name: Anonymous 2008-03-19 18:13

>>8
Hello and welcome to Bus Errorlern2alignment

Name: Anonymous 2008-03-19 18:27

>>1
Just cast it to an int and check if it's a valid value, e.g. for a pointer:

if (INT_MIN <= *(*int)foo <= INT_MAX) {
        /* *foo points to an int */
}

Your coding style is horrible, by the way. You really should see an orthodontist about those misplaced braces.

Name: Anonymous 2008-03-19 18:29

>>10
I normally code K&R style, but /prog/ doesn't see the difference since the faggets moved in from /b/, so I didn't bother.

Could you give me some more background on what exactly your code does? I don't like to use something unless I fully understand it.

Name: Anonymous 2008-03-19 18:31

Welcome to GNU

Name: lol !8mQB/2odm6 2008-03-19 18:32

>>4,7,12
You are not programmers, stop trying to fit in.

Name: Anonymous 2008-03-19 18:35

>>1
Hungarian notation

Name: Anonymous 2008-03-19 18:35

>>13
Try lurking more tripfag

Name: Anonymous 2008-03-19 18:37

>>15
Not recognising my trip means you need more lurking, sir.

Name: Anonymous 2008-03-19 18:37

>>10
'INT_MIN' undeclared (first use in this function)

Name: Anonymous 2008-03-19 18:39

>>16
Shut up, tripfag.

Name: GJSussman !xpQSO2ECEY 2008-03-19 18:39

>>13
lol!8mQB/2odm6! Good to see you again.

Name: Anonymous 2008-03-19 18:43

>>9
FUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUCK!!!

Well, how about storing the byte at the value returned by malloc - 1?  Is that writable or not?  (I assume yes.)

That's what I would do then.

Name: Anonymous 2008-03-19 20:39

Found a better way to do it, thread over.

Name: Anonymous 2008-03-19 20:42

>>21
Forced indention?

Name: Anonymous 2008-03-19 20:46

>>22
back to reddit, please

Name: Anonymous 2008-03-19 20:54

>>23
Huh?

Name: Anonymous 2008-03-19 20:54

>>20
enjoy your segfault

Name: Anonymous 2008-03-19 21:40

WHY DONT YOU JUST USE A STRUCT WITH AN ENUM WHERE THE ENUM IS THE TYPE OF THE UNION AND THE UNION CONTAINS *ALL* TYPES, JUST A THOUGHT

Name: Anonymous 2008-03-19 23:37

operator typeof

Name: EXPERT PROGRAMMER 2008-03-19 23:49

Basically you need to create a list of all the available types and their sizes, and then use sizeof on your variable to determine which type it is.



char* types[4] = {
    "char",  // 1 byte
    "short",  // 2 bytes
    "long",  // 4 bytes
    "long long,"  // 8 bytes
};

int sizes[4] = { 1, 2, 4, 8 };


bool typecheck (variable, type)
{
    int i;

    for (; i <= 3; i++)
    {
        if (strcmp(types[i], type)
           return  sizes[i] = sizeof(variable) ? TRUE : FALSE;
    }

    return FALSE;
}

Name: Anonymous 2008-03-20 0:10

>>28
VARIABLE SCOPE, FAGGOT

lrn2c

Name: Anonymous 2008-03-20 0:33

wait... if you declare a variable as an integer, then why would it change? cant you just go back and read your code?

Name: Anonymous 2008-03-20 0:55

only faggots use bool real men use integers

Name: Anonymous 2008-03-20 0:57

>>28

"long",  // 4 bytes

FAIL

Name: Anonymous 2008-03-20 1:44

>>30
exactly, checking what type a variable is can be done in perl, php or any other scripted language but in c it's quite useless as the types cannot change, you can have void pointers and cast them but technically that's not checking what type they are

the closest you'll get is ctype.h

Name: Anonymous 2008-03-20 2:40

>>32
No, a "long" / DWORD is 4 bytes.

CORRECT

Name: Anonymous 2008-03-20 2:51

Simply, you cannot do it.
sizeof won't work, because sizeof (short) might be 1, or sizeof (float) == sizeof (long).
It's simply impossible just with the size.
Also, sizeof (size_t) might be 4 in your system but 128 in others.

one thing you can do if you know you work with integers and pointers only is to use intmax_t, but I'm not sure if that is what you were looking for.

Name: Anonymous 2008-03-20 2:51

A signed long can hold all the values between  LONG_MIN and LONG_MAX inclusive. LONG_MIN is required to be -2147483647 or less, LONG_MAX must be at least 2147483647. Again, many 2's complement implementations will define LONG_MIN to be -2147483648 but this is not required.

An unsigned long can hold all the values between 0 and ULONG_MAX inclusive. ULONG_MAX must be at least 4294967295. The long types must contain at least 32 bits to hold the required range of values.

Name: Anonymous 2008-03-20 3:25

>>35
facepalm.mm

Name: Anonymous 2008-03-20 3:28

buttscoop

Name: Anonymous 2008-03-20 3:30

Stop trying to do dynamic typing in C, the language wasn't meant to work that way and I'm sure you can find a better solution to your problem that doesn't need it.

Name: Anonymous 2008-03-20 4:14

>>36
they sure do buddy, what prevents sizeof (long) to be 1 though?
Nothing, as long as CHAR_BIT >= 32.
So.. gtfo. You're talking with an expert here

>>37
Post again when you comprehend half of my post.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List