I don't understand this alias

This is a discussion on I don't understand this alias within the C Programming forums, part of the General Programming Boards category; Given the following: Code: #include <stdio.h> int main(void) { char x = 1; char y = 2; char *head, **tailp, ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195

    I don't understand this alias

    Given the following:

    Code:
    #include <stdio.h>
    
    int main(void) {
      char x = 1;
      char y = 2;
    
      char *head, **tailp, *new;
    
      head = &x;
      new = &y;
    
      tailp = &head;
    
      printf("THe value of **tailp: %d \n", **tailp);
      printf("The value of head: %x and &x: %x \n", head, &x);
    
      *tailp = NULL;
    
      printf("THe new value of head: %d \n", head);
      printf("THe value of *tailp is: %d \n", *tailp);
    
      return 0;
    }
    I don't see how *tailp is an alias for head when *tailp is NULL.

    Thanks

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    5,828
    tailp is a pointer to head (or it contains the address of head, &head). *tailp is therefore an alias of head (in C++ *tailp would be refered to as a reference to head). The operative bits are
    Code:
       tailp = &head;
      *tailp = NULL;
    The second line in this does not change tailp, but it does change the value at the address identified by tailp. Or, to put it another way, the second line is functionally equivalent to
    Code:
       *(&head) = NULL;
    or (simplified)
    Code:
        head = NULL;

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Okay,

    I think I see this. For some reason, I thought when I went

    *tailp = NULL;

    it went somewhere else. Like the following


    tailp ---->head
    |
    |------>NULL (when I go *tailp)

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    46
    Quote Originally Posted by grumpy
    Code:
       tailp = &head;
    can this be replaced by tailp=head?

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    5,828
    Quote Originally Posted by KidMan
    can this be replaced by tailp=head?
    Only if you want code that won't compile due to a mismatch of types.

    head is a pointer to char (char *). tailp is a pointer to pointer to char (char **) meaning it can hold the address of a pointer to char. Implicit conversion of a char * to a char ** is not allowed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct Pointer Problems. (Warning: Long Post.)
    By Phoenix940 in forum C Programming
    Replies: 1
    Last Post: 11-30-2008, 09:04 PM
  2. command line alias remove for linux
    By iain in forum Tech Board
    Replies: 6
    Last Post: 11-22-2004, 12:39 PM
  3. signiture and alias pic
    By Klinerr1 in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-05-2002, 01:52 PM
  4. Alias...
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-01-2001, 04:25 PM
  5. how to set a function to an alias in C shell
    By xstone in forum Tech Board
    Replies: 1
    Last Post: 09-29-2001, 03:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21