Thread: pointer access

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    1

    pointer access

    hello can someone tells me how did we access the values using :
    buffer[i]
    as i see from the code it is a pointer an we should use * to accses the value inside the address

    code from :

    How to copy complete structure in a byte array (character buffer) in C language? - IncludeHelp
    Code:
    
    
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct Person
    {
        char name[20];
        int age;
    };
    
    int main()
    {
        //structure variable declaratio with initialisation
        struct Person person={"Deniss Ritchie", 60};
        //declare character buffer (byte array)
        unsigned char *buffer=(char*)malloc(sizeof(person));
        int i;
        
        //copying....
        memcpy(buffer,(const unsigned char*)&person,sizeof(person));
        
        //printing..
        printf("Copied byte array is:\n");
        for(i=0;i<sizeof(person);i++)
            printf("%02X",buffer[i]);
        printf("\n");
        
        //freeing memory..
        free(buffer);
        return 0;
    }
    

    Output
    Copied byte array is:
    44 65 6E 69 73 73 20 52 69 74 63 68 69 65 00 00 00 00 00 00 3C 00 00 00

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    692
    Array indexing notation is just "syntactic sugar" for pointer operations.
    Code:
    #include <stdio.h>
     
    int main() {
        int a[10], i = 5;
         
        a[i] = 1;       // array notation       | THESE DO THE
        *(a + i) = 1;   // pointer notation     | EXACT SAME THING
         
        // here's something strange:
        *(i + a) = 1;   // same thing, just switched a and i around
        i[a] = 1;       // What??? Yet legal!
    }
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-06-2018, 11:22 AM
  2. far pointer to access memory contents
    By pole1080 in forum C Programming
    Replies: 5
    Last Post: 06-23-2010, 03:06 PM
  3. Access via iterator to pointer container
    By atlmallu in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2009, 01:13 PM
  4. Using pointer to access list element
    By GOBLIN-85 in forum C++ Programming
    Replies: 3
    Last Post: 03-03-2008, 04:15 AM
  5. NULL Pointer Access?
    By Dirty Sanchez in forum C Programming
    Replies: 4
    Last Post: 01-19-2004, 12:17 PM

Tags for this Thread