Struct/parse returning wrong information

This is a discussion on Struct/parse returning wrong information within the C Programming forums, part of the General Programming Boards category; So I have the following parsing code that sends of a string in msgbuf comprised of binary information: Code: parse_binaryxyz(msgbuf,&xyz); ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    7

    Struct/parse returning wrong information

    So I have the following parsing code that sends of a string in msgbuf comprised of binary information:
    Code:
    parse_binaryxyz(msgbuf,&xyz);
                    new_nav_posecef = TRUE;
                   (#1) printf("%x %x %x %x %x %d %d %d %d %x %d %d\n",xyz.header[0],xyz.header[1],xyz.msg_class,xyz.msg_id, xyz.len, xyz.ITOW, xyz.ECEF_X, xyz.ECEF_Y, xyz.ECEF_Z, xyz.Pacc, xyz.cs[0], xyz.cs[1]);
    				//Reset variables
    				gettingbinarymsg = FALSE;
    				msgbufindex = 0;
    The msgbuf is sent here where the information is split up into its meanings by a predifined struct that is listed below the following code:
    Code:
    int parse_binaryxyz(unsigned char *msg, struct nav_posecef *xyz)
    
    {
    
    	if ((msg[2] == 0x01) && (msg[3] == 0x01))
    
    	{//Parse the NAV_POSECEF message
    
    		//Cast the message into the nav_posecef structure
    
    		xyz = (struct nav_posecef*) msg;
            (#2)printf("%x %x %x %x %x %d %d %d %d %x %d %d\n",xyz->header[0],xyz->header[1],xyz->msg_class,xyz->msg_id, xyz->len, xyz->ITOW, xyz->ECEF_X, xyz->ECEF_Y, xyz->ECEF_Z, xyz->Pacc, xyz->cs[0], xyz->cs[1]);
    
    		return 0;
    
    	}
    
    }
    So I have a struct that is of the form:
    Code:
    struct nav_posecef{
    	unsigned char header[2];
    	unsigned char msg_class;
    	unsigned char msg_id;
    	unsigned short len;
    	unsigned long ITOW;
    	long ECEF_X ;
            long ECEF_Y ;
            long ECEF_Z ;
    	unsigned long Pacc;
    	unsigned char cs[2];
    }__attribute__((packed));
    Alright here is my problem: You can see the two printf commands: The one labeled (#2) ,inside the actual parsing code, prints out the correct information. However, (#1) , where the information is returned does not. Why is it not passing the information back to the main program correctly.

    The following is a what I see printing to the screen: (Note the first line is from the parser and correct and second is what is returned)
    b5 62 1 1 14 333670999 42332163 -536164414 341727981 136 170 164
    0 0 0 0 0 -1312895732 234 0 1122304 2 4 0
    0 0 0 0 0 -1312895732 234 0 1122304 2 4 0
    0 0 0 0 0 -1312895732 234 0 1122304 2 4 0
    0 0 0 0 0 -1312895732 234 0 1122304 2 4 0
    0 0 0 0 0 -1312895732 234 0 1122304 2 4 0
    b5 62 1 1 14 333671999 42332165 -536164415 341727982 136 152 44
    0 0 0 0 0 -1312895732 234 0 1122304 2 4 0
    b5 62 1 1 14 333672999 42332169 -536164418 341727984 136 135 196
    0 0 0 0 0 -1312895732 234 0 1122304 2 4 0
    b5 62 1 1 14 333673999 42332174 -536164419 341727990 136 125 164
    0 0 0 0 0 -1312895732 234 0 1122304 2 4 0
    b5 62 1 1 14 333674999 42332178 -536164420 341727996 136 113 97
    0 0 0 0 0 -1312895732 234 0 1122304 2 4 0
    b5 62 1 1 14 333675999 42332181 -536164420 341728001 137 103 48
    0 0 0 0 0 -1312895732 234 0 1122304 2 4 0
    b5 62 1 1 14 333676999 42332183 -536164421 341728005 137 88 208
    0 0 0 0 0 -1312895732 234 0 1122304 2 4 0

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    Are you trying to put msg into xyz?
    What you're doing is simply reassigning the address where the (local variable) xyz points to. Try the following:
    Code:
    memcpy(xyz, msg, sizeof(msg));
    This will copy the contents of msg into the memory pointed to by msg, thus reflecting the changes in your code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    7
    Would that still give me the format of the struct I created so that I could print out the information separately inside my main program once returned? Where would it be placed? After
    Code:
    xyz = (struct nav_posecef*) msg;
    or in replace of it?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    Replace, and yes, it would overwrite the information in the struct.
    Basically, the struct is just one big block of information. The variables tells the compiler how to interpret the data.
    Now that I think of it, this might work too:
    Code:
    *xyz = *(struct nav_posecef*)msg;
    It might be safer too. Try it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    7
    THank you so much. I think that last suggestion fixed my problem. That is a huge weight off my shoulders. Thanks again.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    No problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to get information from cpuid using c code
    By ZeroMemory in forum C Programming
    Replies: 2
    Last Post: 07-01-2008, 03:03 AM
  2. CPU information
    By Newmer60 in forum C Programming
    Replies: 9
    Last Post: 06-17-2008, 04:00 PM
  3. Information: Exiting Windows
    By Okiesmokie in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2002, 10:42 AM
  4. Capturing file stat information
    By Sue Paterniti in forum C Programming
    Replies: 3
    Last Post: 04-15-2002, 06:47 AM
  5. using information from an array
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 12-07-2001, 05:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21