Thread: question on higher dimensional pointer

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    10

    question on higher dimensional pointer

    Code:
    #include<stdio.h>
    char *c[]={"ENTNG","NST","AMAZI","F​IRBE"};
    char **cp[]={c+3,c+2,c+1,c};
    char ***cpp=cp;
    void main()
    {
    printf("%s",**++cpp);
    printf("%s",*--*++cpp+3);
    printf("%s",*cpp[-2]+3);
    printf("%s",cpp[-1][-1]+1);
    
    }
    please explain clearly..

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What is it we are supposed to be explaining to you? How about I explain how to use main correctly?


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

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    10
    explain how cpp,cp,c are pointing..and give the answer by giving explaination with the help of some starting address..

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How about you explain it. Go through each line and explain what they mean as far as you understand them.


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

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    10
    i understod up to 3 printf statements but i did not get cpp[-1][-1]+1..try to solve and explain each line.we cant go directly to 4 th statement because it is incrementing by unary operator..so cpp is modifying..solve from start..i got the correct ans upto 3 pfs correctly..

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    char *c[]={"ENTNG","NST","AMAZI","F​IRBE"};
    This is an array of pointers to characters, being initialized with string literals.
    Code:
    char **cp[]={c+3,c+2,c+1,c};
    This is an array of pointers-to-pointers to characters, being initialized with values from the previous line.
    Code:
    char ***cpp=cp;
    This is a pointer to a pointer to a pointer to a character, pointing to the first element of the previous line's array.
    Code:
    void main()
    This is wrong.
    Code:
    printf("%s",**++cpp);
    This dereferences a pointer to a pointer to a character twice, and increments the pointer.
    Code:
    printf("%s",*--*++cpp+3);
    This should be undefined behavior, and as such, it doesn't matter what output you happen to get. If it's not undefined behavior, it's an ugly piece of code, and no one in their right mind would program like this, and that's what you should write as your answer.
    Code:
    printf("%s",*cpp[-2]+3);
    Using a negative array index tells me that you are doing something wrong, I have never had occasion to use a negative array index. Combine with the previous line, whatever it gives you is beyond my caring. (Yeah, I know, it's supposed to be tricky or cool, but it's crap. No one programs that way, or if they do, they shouldn't.)
    Code:
    printf("%s",cpp[-1][-1]+1);
    See above.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. double pointer to 2 dimensional array
    By siperi in forum C Programming
    Replies: 3
    Last Post: 12-07-2010, 11:33 AM
  2. Double pointer & Two Dimensional Array
    By vijay s in forum C Programming
    Replies: 2
    Last Post: 12-11-2009, 10:13 PM
  3. Pointer with Multi-dimensional Array
    By whichet in forum C Programming
    Replies: 7
    Last Post: 11-28-2007, 12:26 AM
  4. 2 dimensional pointer array to a struct
    By AdamLAN in forum C++ Programming
    Replies: 5
    Last Post: 05-22-2005, 05:38 PM
  5. any use for 4x pointer or higher?
    By skorman00 in forum C++ Programming
    Replies: 8
    Last Post: 11-22-2003, 04:59 AM