C Programming - Pointers - Discussion
Discussion Forum : Pointers - Point Out Errors (Q.No. 2)
2.
Point out the error in the program
#include<stdio.h>
int main()
{
int a[] = {10, 20, 30, 40, 50};
int j;
for(j=0; j<5; j++)
{
printf("%d\n", a);
a++;
}
return 0;
}
Discussion:
65 comments Page 2 of 7.
Sam said:
1 decade ago
Here to do the computation left hand value of array are required, but here a is treated as simple variable in for loop not as array that why error is coming.
Purushotham said:
1 decade ago
All of you know array name itself act as a pointer. Here this base pointer act as a constant pointer. Constant pointer means it will not allowed to change address. So it gives an error.
Thiyagu said:
1 decade ago
Nice explanation from sam.
Sonu said:
1 decade ago
Lvalue means adressable value where rvalue means actual value ie location and read value respectively.
Keerthi said:
1 decade ago
How to rewrite the program without error? I can't understand from above explanation? any one can do this?
Prakash said:
1 decade ago
int main()
{
int a[] = {10, 20, 30, 40, 50};
int j;
for(j=0; j<5; j++)
{
printf("%d\n", a[j]);
}
return 0;
}
compile this you all can note down the difference ..Thank You
{
int a[] = {10, 20, 30, 40, 50};
int j;
for(j=0; j<5; j++)
{
printf("%d\n", a[j]);
}
return 0;
}
compile this you all can note down the difference ..Thank You
Kiran said:
1 decade ago
From the above explination in order to rectify the error we use a pointer variable which points to an array.
int main()
{
int a[] = {10, 20, 30, 40, 50};
int j,*ptr;
ptr=&a;
for(j=0; j<5; j++)
{
printf("%d\n", *ptr);
ptr++;
}
return 0;
}
int main()
{
int a[] = {10, 20, 30, 40, 50};
int j,*ptr;
ptr=&a;
for(j=0; j<5; j++)
{
printf("%d\n", *ptr);
ptr++;
}
return 0;
}
Kiran said:
1 decade ago
Can any one explain what exactly lvalue and rvalue means ?im not getting them?
Rupinderjit said:
1 decade ago
"a" in function printf acts as an pointer to first element.But when we increment the base address,that is,a++,it gives L value error because we can't increment the subscript of an array in this manner,either we need array indexing plus for loop or we can use pointer arithmetic.
Shri said:
1 decade ago
When we store any value in variable, then c stores it in its internel data structure which is maintained for every program.
To store an variable to things are maintains 1: its address(l value) and 2: its value(r value).
In here a++ is incrementing something c (l value) doesn't know.
To store an variable to things are maintains 1: its address(l value) and 2: its value(r value).
In here a++ is incrementing something c (l value) doesn't know.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers