DIGIT-ARC read-only archive · snapshot: July 23, 2026

Pointer Magic

3 posts · originally from https://geek.digit.in/community/threads/pointer-magic.101514/

Manojap
Manojap Original post #1
Nov 7, 2008 at 1:20 PM
Find sum of 10 nos using array of size 1 and two *pointers


int x[1],i,*xptr,i,*s;

for(i=0;i<10;i++)
{
scanf("%d",&x);
}
xptr=&x;
for(i=0;i<10;xptr++,i++)
{
*s=*s+*xptr;
}
prnitf("%d",*s);
S
Nov 8, 2008 at 7:51 PM
I do not understand what is being done in the code! It seems flawed - use of uninitialized pointers, invalid pointer assignments, etc - care to explain?
Pragadheesh
Nov 18, 2008 at 6:10 AM
in the first line, 'i' has been declared twice and in the last line, its 'printf' and not 'prnitf'.
other than these errors the program compiles successfully, but doesn't run.

the reason is you used x as 'x[10] instead of x[1]'. i.e u tried to store all the 10 values in consecutive locations of x declaring it as an array of size 1.
declaring x[1] and referencing other indexes like x[2], x[3] etc is the reason for this error.
just type, x[10] in the first line instead of x[1]. it works perfectly. so go for a different logic.