This is not an error, but rather a statement about arrays.

  • Thread starter Thread starter africanmasks
  • Start date Start date
  • Tags Tags
    Code Errors Point
AI Thread Summary
The discussion focuses on identifying errors in a code segment involving pointers and arrays in C. The main issues highlighted include the incorrect use of pointer arithmetic with `++zPtr`, which should increment the value pointed to rather than the pointer itself. It is also noted that `number = zPtr;` is erroneous because `zPtr` is an integer pointer and cannot be assigned directly to an integer variable. The attempt to access elements with `number = *zPtr[2];` raises confusion regarding whether `zPtr[2]` is a pointer. Lastly, it is clarified that arrays cannot be incremented directly, emphasizing the importance of understanding pointer types and array indexing.
africanmasks
Messages
11
Reaction score
0

Homework Statement


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;

Homework Equations



None.

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.
 
Physics news on Phys.org
africanmasks said:

Homework Statement


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;

Homework Equations



None.

The Attempt at a Solution



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

That's not the error. You can increment pointers and you can increment what pointers are pointing to.
africanmasks said:
B.) Should zPtr be assigned to the address of number? zPtr=&number;
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.
africanmasks said:
C.) Don't Know
*zPtr[2] refers to the thing that zPtr[2] is pointing to. Is zPtr[2] a pointer?
africanmasks said:
D.) Don't Know
How many times will the for loop run? How many elements are in the array?
africanmasks said:
E.) Don't Know
This code (number = *sPtr;) would actually be OK except that sPtr is not the right type of pointer. What is the type of sPtr?
africanmasks said:
F.) Arrays cannot be incremented.
 

Similar threads

Replies
1
Views
10K
Replies
3
Views
1K
Replies
3
Views
1K
Replies
5
Views
3K
Replies
4
Views
1K
Replies
12
Views
2K
Replies
17
Views
2K
Back
Top