Thread: Casting character point into structure

  1. #31
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by shaileshk View Post
    Yes, but then if its '1234' then why it is falling at the point when its fliping the value i.e. TempBytePtr[0] = TempBytePtr[1];

    And when I comment this piece of code it do work correctly so i have doubt that the fliping is not working due to some restrictions.
    That whole function should not even compile. The compiler should tell you you are doing something very wrong and not create an executable. If it does not, you should try a different compiler.

    Although an int may be four bytes (and, presumably, a "Uint16" is two bytes), that does not mean they are arrays. You could have an array of Uin16:

    Code:
    Uint16 eg[4];
    In this case, eg[0] would be a two byte Uint16. If you only have one:
    Code:
    Uint16 eg;
    There is no such thing as eg[0] or eg[1]. There is only eg.


    If you want to swap the bytes around in a multi-byte datatype, you need to use a union (make sure you find out what a C "union" is) with the datatype in one half and array of unsigned chars in the other. Here's a little demo:

    Code:
    #include <stdio.h>
    
    union integeray {
          int X;
          unsigned char Xbytes[4];
    };
    
    int main () {
    	int i;
    	unsigned char tmp;
    	union integeray eg;
    	
    	/* set all bytes to their highest value */
    	for (i=0;i<4;i++) { eg.Xbytes[i]=255; }
    	printf("X is %d\n",eg.X);
    
    
    	/* now try a different value */
    	for (i=0;i<4;i++) { eg.Xbytes[i]=101; }
    	printf("\nX is %d\n\n",eg.X);
    
    	/* now the reverse... */
    	eg.X = 42;
    	for (i=0;i<4;i++) { printf("byte %d -> %d\n",i,eg.Xbytes[i]); }
    	puts ("\n---------\n");
    
    	/* now swap some bytes */
    	tmp = eg.Xbytes[3];
    	eg.Xbytes[3] = eg.Xbytes[0];
    	eg.Xbytes[0] = tmp;
    	for (i=0;i<4;i++) { printf("byte %d -> %d\n",i,eg.Xbytes[i]); }
    	printf("X is %d\n",eg.X);
    
    	return 0;
    }
    Witness that because X is a signed integer, with all it's bits set in all it's bytes (the first example), X is -1.

    Hopefully this can answer your question(s).
    Last edited by MK27; 06-02-2009 at 07:22 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help realy needed
    By ZohebN in forum C++ Programming
    Replies: 2
    Last Post: 04-08-2008, 09:37 AM
  2. Grammar to FSA to C code (Newbie)
    By aybe in forum C Programming
    Replies: 4
    Last Post: 02-29-2008, 02:10 PM
  3. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. trouble with overloaded operator
    By kkurz in forum C++ Programming
    Replies: 2
    Last Post: 10-31-2003, 12:59 PM

Tags for this Thread