Thread: A multi part Q

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    73

    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. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    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. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    72

    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)
    Last edited by major_blagger; 03-19-2004 at 10:27 PM.

  4. #4
    Registered User
    Join Date
    Feb 2004
    Posts
    73
    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;
    }
    Last edited by pxleyes; 03-19-2004 at 10:23 PM.

  5. #5
    Registered User
    Join Date
    Feb 2004
    Posts
    72
    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.
    Last edited by major_blagger; 03-19-2004 at 10:35 PM.

  6. #6
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    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
    Last edited by linuxdude; 03-19-2004 at 10:36 PM.

  7. #7
    Registered User
    Join Date
    Feb 2004
    Posts
    73
    I get it now. Thanks guys.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Try asking your compiler.
    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]
    Last edited by Dave_Sinkula; 03-19-2004 at 11:05 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to get domain part from URL
    By George2 in forum C# Programming
    Replies: 2
    Last Post: 07-23-2008, 12:06 PM
  2. How to get a part of a string from a point onwards
    By pseudonoma in forum C Programming
    Replies: 2
    Last Post: 03-22-2008, 04:09 PM
  3. Replies: 9
    Last Post: 07-11-2006, 04:28 AM
  4. Can someone look over my code? part 2
    By brooklyn in forum C++ Programming
    Replies: 10
    Last Post: 04-18-2006, 06:33 AM
  5. Suspicious Pointer Conversion
    By mr_spanky202 in forum C Programming
    Replies: 35
    Last Post: 04-11-2003, 12:35 PM