Thread: Is (char *)'c' accepted in C standard ?

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    Is (char *)'c' accepted in C standard ?

    Hello everyone,,

    is this code cross-platform??

    Code:
    #include <stdio.h>
    
    void *value(char c){
            if( c=='s')
                    return "String";
            else if(c=='n')
                    return (int *)45;
            else if(c=='c')
                    return (char *)'f';
    
            return NULL;
    }
    
    int main(void){
            printf("%d\n",value('n'));
            return 0;
    }

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    No. Why would it be? You can't print addresses with %d. No one said,
    (char *) -> (void *) -> (int) will give (char *) -- Although it probably will. A character pointer, or any pointer for that matter doesn't have to fit in an integer.

    And why would you want to do something that stupid?

  3. #3
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Well it is probably cross-platform but ... what are you trying to do here? I am almost certain it will not work as you think...I mean this is a rather twisted use of a void ptr...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    It would also depend on the character set, i.e. what 'f' resolves to. So again, not portable or cross-platform.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by fahd View Post
    Hello everyone,,

    is this code cross-platform??
    No, the code is undefined, because pointers are assigned nonsense values. Even though the pointers are never dereferenced, the mere EXISTENCE of such pointers immediately puts you into undefined territory. I have no idea what this piece of code is supposed to do, but whatever it is, it's ridiculous
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Hard coding memory addresses is generally a bad idea as well:
    Code:
    return (int *)45;

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    On x86-64 for example, int's are 4 bytes (for backward-compatibility) and pointers are 8 bytes.

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    2
    Thank you very your replies

    I just want to be able to do someting like this

    Code:
    void                g_object_set                        (gpointer object,
                                                             const gchar *first_property_name,
                                                             ...);
    ......
    g_object_set(OBJ,     "property_name"
                                      ,value("propery_name"),NULL);
    if there is a good approch, help me.
    Last edited by fahd; 02-19-2010 at 03:58 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help :(
    By ramenen in forum C++ Programming
    Replies: 1
    Last Post: 02-17-2010, 04:31 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM
  5. Extra printed stmts...why?
    By mangoz in forum C Programming
    Replies: 4
    Last Post: 12-19-2001, 07:56 AM