Thread: double pointer

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Two other examples to add to Salem's answer... Let's say I want to make a function which closes a file, using a stream, and want to nullify this "ponter" after closing:
    Code:
    void closefile( FILE **fptr )
    {
      if ( *fptr ) fclose( *fptr );
      *fptr = NULL;
    }
    The other is where you want an array of strings. A string, in C, is an array of chars where a pointer points to the first char, Like this:
    Code:
    char *str = "fred";
    Here "fred" (and the final NUL char) is allocated somewhere in memory and the address to the first element is assigned to str. But what if you want an array of strings?
    Code:
    char *strs[] = { "fred", "was", "here", NULL };
    strs points to the first pointer, which points to "fred"... We can use a pointer to a pointer of char to iterate through this array:
    Code:
    char **p = strs;
    while ( *p )
      puts( *p++ );
    []s
    Fred
    Last edited by flp1969; 01-11-2023 at 04:05 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-01-2015, 12:39 AM
  2. Double liked list and double pointer
    By saillesh.sabari in forum C Programming
    Replies: 1
    Last Post: 12-10-2010, 11:03 AM
  3. Replies: 4
    Last Post: 10-31-2009, 07:18 PM
  4. Replies: 3
    Last Post: 10-30-2009, 04:41 PM
  5. Replies: 5
    Last Post: 04-04-2009, 03:45 AM

Tags for this Thread