• C

simple pointer questions

Why following first & second program gives lvalue error. Also how following 3rd program is evaluated to print output 2 3. i need explanation.

1)      void main()
            {
        int a[]={1,2,3,4,5,6};
        int *ptr=a+2;
        printf("%d %d",*++a,--*ptr);
            }


2) void main()
            {
        int a=50;
          const int* const ptr=&a;
        printf("%d %d",*ptr++,(*ptr)++);
            }


3) void main()
            {
        int a[]={1,2,3,4,5,6};
        int *ptr=a+2;
        printf("%d %d",--*ptr+1,1+*--ptr);
            }

linetnewAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sachin_jindalCommented:
In the first question printf stsement try to change the base address of array which is not possible bcz this is the only way to get acces to the statically  allocated memory



in second question ptr is declared as the const pointer which point to the const integer so any attemp to change the location where ptr is pointing or to change the value pointed by ptr would be an  compilation error



 
arun_vipCommented:
1)     void main()
           {
       int a[]={1,2,3,4,5,6};
       int *ptr=a+2;
       printf("%d %d",*++a,--*ptr);
           }
You are trying move the array pointer. So it is failing. You are doing mistake here.
       printf("%d",*++a);
In C, we can't change the address of array.


2) void main()
           {
       int a=50;
          const int* const ptr=&a;
       printf("%d %d",*ptr++,(*ptr)++);
           }
You are trying change the const pointer. So it is failing. You are doing mistake here.
       printf("%d",*ptr++);
In C, we can't change the address of const pointer.

3) void main()
           {
       int a[]={1,2,3,4,5,6};
       int *ptr=a+2; //Ptr points to a[2] = 3
       printf("%d %d",--*ptr+1,1+*--ptr);
           }

In the second line ptr points to array[2];
first printf
--ptr = array[1]
value of *--ptr = *(array[1]) = 2
1+ *--ptr = 3
Second printf
ptr + 1= array[2]
*ptr +1 = value(array[2]) = 3
-- *ptr + 1 = 2

So the value is 2, 3

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
avizitCommented:
1) you cna replace

>>> printf("%d %d",*++a,--*ptr);

by

 printf("%d %d",++(*)a,--(*ptr));

first you have to dereference and then increment , i have added the brackets to make itclear
 printf("%d %d",++*a,--*ptr);  will work just as fine

avizitCommented:
3)

      printf("%d %d",--*ptr+1,1+*--ptr);

This I think is bad programming , the output value will depend on the order in which the arguments to a function( printf in this case ) are evaluated . whether they are evaluated from left to right or vice versa.
the output as such would be dependent on the compiler. Practically you should not code like this


It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.