S
s.subbarayan
Dear all,
I encountered the following piece of program:
#include <stdio.h>
void strprint(char *str);
void main()
{
char *spr="hello";
strprint(spr);
}
void strprint(char *str)
{
char *ptr="hai";
str=ptr;
printf("%s",str);
}
This program when compiled with MSVC++ compiler prints the output as
"hai".
I have a few doubts regarding this program:
1)Whats the statement str=ptr meaning?
2)In the code above,str contains 5 chars ("hello" is passed from
main),and ptr is getting copied to it which has 3 chars("hai"),now
after copying should str not be actually "hailo"?Because only first 3
chars should be replaced with ptr leaving the rest 2 chars?Can someone
explain me why its not so in the output?
3)AFAIK,when we talk about char arrays,generally the name of the array
means the value of first element in array.i.e,suppose i declare char
a[10],when i say a,it refers to a[0].Having this inference,I suppose
this statement str=ptr is supposed to copy only the first character.
4)But I am not getting how it has copied entire array into another
array with this statement?
5)How exactly in a char array,char a[10], 'a' differ from a[0]?Why I
am asking this is,I am under the impression that both are same with
respect to char arrays.But my colleague argues thats not the case.
It will be helpful if some one throws some light on this.
Looking farward to all your replys and advanced thanks for the same,
Regards,
s.subbarayan
I encountered the following piece of program:
#include <stdio.h>
void strprint(char *str);
void main()
{
char *spr="hello";
strprint(spr);
}
void strprint(char *str)
{
char *ptr="hai";
str=ptr;
printf("%s",str);
}
This program when compiled with MSVC++ compiler prints the output as
"hai".
I have a few doubts regarding this program:
1)Whats the statement str=ptr meaning?
2)In the code above,str contains 5 chars ("hello" is passed from
main),and ptr is getting copied to it which has 3 chars("hai"),now
after copying should str not be actually "hailo"?Because only first 3
chars should be replaced with ptr leaving the rest 2 chars?Can someone
explain me why its not so in the output?
3)AFAIK,when we talk about char arrays,generally the name of the array
means the value of first element in array.i.e,suppose i declare char
a[10],when i say a,it refers to a[0].Having this inference,I suppose
this statement str=ptr is supposed to copy only the first character.
4)But I am not getting how it has copied entire array into another
array with this statement?
5)How exactly in a char array,char a[10], 'a' differ from a[0]?Why I
am asking this is,I am under the impression that both are same with
respect to char arrays.But my colleague argues thats not the case.
It will be helpful if some one throws some light on this.
Looking farward to all your replys and advanced thanks for the same,
Regards,
s.subbarayan