Thread: How will this variable assignment treat ...........

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    145

    How will this variable assignment treat ...........

    I Have a code snippet
    Code:
    #include <string.h>
    #include <stdio.h>
    
    
    typedef enum {
        e_NW_NFY_ID_UNKNOWN = 0,
        e_NW_NFY_ID_ETHERNET_PLUGIN,        	/*< Ethernet cable plugged in, pv_nfy_param: not used */
        e_NW_NFY_ID_ETHERNET_UNPLUG,        	/*< Ethernet cable unplugged, pv_nfy_param: not used */
        e_NW_NFY_ID_ADDRESS_CHANGED,        	/*< IP address changed */
        e_NW_NFY_ID_ADDRESS_EXPIRED,        	/*< IP address expired */
        e_NW_NFY_ID_DHCP_SUCCESS_DHCPv4,    	/*< DHCP started successfully and get IPV4 */
        /*e_NW_NFY_ID_DHCP_FAILURE_DHCPv4,*/  	/*NW_NFY_ID_NEW_ADDRESS_DHCPv4,*/
        e_NW_NFY_ID_DHCP_SUCCESS_LINKLOCAL,   /*< DHCP started successfully but get link local IP */
    }E_NetworkStatus;
    
    
    int minecall(void *ptr);
    
    
    int minecall(void *ptr)
    {
    	E_NetworkStatus lNetworkStatus = (E_NetworkStatus)ptr ;
    	printf("ptr:%x  ,lNetworkStatus = %x \n",(E_NetworkStatus)ptr,&lNetworkStatus);
    	printf("Value : %d\n",lNetworkStatus);
    	return 0;
    }
    
    
    int main()
    {
    	E_NetworkStatus NetworkStatus;
    	NetworkStatus = e_NW_NFY_ID_ADDRESS_EXPIRED;
    	printf("NetworkStatus:%x \n",NetworkStatus);
    	minecall((void *)NetworkStatus);
    	return 0;
    }
    Questions:

    1. What will this assignment mean "E_NetworkStatus lNetworkStatus = (E_NetworkStatus)ptr ;"

    Since the Output is a correct value.

    2. Does the above assignment mean address is assigned to the variable "lNetworkStatus "?
    Or is the value is assigned to the variable "lNetworkStatus "?
    I am little confused with this assignment.....


    Thanks in advance

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    void * is the closest you get to a generic type in C, and as such is often used when dealing with a group of functions that have to have the same signature (because they're being used via function pointer or similar). You don't have any of that going on, but the basic mechanics behind the idea are still there; any value that "fits" in a pointer (however large a pointer is on your system) can survive being casted to void* and back, and an enum with six things in it can definitely fit in a pointer.

    Basic lesson: a void * does not necessarily imply "an address of something" like a "normal" pointer does.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    Quote Originally Posted by tabstop View Post
    void * is the closest you get to a generic type in C, and as such is often used when dealing with a group of functions that have to have the same signature (because they're being used via function pointer or similar). You don't have any of that going on, but the basic mechanics behind the idea are still there; any value that "fits" in a pointer (however large a pointer is on your system) can survive being casted to void* and back, and an enum with six things in it can definitely fit in a pointer.

    Basic lesson: a void * does not necessarily imply "an address of something" like a "normal" pointer does.
    Thanks for the reply

    My confusion was with the "above highlighted line"

    Why didn't we initialize the line with the below line:
    E_NetworkStatus lNetworkStatus = *(E_NetworkStatus *)ptr ;

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Because the value that main() converted to (void *) was not a pointer. Retrieving its value therefore does not involve dereferencing any pointer.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static variable assignment doubts
    By sanddune008 in forum C Programming
    Replies: 2
    Last Post: 04-22-2010, 07:42 AM
  2. Dynamic variable assignment
    By Epy in forum C Programming
    Replies: 4
    Last Post: 10-15-2009, 02:12 PM
  3. out variable assignment
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 06-15-2008, 07:12 PM
  4. extern variable assignment
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 07:41 AM
  5. About Variable assignment
    By nick048 in forum C Programming
    Replies: 5
    Last Post: 04-07-2007, 09:47 AM