# Point out errors in code

1. Nov 15, 2009

1. The problem statement, all variables and given/known data
Suppose we have the following program segments:

int *zPtr;
int *aPtr = NULL;
void *sPtr = NULL;
int number,i;
int z[5] = {1,2,3,4,5};
sPtr = z;

point out the error of the following code:
(a) ++zPtr;
(b) number = zPtr;
(c) number = *zPtr[2];
(d) for(int i=0;i<=5;i++)
printf("%d ", zPtr);
(e) number = *sPtr;
(f) ++z;

2. Relevant equations

None.

3. The attempt at a solution

A.) It should be (*zPtr)++. Whatever zPtr is pointing to should be incremented, not the pointer.

B.) Should zPtr be assigned to the address of number? zPtr=&number;

C.) Don't Know

D.) Don't Know

E.) Don't Know

F.) Arrays cannot be incremented.
1. The problem statement, all variables and given/known data

2. Relevant equations

3. The attempt at a solution

2. Nov 15, 2009

### Staff: Mentor

That's not the error. You can increment pointers and you can increment what pointers are pointing to.
No, that's not it. zPtr and number are two different types. zPtr is of type int *, meaning that it is a pointer to an int. number is of type int. It is an error to take the address of an int value.
*zPtr[2] refers to the thing that zPtr[2] is pointing to. Is zPtr[2] a pointer?
How many times will the for loop run? How many elements are in the array?
This code (number = *sPtr;) would actually be OK except that sPtr is not the right type of pointer. What is the type of sPtr?