basic char vs string question

#include<stdio.h>
#include<stdlib.h>

main()
{
char a[32],*a1,b[64],*b1;
a1=&a[0];
b1=&b[0];
a1="hellohellohellohellohellohello" ;
printf("\n contents of a1: %s\n",a1);

int i =0;
for(i=0;i<=31;i++)
{
printf("%c",a[i]);
}

}

So why does printf ("%s",a1) work correctly while printf("%c",a[i]) display junk ? Since a1 points to a[0], shouldn'it it display the same values ?
So why does printf ("%s",a1) work correctly while printf("%c",a[i]) display junk ? Since a1 points to a[0], shouldn'it it display the same values ?


NO.
Have a close look at your code again......
I printed out the address values of &a[0] and a1 and they match initially. but after a1 gets the "hello...." value, it changes its address value and points elsewhere.
Not sure I get it.
strcpy(a,a1)
did it ;)
Thanks anyways.
Topic archived. No new replies allowed.