Thread: Pointer to an array : string

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jun 2018
    Posts
    26

    Pointer to an array : string

    Hello,

    I only have intermediate knowledge in c.
    I'm learning some advanced topics about pointers in c,

    Below is the program that confuses me
    Code:
    #include<stdio.h>
    
    int main(void){
    
        //section 1
    
        char str1[]="Hello";
        printf("%d, %d, %d, %s\n",&str1,&str1[0], str1, str1);
        printf("\n");
        
        //section 2
    
        char*str2;
        str2 ="Goodbye";
        printf("%d, %d, %s\n",&str2, str2, str2);
        printf("\n");
    
        //section 3
    
        char*string[3];
    
        string[0]="zero";
        string[1]="one";
        string[2]="two";
    
        printf("%d, %d, %d, %d, %s\n",&string, string,&string[0], string[0], string[0]);
        printf("%d, %d, %d, %d, %s\n",&string, string,&string[1], string[1], string[1]);
        printf("%d, %d, %d, %d, %s\n",&string, string,&string[2], string[2], string[2]);
        printf("\n");
        system("pause");
    }
    OUTPUT : Pointer to an array : string-c2-png

    In section 1

    Code:
    char str1[]="Hello";
        printf("%d, %d, %d, %s\n",&str1,&str1[0], str1, str1);
        printf("\n");
    //output : 15989136, 15989136, 15989133, Hello
    Pointer to an array : string-c3-jpg

    I know address of an array is the address of first element of that array, which is same as the array name

    In section 2

    Code:
    char*str2;
        str2 ="Goodbye";
        printf("%d, %d, %s\n",&str2, str2, str2);
        printf("\n");
    //output : 15989124, 2390488, Goodbye
    Pointer to an array : string-c-jpg

    Here str2 is a pointer, which is stored at some place in memory
    and &str2 will display that address. Value of str2 is the address at which the string "Goodbye" is stored

    I understand up to this part. But third section confuses me,

    Code:
    char*string[3];
    
        string[0]="zero";
        string[1]="one";
        string[2]="two";
    
        printf("%d, %d, %d, %d, %s\n",&string, string,&string[0], string[0], string[0]);
        printf("%d, %d, %d, %d, %s\n",&string, string,&string[1], string[1], string[1]);
        printf("%d, %d, %d, %d, %s\n",&string, string,&string[2], string[2], string[2]);
        printf("\n");
    
    //output:
    //15989104, 15989104, 15989104, 2390852, zero
    //15989104, 15989104, 15989108, 2391020, one
    //15989104, 15989104, 15989112, 2390840, two
    
    
    which shows the address of zero, one, two??
    What each value in output represents??


    Thanks
    Last edited by Athul; 06-25-2018 at 10:19 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of pointer to string
    By lovelycse in forum C Programming
    Replies: 4
    Last Post: 08-31-2012, 06:55 AM
  2. is a string in C = pointer = array?
    By winggx in forum C Programming
    Replies: 1
    Last Post: 03-21-2010, 11:54 PM
  3. qsort() in Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 8
    Last Post: 06-16-2007, 04:18 PM
  4. Pointer to array of string and Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-15-2007, 06:04 AM
  5. string to pointer array
    By pankleks in forum C Programming
    Replies: 7
    Last Post: 02-09-2005, 06:45 AM

Tags for this Thread