Thread: Pointer to pointer question

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    Pointer to pointer question

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	char *text[] = {"foo", "bar"};
    
    	printf("%c\n", (*text)[0]);
    
    	return 0;
    }
    This will display the character f. Now how would I display the first character of "the second" array of characters? I tried this but the program won't compile
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	char *text[] = {"foo", "bar"};
    
    	printf("%c\n", (*++text)[0]);
    
    	return 0;
    }
    Thanks.

  2. #2
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    printf( "%c\n", ( *(text+1) )[0] );

    or

    printf("%c\n", text[1][0]);

  3. #3
    Watch for flying houses. Nessarose's Avatar
    Join Date
    Sep 2004
    Posts
    46
    Like this?

    Code:
    printf("%c\n", (*text)[4]);

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> printf("%c\n", (*text)[4]);
    That's a bad assumption to make. There's no guarantee that those string literals will be placed back-to-back in memory.

    To expand on Antigloss's examples:
    Code:
    printf("%c\n", **(text+1));
    printf("%c\n", *text[1]);
    gg

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    The reason you can't do ++text is because text is an array. Likewise, you can't do this:
    Code:
    {
      int a[5];
    
      a++;
    }
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Codeplug
    >> printf("%c\n", (*text)[4]);
    That's a bad assumption to make. There's no guarantee that those string literals will be placed back-to-back in memory.
    As a matter of fact, there is such a guarantee. This is initialised as an array, and the array elements (the strings, in this case) are guaranteed to be contiguous in memory.

    However, a function that receives a raw pointer to pointer to char is not allowed to assume this. For example;
    Code:
    /*  #include appropriate standard headers */
    void f(char **x)
    {
          printf("%c\n", (*x) + 4);
          printf("%c\n", x[1][0]);
    }
    
    int main()
    {
         char *text[] = {"foo", "bar"};
         char **text2;
    
        /*  If your compiler chokes on the following three lines, it is a C++ compiler.  A vanilla C compiler will not choke */
    
         text2 = malloc(2*sizeof(char *));
         text2[0] = malloc(4);
         text2[1] = malloc(4);
         strcpy(text2[0], "foo");
         strcpy(text2[1], "bar");
         
         f(text);
         f(text2);
         return 0;      
    }
    The problem, for function f(), is that it cannot assume that the pointer to pointer it is given actually an array of strings, or that elements of that array are contiguous. The first printf() line will generate undefined behaviour when called with f(text2) in the above.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by grumpy
    As a matter of fact, there is such a guarantee. This is initialised as an array, and the array elements (the strings, in this case) are guaranteed to be contiguous in memory.
    The array is of pointers.
    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.*

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    As a matter of fact, there is such a guarantee. This is initialised as an array, and the array elements (the strings, in this case) are guaranteed to be contiguous in memory.
    Not the strings theselves, but their memory addresses. Which means that if the strings were at address 0x1234 and 0x5678 then those 2 values (the addresses) would be contiguous in memory for your array. But you can see that the strings themselves don't need to be right next to each other.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 10:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  4. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 02:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM