Sunday, December 4, 2011

Look at a String literals



#include <stdio.h>
#include <string.h>
char *ap1="char *ap1";
static char *ap2="static char *ap2";
int ii;
main()
{
int i;
char a[] ="char a[]";
char a1[] ="char a1[]";
static char a2[32] ="static char a2[10] ";
const char a3[32] ="const char a3[10] ";
char *ap ="char *ap";
static char *aptr ="static *aptr";

printf("strlen a[] %d \n",strlen(a));
printf("sizeof a[] %d \n",sizeof(a));
printf("a[sizeof] %d \n",a[strlen(a)]);

char help[20]="a b c d e ";

printf("local:\n&i %p %d \n",&i,i);
printf("a1 %p %s \n",a1,a1);
printf("a2 %p %s \n",a2,a2);
printf("a3 %p %s \n",a3,a3);
printf("*ap %p %s \n",ap,ap);
printf("*aptr %p %s \n",aptr,aptr);
printf("global:\n*ap1 %p %s \n",ap1,ap1);
printf("*ap2 %p %s \n",ap2,ap2);
printf("&ii %p %s \n",&ii,ii);

}

[output]: ./a.out

strlen a[] 8
sizeof a[] 9
a[sizeof] 0
local:
&i 0xbff103f8 -1074723816 < stack
a1 0xbff103e5 char a1[]
a2 0x8049940 static char a2[10]
a3 0xbff103c5 const char a3[10]
*ap 0x80486d8 char *ap
*aptr 0x80486cb static *aptr
global:
*ap1 0x80486b0 char *ap1
*ap2 0x80486ba static char *ap2
&ii 0x8049968 (null)

NULL in stdio is ((void*)0)