Pointer Magic

Status
Not open for further replies.

Manojap

Broken In
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);
 

srinivasa.s

Right off the assembly line
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

In the zone
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.
 
Status
Not open for further replies.
Top Bottom