Thread: I don't understand why the address-of operator is ignored

  1. #1
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332

    I don't understand why the address-of operator is ignored

    Code:
    #include <stdio.h>                                 
    #include <string.h>                                
                                                       
                                                       
    int main(void) {                                   
       char * ptr ;                                    
       char string0[16] ;                              
       char string1[16] ;                              
       char string2[16] = "THIS IS DATA" ;             
                                                       
       memcpy(&string0, &string2, sizeof(string2)-1) ; 
       printf("string0 = %s\n", string0 ) ;            
                                                       
       memcpy( string1,  string2, sizeof(string2)-1) ; 
       printf("string1 = %s\n", string1 ) ;            
       return 0 ;                                      
    }
    Why is the address-of operator ignored here?
    Mainframe assembler programmer by trade. C coder when I can.

  2. #2
    Registered User
    Join Date
    Sep 2022
    Posts
    55
    The address-of operator is not ignored at all. The name of an array denotes a pointer to its firest element. That makes that the address of the first element is the same as the address of the array. The type, however, is different (char* and char**, respectivly). It compiles since both types can be implicitly casted to a pointer to void that memcpy expects.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Why do you think it's being ignored?
    Code:
    $ cat foo.c
    #include <stdio.h>                                 
    #include <string.h>                                
                                                       
                                                       
    int main(void) {                                   
       char * ptr ;                                    
       char string0[16] ;                              
       char string1[16] ;                              
       char string2[16] = "THIS IS DATA" ;             
                                                       
       memcpy(&string0, &string2, sizeof(string2)-1) ; 
       printf("string0 = %s\n", string0 ) ;            
                                                       
       memcpy( string1,  string2, sizeof(string2)-1) ; 
       printf("string1 = %s\n", string1 ) ;            
       return 0 ;                                      
    }
    $ gcc foo.c
    $ ./a.out 
    string0 = THIS IS DATA
    string1 = THIS IS DATA
    A pointer to the whole array has the same value (but not the same type) as a pointer to the first element of the array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > The type, however, is different (char* and char**, respectivly).
    Not quite.

    A pointer to a whole array isn't a char**, but something like char (*)[16] - in this example.
    Code:
    #include <stdio.h>                                 
    int main(void) {                                   
       char string2[16] = "THIS IS DATA" ;            
       char (*s)[16] = &string2;
       char *t = string2;
       char **w = &string2;
       return 0 ;                                      
    }
    $ gcc foo.c
    foo.c: In function ‘main’:
    foo.c:6:15: warning: initialization of ‘char **’ from incompatible pointer type ‘char (*)[16]’ [-Wincompatible-pointer-types]
        6 |    char **w = &string2;
          |               ^
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I said it was "ignored" because the end result was the same. I wasn't aware that I could use &array. I've always just used array or &array[0]. Now I have a third option.

    I was looking at the type differences (char * and char **) and expecting an error when using the &.

    Thank you both for the lesson.
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Registered User
    Join Date
    Sep 2022
    Posts
    55
    FWIW For the sake of ease I've been not precise in both cases as I should have explained it as "array to pointer decay" when it gets passed to the parameter of memcpy.

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by Dino View Post
    I was looking at the type differences (char * and char **) and expecting an error when using the &.
    You didn't get an error (or even a warning) in this case because memcpy takes void*—it doesn't care nor check what pointer types you're passing to it.

    If you had a function that takes char* and you passed &string2 to it, then you could expect a warning or error.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-20-2020, 11:05 PM
  2. Address of (&) Operator and its use
    By Ali.B in forum C Programming
    Replies: 3
    Last Post: 03-10-2010, 11:43 AM
  3. How overload operator & (bitwise) versus & (address)?
    By 6tr6tr in forum C++ Programming
    Replies: 10
    Last Post: 04-24-2008, 02:17 PM
  4. I dont understand this operator
    By abhi_86 in forum C++ Programming
    Replies: 1
    Last Post: 08-21-2006, 06:08 AM
  5. Query regarding Overloading Address-of Operator
    By shiv_tech_quest in forum C++ Programming
    Replies: 20
    Last Post: 07-27-2006, 09:16 AM

Tags for this Thread