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
Click For Summary
SUMMARY

The discussion focuses on identifying errors in C programming code involving pointers and arrays. Key points include that incrementing a pointer should be done using the correct syntax, such as (*zPtr)++, and that zPtr must be assigned a valid address, not the address of an int. Additionally, accessing elements through pointers requires understanding the types involved, as seen in the errors related to zPtr and sPtr. Finally, it is established that arrays cannot be incremented directly.

PREREQUISITES
  • Understanding of C programming syntax and semantics
  • Knowledge of pointer types and memory addressing in C
  • Familiarity with array manipulation and indexing
  • Basic debugging skills in C programming
NEXT STEPS
  • Study pointer arithmetic in C programming
  • Learn about type casting and pointer type compatibility
  • Explore array and pointer relationships in C
  • Practice debugging common pointer-related errors in C code
USEFUL FOR

C programmers, computer science students, and software developers looking to deepen their understanding of pointers and arrays in C.

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 ·
Replies
1
Views
11K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
9
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 18 ·
Replies
18
Views
3K