# Thread: A multi part Q

1. ## A multi part Q

I hate to call upon you guys again, and I guess in a way I shouldn't be asking, but could you check over these answers on a question about pointers and see if they are right. If they are, then I am finally getting a hold of this whole pointer concept.

Q5 (10 pts): Suppose that the following declarations are in effect: Make sure you explain why on each part to get any credit.
Code:
```int a [] = {5, 15, 34, 54, 14, 2, 52, 72};
int *p = &a[1];
int *q = &a[5];```
(a) What is the value of *(p+3)?
(b) What is the value of *(q-3)?
(c) Can I perform addition and subtraction on pointers? If so, what is q – p?
(d) Is the condition p < q true or false?
(e) Is the condition *p < *q true or false?
(f) Can I do the following, *p = a [4]; ?
(g) What happens when I do this, *p = *q; ?
(h) What happens when I do this, p = q; ?
(i) Is it legal for me to write the following line: p = a; ?
(j) Is it legal for me to write the following line: p = a [1]; ?

Legal refers to whether the code will compile or not.

a) 14. *p=the second element in the array, 15. *(p+3) is the 5th element, 14.
b) 34. *q=the sixth element in the array, 2. *(q-3) is the 3rd element, 34.
c) You can only add pointers. There is no pointer subtraction.
d) True, because the addresses of elements in arrays fall in succession, so since the address of p is the first element in the array, and q is the 5th element, p<q.
e) False, because the contents found at p=15, and the contents found at q=2. Thus 15>2.
f) Yes, because when declaring pointers with arrays, the & is not needed. It is an exception to the pointer rule.
g) The contents found at *p now becomes = to the contents at *q because the second element in the statement copies its value to
the first.
h) The address of q is copied to to the address of p for the same reason as stated in g.
i) Yes, because an array is looked at as a constant, so no & is needed. Although, with removing the [], it automatically points to the first element in the array.
j) Yes, because the address of the second element of array a is copied to the points p.

2. quzah we call upon your almighty pointer skills.

I don't have a compiler to test but I think the answer to c) is valid, because it wouls subtract the memory addresses
f) yes, but the & isn't even in the discussion. the statement says that the value of p == value of a[4]
j) no you need the & because ptr=string is the same as ptr=&string[0] a string already is a memory address but an element in the string isn't

3. ## Re: A multi part Q

Originally posted by pxleyes
c) You can only add pointers. There is no pointer subtraction.
http://www.eskimo.com/~scs/cclass/notes/sx10c.html

Originally posted by pxleyes
h) The address of q is copied to to the address of p for the same reason as stated in g.
The value of q is copied to the value of p. (The value happens to be an address but the addresses of p and q stay the same)

4. Thanks major for that info.

Can any of you explain what linuxdude said in reference to part j. I am slightly confused about that one.

THe reason I ask is because I was evalusating anothing question, and i plugged it into a compiler and i got no error. see below:

Code:
```int main()
{
int *zPtr;
int *aPtr = NULL;
int number, i;
int z [5] = {1, 2, 3, 4, 5};
zPtr = z;

number = *zPtr; /* use pointer to get the first value of the array */
number = zPtr [2]; /* assign element with subscript 2 to number */
for (i = 0; i <= 4; i++) { /* print array z using zPtr */
printf("%d", zPtr[i]);
}

system("PAUSE");
return 0;
}```

5. Try compiling with zPtr = z[1]. It will probably compile, but you should get warnings and weird things will probably happen when you run it.

z[1] is not an address but a value.

6. number is a normal integer in your example. but in the your original post it is a pointer ot type int; any questions feel free to ask
here is the gcc error with it as type int
Code:
`test.c:13: warning: assignment makes pointer from integer without a cast`

7. I get it now. Thanks guys.

Code:
```#include <stdio.h>

int main(void)
{
int a [] = {5, 15, 34, 54, 14, 2, 52, 72};
int *p = &a[1];
int *q = &a[5];
/* (a) What is the value of *(p+3)? */
printf("*(p+3) = %d\n", *(p+3));
/* (b) What is the value of *(q-3)? */
printf("*(q-3) = %d\n", *(q-3));
/* (c) Can I perform addition and subtraction on pointers? If so, what is q - p? */
printf("q - p  = %d\n", (int)(q-p));
/* (d) Is the condition p < q true or false? */
printf("p < q  = %s\n", p < q ? "true" : "false");
/* (e) Is the condition *p < *q true or false? */
printf("*p<*q  = %s\n", *p<*q ? "true" : "false");
/* (f) Can I do the following, *p = a [4]; ? */
*p = a [4]; /* yup */
/* (g) What happens when I do this, *p = *q; ? */
*p = *q;
printf("*p = %d, *q = %d\n", *p, *q);
/* (h) What happens when I do this, p = q; ? */
p = q;
printf("p = %p, q = %p\n", (void*)p, (void*)q);
/* (i) Is it legal for me to write the following line: p = a; ? */
p = a;
/* (j) Is it legal for me to write the following line: p = a [1]; ?
p = a[1]; // no -- Type mismatch (assignment) (int * = int)
*/
return 0;
}```
[edit1]D'oh! Forgot the *p = *q; part (g).[/edit1]
[edit2]Grrrr. *q in printf format literal instead of q (h).[/edit2]