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 :
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
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
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