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 1 of 7.
Amrendra said:
8 years ago
The lvalue is an object that has identifiable memory location.
For example:
int i=9 ;
here i is a lvalue because it has identifiable memory location and possible to modify its content.
int *p=&i (identifiable memory location),
*p=4;
rvalue : any object that is not lvalue.
For exapmle: int i =2; here 2 is a rvalue because int *p= &2 //error,
int x=y+5; y+5 is a rvalue because int *p= &(y+5) //error.
For example:
int i=9 ;
here i is a lvalue because it has identifiable memory location and possible to modify its content.
int *p=&i (identifiable memory location),
*p=4;
rvalue : any object that is not lvalue.
For exapmle: int i =2; here 2 is a rvalue because int *p= &2 //error,
int x=y+5; y+5 is a rvalue because int *p= &(y+5) //error.
Nila said:
1 decade ago
@Bele.
We use the array's variable name(without subscript )for sake of passing them to a function which receives with a pointer variable.
So now that pointer will be holding the base address of the array..using which you could access the rest of the array elements(coz array elements are continuously stored).
This reduces the degrading of performance like stack overflow in passing arrays on the whole to a function.
We use the array's variable name(without subscript )for sake of passing them to a function which receives with a pointer variable.
So now that pointer will be holding the base address of the array..using which you could access the rest of the array elements(coz array elements are continuously stored).
This reduces the degrading of performance like stack overflow in passing arrays on the whole to a function.
Anil said:
1 decade ago
Arrays internally follows const pointer mechanism.
ex: int const *arr =(int*)malloc(10*sizeof(int));
Can you perform increment operation here. No never not possible.
int *arr =(int*)malloc(10*sizeof(int));
Here increment operation works.
So its not matter of L-value. We can't change array base address that's the answer. Because array internally for const pointer mechanism.
ex: int const *arr =(int*)malloc(10*sizeof(int));
Can you perform increment operation here. No never not possible.
int *arr =(int*)malloc(10*sizeof(int));
Here increment operation works.
So its not matter of L-value. We can't change array base address that's the answer. Because array internally for const pointer mechanism.
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).
Surender said:
1 decade ago
@Gouri
L Value means Location Value ( Addressable memory ).
Here a[], a is constant pointer, pointing to the base address of a array 'a'.
we can't update constant after assignment.
So here L-value required means Our Program asking for such a memory location where he can store value of 'a' after the increment (a++).
Hope you got your answer.
L Value means Location Value ( Addressable memory ).
Here a[], a is constant pointer, pointing to the base address of a array 'a'.
we can't update constant after assignment.
So here L-value required means Our Program asking for such a memory location where he can store value of 'a' after the increment (a++).
Hope you got your answer.
Parth said:
8 years ago
The reason why Dennis didn't allowed us to play with the name of the array was b/c it is the only way to access the array we have declared. Ones the base address of the array is lost we can't access it. It still exists in the memory but it's hidden in a pool of 1073741824 bytes of RAM memory.(If you are running on 1GB ).
(2)
Deepak said:
1 decade ago
Ya. What Ashwini said was right. I want to add some more things to it. It does not allow us to directly over write the base pointer. So if we want to change it, We need to copy it to another pointer and then perform increment or decrement. Tht is what the error it is giving 'L value required' (Left side value).
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++;
Vinay Rao said:
9 years ago
Array name a holds the base address of the array. You cannot change it.
In the above program,
a++ means a= a+1.
But a holds base address...It's not a variable.
So, if base address is 1000, it becomes;
1000 = 1000+1;
This does not mean anything. Because Lvalue(i.e., Left side reference) is missing.
In the above program,
a++ means a= a+1.
But a holds base address...It's not a variable.
So, if base address is 1000, it becomes;
1000 = 1000+1;
This does not mean anything. Because Lvalue(i.e., Left side reference) is missing.
Bele said:
1 decade ago
What is the reason to make array names as cons pointer, and give them the address of the first element, if we give them the address of the first element why we can't modify them and get the other elements? is there security issue or performance or something else. By not making arrays names const ptr?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers