1

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
10
  • Which part specifically you have problem understanding? Commented Dec 3, 2018 at 5:25
  • 1
    Unless you are on a non-conforming embedded system, void main() is an invalid program startup. See: C11 Standard §5.1.2.2.1 Program startup p1 (draft n1570). See also: See What should main() return in C and C++? Commented Dec 3, 2018 at 5:27
  • @SouravGhosh *( ++t->p ) and ++t->p what happen with these two? Commented Dec 3, 2018 at 5:30
  • 2
    This looks an awful lot like a problem designed to introduce you to C Operator Precedence Commented Dec 3, 2018 at 5:30
  • @DavidC.Rankin -- I think that some versions of Microsoft (non-conforming) C allow void main() as a signature. Commented Dec 3, 2018 at 5:32

2 Answers 2

2

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'
Sign up to request clarification or add additional context in comments.

4 Comments

"Since -> and ++ operator are in the same level" -- wrong. That only applies to Post-Increment, not Pre-Increment. Pre-Increment is Level 2. No dings for it, but it needs fixing. Also, explanations in comments that spill off the side of the page are not as helpful as tidy paragraphs explaining the same thing.
@DavidC.Rankin Exactly. Also The associativity is checked from left to right for level 2
@DavidC.Rankin Pre increment is level 3 and arrow is level 2
@VidorVistrom - see: C Operator Precedence
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.