Thread: array and pointers confusion

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    2

    Question array and pointers confusion

    I've been trying to figure this out for a few days, when I have time - but I really can't understand parts of this code.

    Code:
    #include <stdio.h>
    
    char *c[] = {
    "ENTER",
    "NEW",
    "POINT",
    "FIRST"
    };
    char **cp[] = { c+3, c+2, c+1, c };
    char ***cpp = cp;
    
    main()
    {
    printf("%s", **++cpp);
    printf("%s ", *--*++cpp+3);
    printf("%s", *cpp[-2]+3);
    printf("%s\n", cpp[-1][-1]+1);
    }
    I know how the first part printf("%s", **++cpp); points to "POINT", but the second print statement is beyond me completely. I was wondering if anyone could show me how to read this.

    By the way, when run, it prints "POINTER STEW".

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    there is a lot of const missing here...

    to understand it - split the expression in steps

    printf("&#37;s ", *--*++cpp+3);
    Code:
    char *** temp = ++cpp ; /* points to c+1 */
    char** temp2 = *temp; /* == c+1 */
    char** temp3 = --temp2; /* == c */
    char* temp4 = *temp3; /* points to "ENTER" */
    char* temp5 = temp4 + 3; /* points to "ER" */
    printf("%s",temp5);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User nenpa8lo's Avatar
    Join Date
    Jan 2008
    Posts
    42
    That's really bad coding practice. If you keep writing so obscured code you will find sooner or later that you waste majority of your time to understand own code.

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    I don't think that the OP wrote this...

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    2
    Quote Originally Posted by nenpa8lo View Post
    That's really bad coding practice. If you keep writing so obscured code you will find sooner or later that you waste majority of your time to understand own code.
    I recently bought a book called C puzzles, and I was storming through it until I got to the pointer section. This was a puzzle in the book, and in the explainations section, there wasn't even a good explaination of how it all worked - hence my post here.

    vart, nice one dude.

  6. #6
    Registered User nenpa8lo's Avatar
    Join Date
    Jan 2008
    Posts
    42
    OK. So .....
    Code:
    printf("%s", *cpp[-2]+3);
    is the same as
    Code:
    char **temp1 = cpp[-2];    /* points to c+3 */
    char *temp2   = *temp1;    /* points to FIRST */
    char *temp3    = temp2 + 3;/* points to ST */
    printf("%s", temp3);
    and
    Code:
    printf("%s\n", cpp[-1][-1]+1);
    is the same as
    Code:
    char **temp1 = cpp[-1];    /* points to c+2 */
    char *temp2 = *--temp1;	   /* points to NEW */
    ++temp2;		   /* points to EW */
    printf("%s", temp2);
    hope that help and outputs are
    Code:
    POINT, ER, ST, EW

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of pointers to an array pointers
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 07-28-2008, 11:45 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM