简体   繁体   中英

structure in C using pointers

i can't understand the output that give by my code which use with pointers. can anyone help me with this

here is my code,

#include <stdio.h>

void main(){

    struct stype {
        int x;
        char *p;
    };

    struct stype s[ ] = {
                        { 1 , "Colombo" },
                        { 2 , "Gampaha" },
                        { 3 , "Kalutara" },
                        { 4 , "Matara" },
                        { 5 , "Galle" },
                        };
    struct stype *t;

    t = s;
    t++;

    printf( "%d\n" , t->x );
    printf( "%c\n", *( ++t->p ) );
    printf( "%s\n" , t->p );
    printf( "%d\n" , ( ++t )->x );
    printf( "%s\n", ++t->p );
    printf( "%s\n" , ++t->p );
    printf( "%c\n" , *( ++t->p ) + 5 );

}

Here is the output i get

2
a
ampaha
3
alutara
lutara
z

The explanation is given line by line below

struct stype *t ;                  // t is a pointer to struct
t = s ;                            // t will point to the array
t++;                               // increment t, so it will point to the 
                                   //  first element i.e. s[1] 
printf( "%d\n" , t->x ) ;          // print s[1].x i.e 2
printf( "%c\n", *( ++t->p ) ) ;    // Here the precedence rules come into play. 
                                   // The Prefix increment is in level 2 and -> operator 
                                   // is in level 1, so -> operator will be carried out first   
                                   // and then ++, t-> p will point to  "Gampaha"   
                                   // and incrementing that will point 
                                   // to the next character "ampaha" 
                                   // so *(++t->p) will give 'a'
printf( "%s\n" , t->p ) ;          // t->p is already incremented, 
                                   // so it will point to "ampaha". 
printf( "%d\n" , ( ++t )->x ) ;    // t is incremented to point to s[2] 
                                   // and x, of that is taken, so will print 3
printf( "%s\n", ++t->p ) ;         // t-> p is first executed, "Kalutara",  
                                   // t->p is incremented so, "alurata" is printed.
printf( "%s\n" , ++t->p ) ;        // again t-> p is first executed, "alutara",  
                                   // t->p is incremented so, "lurata" is printed.
printf( "%c\n" , *( ++t->p ) + 5 ) ;   // t-> p is first executed "lutara", 
                                       // t-> p is incremented "utra" *( ++t->p ) is 'u' 
                                       // and 5 is added to that to get 'z'

I think u had problem in understanding the printf( "%c\\n" , *( ++t->p ) + 5 ); is it right?

*( ++t->p ) = u

ascii value of u is 117.

117+5 = 122

ascii value of z is 122.

so,the output is z.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM