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.
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'.
Aditya reja said:
1 decade ago
The name of An Array is automatically constant pointer (i.e. cannot be alter) that always points to its 0th element. Due to its constant nature ++ or -- operators can't be apply to array's name.
Aim said:
1 decade ago
Here printf statement display address of memory location so when we do a++ is increment the address value but here left side any variable is not available to take it. So error is LValue required
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.
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?
Atul yadav said:
1 decade ago
Simply replace a++; with a[j]++;
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.
Chandru said:
1 decade ago
@Sudha, can you please explain me with example program?
POOJA said:
1 decade ago
Very simple logic.
a is an array.
We can't perform following operations on array:
a++;
a--;
a+=2;
....
If pointer to an array will be there then we can perform above operations.
a is an array.
We can't perform following operations on array:
a++;
a--;
a+=2;
....
If pointer to an array will be there then we can perform above operations.
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++;
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers