#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) display junk ? Since a1 points to a[0], shouldn'it it display the same values ?

int main()
{
     int i;
     char a[32],*a1,b[64],*b1;
     a1=&a[0];
     b1=&b[0];

     for (i = 0; i < 26; i++)
         a[i] = i + 65;
     a[26] = 0;

     printf ("address of a1 = %d\n", a1);
     printf ("address of a = %d\n", &a[0]);
     printf("contents of a1: %s\n",a1);
     printf ("Contents of a: ");
     for(i=0;i<=25;i++)
     {
          printf("%c",a[i]);
     }     
     printf ("\n\n");

     a1="hellohellohellohellohellohello" ;

     printf ("address of a1 = %d\n", a1);
     printf ("address of a = %d\n", &a[0]);
     printf("contents of a1: %s\n",a1);
     printf ("Contents of a: ");
     for(i=0;i<=25;i++)
     {
          printf("%c",a[i]);
     }     
     printf ("\n\n");

     return 0;
}

Line 22 changes the address of a1. After line 22 is executed, a1 is no longer pointing to a[0]. Thus making a1 hold a string does not make a hold that same string. You are getting garbage when you display a because a is never initialized in your program, unlike the program above. Line 22 doesn't initialize a since a1's address has changed. Run the program above to see what I mean. On the other hand, try changing line 22 above to:

strcpy (a1, "hellohellohellohellohellohello");

a1 will still point to a[0], so both will display the "hello" string.

int main()
{
     int i;
     char a[32],*a1,b[64],*b1;
     a1=&a[0];
     b1=&b[0];

     for (i = 0; i < 26; i++)
         a[i] = i + 65;
     a[26] = 0;

     printf ("address of a1 = %d\n", a1);
     printf ("address of a = %d\n", &a[0]);
     printf("contents of a1: %s\n",a1);
     printf ("Contents of a: ");
     for(i=0;i<=25;i++)
     {
          printf("%c",a[i]);
     }     
     printf ("\n\n");

     a1="hellohellohellohellohellohello" ;

     printf ("address of a1 = %d\n", a1);
     printf ("address of a = %d\n", &a[0]);
     printf("contents of a1: %s\n",a1);
     printf ("Contents of a: ");
     for(i=0;i<=25;i++)
     {
          printf("%c",a[i]);
     }     
     printf ("\n\n");

     return 0;
}

Line 22 changes the address of a1. After line 22 is executed, a1 is no longer pointing to a[0]. Thus making a1 hold a string does not make a hold that same string. You are getting garbage when you display a because a is never initialized in your program, unlike the program above. Line 22 doesn't initialize a since a1's address has changed. Run the program above to see what I mean. On the other hand, try changing line 22 above to:

strcpy (a1, "hellohellohellohellohellohello");

a1 will still point to a[0], so both will display the "hello" string.

Thanks, I figured the strcpy part after a little bit, but thanks for all your help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.