Thread: A question on arrays and pointers and right associativity

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    45

    A question on arrays and pointers and right associativity

    I've got another question that hinges on right associativity.

    I got to the next chapter, on pointers and arrays, and was having some difficulty understanding the statements in this chart. I thought it might help if I added assignments to each of them to better understand what they mean.

    I'm not sure if the natural language expressions I wrote back are correct. My additions are in blue highlight marks. Are they the correct natural language text for the corresponding blue expressions?

    To get a better sense of my train of thought, here are the full notes I wrote down, which contain some of the book text. Uploaded the PDF to an external site since my PDF is bigger than allowed by the message board: https://www.pdf-archive.com/2017/01/02/and-combination/

    http://i.cubeupload.com/qg1eLv.jpg

    A question on arrays and pointers and right associativity-11-jpg
    Last edited by potomac; 01-02-2017 at 09:06 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The last one doesn't involve any increment of any pointer.

    Two of them aren't even legal code.
    Code:
    $ gcc -std=c99 -Wall main.c
    main.c: In function ‘main’:
    main.c:21:10: error: lvalue required as left operand of assignment
       (*p)++ = j;
              ^
    main.c:25:10: error: lvalue required as left operand of assignment
       ++(*p) = j;
    You can always write code to test ideas you know.
    Code:
    #include <stdio.h>
    #include <stddef.h>
    #include <stdlib.h>
    
    void dump ( char *s, int *arr, int *p, int n ) {
      printf("%s results in elements=",s);
      for ( int i = 0 ; i < n ; i++ ) {
        printf("%d ", arr[i]);
      }
      ptrdiff_t pd = p - arr;
      printf("\np is pointing to the %td element\n",pd);
    }
    
    int main()
    {
      int arr[10] = { 0 };
      int *p = arr;
      int j = 3;
      *(p++) = j;
      dump("*(p++) = j",arr,p,10);
    //   (*p)++ = j;
    //   dump("",arr,p,10);
      *(++p) = j;
      dump("*(++p) = j",arr,p,10);
    //   ++(*p) = j;
    //   dump("",arr,p,10);
      return 0;
    }
    
    $ gcc -std=c99 -Wall main.c
    $ ./a.out 
    *(p++) = j results in elements=3 0 0 0 0 0 0 0 0 0 
    p is pointing to the 1 element
    *(++p) = j results in elements=3 0 3 0 0 0 0 0 0 0 
    p is pointing to the 2 element
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about pointers and arrays
    By Daikon in forum C++ Programming
    Replies: 4
    Last Post: 10-16-2016, 05:08 AM
  2. Question in arrays and pointers
    By ASophokleous in forum C Programming
    Replies: 3
    Last Post: 10-27-2013, 11:35 AM
  3. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  4. Beginner's question concerning pointers and arrays
    By rcomj in forum C Programming
    Replies: 5
    Last Post: 08-11-2009, 09:09 AM
  5. Quick question arrays/pointers
    By liljp617 in forum C Programming
    Replies: 6
    Last Post: 10-02-2008, 12:11 PM

Tags for this Thread