Thread: pointer and arrays

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    13

    pointer and arrays

    Ok I read the chapter again on pointers and still can't seem to grab hold of it. I understand the what pointer are and what they do in theory just can't seem to do it in real life. Here is another pointer problem we I have to show what the output would be base of the assumed values. If possible use my code to explain pointers to me I think I will understand better.

    Code:
    int list[] = {3, 9, 6};             /* assume list is at address 10240 */
    int n = 150;                         /* assume n is at address 10252 *
    int *p = list                        /* assume n1 is at address 10256 */
    printf("%d -- %d\n", &list[0], list[0]);
    printf("%d -- %d\n", &list[1], list[1]);
    printf("%d -- %d\n", &list[2], list[2]);
    printf("%d -- %d\n", &n, n);
    printf("%d -- %d -- %d\n", &p, p, *p);
    p++;
    printf("%d -- %d -- %d\n", &p, p, *p);
    p = list;
    printf("%d -- %d\n", (p + 1), *(p + 1));
    printf("%d -- %d\n", (p + 2), *(p + 2));
    printf("%d -- %d\n", p + 1, *p + 1);
    
    
    ----- My Output ---------
    
    10540 -- 3
    10541 -- 9
    10542 -- 6
    10252 -- 150
    10256 -- 10240 -- 3
    10256 -- 10244 -- 9
    10257 -- 4
    10258 -- 5
    10257 -- 4
    
    -----------------------------
    I'm not real confident my output is right. using this example could someone explain it to me.

    thanks.

  2. #2
    customized user title!
    Join Date
    Mar 2005
    Posts
    24
    Code:
    10257 -- 4
    10258 -- 5
    10257 -- 4
    This should be:
    Code:
    10257 -- 9
    10258 -- 6
    10257 -- 4
    (p + 1) will print the address of p[1], because you're incrementing where it points to, not what it points to, which is &p[1] or 10257
    Because *(p + 1) is equivalent to p[1], it will print the contents of p[1], which is 6.
    *p + 1 will print the contents of p plus 1, because * has a lower precedence than +. So it will be 3 + 1, that's why you get 4.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    13
    Thanks l0cke

    I think I'm finally understanding. At least a little.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    On an aside, you shouldn't be usind %d for pointers. You should be using %p to display the value of its address. See this for more.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Pointer Arrays
    By valaris in forum C Programming
    Replies: 3
    Last Post: 07-27-2008, 01:20 AM
  2. Pointer and multidimensional arrays
    By disruptivetech in forum C Programming
    Replies: 6
    Last Post: 06-05-2006, 03:04 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. pointer to pointer and 2D arrays
    By rfbu in forum C Programming
    Replies: 6
    Last Post: 06-23-2002, 08:36 PM