Thread: Incrementing a struct pointer as if it were a char pointer.

  1. #1
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332

    Incrementing a struct pointer as if it were a char pointer.

    I'm stuck on how to fix this. The program does not work, and I know why, but I don't know how to fix it.

    It's not working because of this line:

    Code:
         tempptr += ( tempptr->glen + sizeof(AMG) )   ;
    The addition to tempptr is taking the value inside the parens on the right size and multiplying it times the size of the structure.

    I've tried all sorts of casts, but nothing is working. Help!

    The buffer array is a structure that is sent as input, built by another process. The first byte is a length of the name, then there is a fill byte, and then the name. I need to extract all three piece for an many times as there are entries.

    Code:
    #include <stdio.h>                                                   
    #include <memory.h>                                                  
                                                                         
    struct amg {                // a multi-group mapping                 
       unsigned char glen ;                                              
       unsigned char gprv ;                                              
                char gnam[0] ;                                           
    } ;                                                                  
                                                                         
    typedef struct amg AMG ;                                             
                                                                         
                                                                         
    int main(void) {                                                     
                                                                         
       int i ;                                                           
       int increment ;                                                   
       char gname[9] ;                                                   
       char * ptr ;                                                      
                                                                         
       // 3 group structures.   L=6, 0, Name=A23456                      
       //                       L=4, 0, Name=SYS1                        
       //                       L=5, 0, Name=B2345                       
                                                                         
       unsigned char buffer[] = { 6 , 0 , 'A', '2', '3', '4', '5', '6' , 
                                  4 , 0 , 'S', 'Y', 'S', '1',            
                                  5 , 0 , 'B', '2', '3', '4', '5' } ;    
                                                                         
       int grpcnt = 3 ;                                                  
                                                                         
                                                                         
       AMG * tempptr ;                                                   
                                                                         
       tempptr = (AMG *) buffer ;                                        
                                                                         
       for (i = 0 ; i < grpcnt ; i++ ) {                                 
                                                                         
          printf("tempptr=%08p, buffer@=%08p\n", tempptr, buffer ) ;     
                                                                         
          memset(gname, 0x00, (unsigned) sizeof(gname)) ;                
                                                                         
          ptr = (char *) tempptr->gnam ;                                 
                                                                         
          strncpy(gname, ptr , tempptr->glen) ;                          
                                                                         
          printf("GrpLen=%d, GrpName=>%.8s<\n", tempptr->glen, gname) ;  
                                                                         
          increment = tempptr->glen + sizeof(AMG) ;                      
          printf("Increment is %d\n", increment) ;                       
                                                                         
          tempptr += ( tempptr->glen + sizeof(AMG) )   ;                 
       }                                                                 
                                                                         
       return 0 ;                                                        
    }
    Mainframe assembler programmer by trade. C coder when I can.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Like so
    Code:
    #include <stdio.h>
    
    struct amg {                // a multi-group mapping
        unsigned char glen ;
        unsigned char gprv ;
                 char gnam[0] ;
    } ;
    
    typedef struct amg AMG ;
    
    int main(void) {
        unsigned char buffer[] = { 6 , 0 , 'A', '2', '3', '4', '5', '6' ,
                                   4 , 0 , 'S', 'Y', 'S', '1',
                                   5 , 0 , 'B', '2', '3', '4', '5' } ;
        unsigned char *ptr = buffer;
        for ( int i = 0 ; i < 3 ; i++ ) {
            AMG *this = (AMG*)ptr;
            printf("Len=%u, prv=%u, string=%.*s\n",
                this->glen, this->gprv, this->glen, this->gnam);
            ptr += this->glen + sizeof(AMG);
        }
        return 0 ;
    }
    
    $ gcc foo.c
    $ ./a.out 
    Len=6, prv=0, string=A23456
    Len=4, prv=0, string=SYS1
    Len=5, prv=0, string=B2345
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    So simple!!

    If a guy wanted to buy you a bottle of your favorite libation, for appreciation of all the times you've helped me, how would he do that?
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-08-2018, 02:27 PM
  2. incrementing a pointer
    By lmanukyan in forum C Programming
    Replies: 1
    Last Post: 01-20-2016, 08:35 AM
  3. Access to char pointer within the struct problem
    By dRichard in forum C Programming
    Replies: 4
    Last Post: 08-30-2015, 07:52 PM
  4. C assign value to char pointer in struct
    By mothermole1 in forum C Programming
    Replies: 5
    Last Post: 04-18-2011, 05:10 PM
  5. incrementing a char pointer
    By Bleech in forum C Programming
    Replies: 13
    Last Post: 11-20-2006, 11:25 PM

Tags for this Thread