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

Pages: 1-4041-

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.

Name: Anonymous 2008-03-20 4:49

>>40
face.pdb

Name: Anonymous 2008-03-20 5:02

>>40
I was talking to ``No, a "long" / DWORD is 4 bytes.'' person.

Also, if you were serious in >>35, you are a dumbass, same types having different sizes on different machines have nothing to do with this.

Name: Anonymous 2008-03-20 5:16

>>42
No it has exactly to do with this.

Name: Anonymous 2008-03-20 5:40

We have beet trolled constantly.

Name: Anonymous 2008-03-20 5:49

>>25
Thanks, fixed it so it doesn't segfault:
char*
allocate_type(type, length)
char type;
unsigned int length;
{
    int* ret = malloc(length);
    *(ret - 3) = type; /* block size is stored at -2 and -1 */
    return ret;
}

int main(int argc, char* argv[])
{
    short* s = (short*)allocate_type('s', sizeof(short));
    long* l = (long*)allocate_type('l', sizeof(long));
    *s = 100; *l = 1000;
    int* p1 = s; int* p2 = l;
    printf("%c, %c\n", *(p1 - 3), *(p2 - 3));
    printf("%d, %ld\n", *s, *l);
    free(s); free(l);
    return 0;
}

Name: Anonymous 2008-03-20 5:53

>>26
That's how variants work in COM.

Name: Anonymous 2008-03-20 5:56

>>13
Please learn Haskell and you will understand >>7. You might also cease being an asshole. Thank you.

Name: Anonymous 2008-03-20 5:58

DWORD = 4 bytes

Name: Anonymous 2008-03-20 6:01

>>45
that's hackish bullshit
Have an opaque object instead

#include <stdio.h>
#include <stdlib.h>

#define P(x) (x).ptr
#define T(x) (x).type
#define ALLOC(x, t) T(x) = #t, P(x) = malloc(x)
#define FREE(x) free(P(x))

typedef struct {
  void *ptr;
  const char *type;
} alloc_t;

int main(void) {

  alloc_t foo;
  foo = alloc(1024, int);
  printf("foo = %s\n", T(foo));
  FREE(foo);

  return 0;
}

Name: Anonymous 2008-03-20 6:09

Name: Anonymous 2008-03-20 6:09

>>49
that's hackish bullshit
Have a LISP VM instead

(car (cdr NIL))

Name: Anonymous 2008-03-20 6:35

>>51
that's hackish bullshit
Have a Scheme instead

(cons car cdr)

Name: Anonymous 2008-03-20 7:13

>>52
that's hackish bullshit
Have a BBcode enhanced post instead

Name: Anonymous 2008-04-03 13:44

>>13
hahaha learn Haskell and then bash >>7 again, asshole.

Name: Anonymous 2008-04-03 14:01

What the shit, this thread is over, die.

Name: Anonymous 2008-04-03 14:41

>>54
Have some improperly nested bbcode for your amusement.

Name: Anonymous 2008-04-04 17:45

Why would OP want to test for the type of a variable anyhow? Can't he just look at the damned declaration?

Name: Anonymous 2008-04-04 17:47

>>57
RTTI.

Name: Anonymous 2008-04-05 0:30

>>58
Pronounced Ritty

Name: Anonymous 2010-12-26 8:37


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