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 4 of 7.
Sanjay Yadav said:
1 decade ago
#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++;//error because a is an r-value and we cant increment it
}
return 0;
}
//Correct form.
int *p;
p=a;
printf("%d",*p);
p++;
int main()
{
int a[] = {10, 20, 30, 40, 50};
int j;
for(j=0; j<5; j++)
{
printf("%d\n", a);
a++;//error because a is an r-value and we cant increment it
}
return 0;
}
//Correct form.
int *p;
p=a;
printf("%d",*p);
p++;
Jagan said:
1 decade ago
Here LValue means a[].
We have to specify printf("%d",a[i]); instead of 'a'.
We have to specify printf("%d",a[i]); instead of 'a'.
Deepali said:
1 decade ago
A is memory address & we can not use %d for address. We have to use unsigned int.
Sudha said:
1 decade ago
@Sugan.
An "lvalue" of a variable is the value of its address, i.e. where it is stored in memory.
The "rvalue" of a variable is the value stored in that variable (at that address).
An "lvalue" of a variable is the value of its address, i.e. where it is stored in memory.
The "rvalue" of a variable is the value stored in that variable (at that address).
Prashant singh said:
1 decade ago
Array name itself act as a pointer and it is a constant pointer. So it can not be modified even if you apply a Lvalue to it still it would give you the same error. So you can't modify constant pointer.
Simply a constant pointer can not be modified.
Simply a constant pointer can not be modified.
Kunal Bansal said:
1 decade ago
It's simple , thing is you can do arithmetic operation like ++,-- and others on pointers but these operation are not allowed while implementing on array .
Reason : pointer is a variable
array name is not a variable
Reason : pointer is a variable
array name is not a variable
Praveen said:
1 decade ago
Array name itself act as a pointer and it is a constant pointer. So it can not be modified even if you apply a Lvalue to it still it would give you the same error. So you can't modify constant pointer.
You can write this way
printf("%d",(a+1)); inside for loop. It will print the addresses if you want values to be printed then write *(a+1) instead of (a+1).
You can write this way
printf("%d",(a+1)); inside for loop. It will print the addresses if you want values to be printed then write *(a+1) instead of (a+1).
Veeraselvi said:
1 decade ago
What you mean by Lvalue & Rvalue?
RKV Gopi said:
1 decade ago
yes I agree with Neha..
Neha said:
1 decade ago
The error is because array is a constant pointer and we can't increment or decrement its base address, we can do it by assigning it into some pointer variable. Thats why the error is lvalue error as we are not providing left side pointer for assignment.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers