Thread: C string into an int pointer

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    19

    Unhappy C string into an int pointer

    I've bee having a problem with my code. I have a structure with a nested union that contains to pointers. I want to read the first 4bytes from a buffer into a the integer pointer from the structure.

    This si the code:

    Code:
    typedef struct s_test{
    		char	m_flag;
    			union{
    			char	*id;
    			int		*value;
    		}utest;
    		
    }ST;
    
    
    int main()
    {
    	char 	test_buffer[]="abcdefghijkl12345678";
    	ST s_table[100];
    	int i=0;
    
    	s_table.utest.id= test_buffer;
    		
    	for (i=0; i<3; i++)
    	{
    		s_table[i].utest.value=&test_buffer[i]; /* How do i make my structure point to the arra?????*/
    		printf(" Buffer &#37;08x\n\n", *(s_table.utest.value));
    	}
    	
    	  return 0;
    }
    Many thanks.
    Last edited by cosmo1996; 07-31-2007 at 08:37 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ehm, exactly what do you want to do?

    value is a pointer to an integer, and you are setting it to the address of a char in your test_buffer... That seems to be fine code as such.

    Then you print the value that the pointer points at. I expect that to print 64636261 assuming it's an x86 processor (or other little endian one). Is this what you wanted?

    By the way, a char * and int * is essentially the same type - the only difference is the size of the data it points to. So if you were to add a line to print a %x of *s_table.utest.id, you'd get 61.

    --
    Mats

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    19
    Yes i want to print 64636261, but I am not getting that ... I get an error in my int pointer when i make it point to the buffer....
    Code:
    s_table[i].utest.value=&test_buffer[i];
    Thank you.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What error?

    Shouldn't you index the printout too?

    --
    Mats

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    19
    At compilation it give me:
    error #2168: Operands of = have incompatible types 'int *' and 'char *'.
    *** Error code: 1 ***
    on this line:
    s_table[i].utest.value=&test_buffer[i];

    Is this a wrong assignment????

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, there's two options:

    1. use the fact that you are using a union in the first place: set the "id" part to point to the string.

    2. use a cast (int *) before the & in that line. That tells the compiler "make this type compatible with "int *". Cast's aren't "pretty" programming, but sometimes you have to.

    --
    Mats

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    19
    Thank you very much... lets see if my boss likes it with the cast... I'll let you know later..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM