help me understand this char array please....

From: s.subbarayan (s_subbarayan_at_rediffmail.com)
Date: 02/23/05


Date: 22 Feb 2005 21:26:52 -0800

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



Relevant Pages

  • Re: Comment on trim string function please
    ... void TrimCString(char *str) ... isspaceexpects an int and not a char as it's argument. ...     char *p, ...
    (comp.lang.c)
  • Re: How to transfer an address value without using pointers
    ... void send; ... In main sprintf is used to convert the pointer value returned by ... the send function which accesses 's' as the local 'str'. ... to use an unsigned char array instead of a plain char one for 's.' ...
    (comp.lang.c)
  • Printf and output redirection.
    ... void sys_err(const char *str) ... int main ...
    (comp.os.linux.development.apps)
  • Duffs Device
    ... void duff2(const char *str, int len) { ... may be cheaper than n-=8 on some architectures. ...
    (comp.lang.c)
  • [PATCH 2.6.19-rc1 V9] drivers: add LCD support
    ... Adds support for the ks0108 LCD Controller as a device driver. ... +The buffer should be a 128*64 unsigned char array: ... * GNU General Public License for more details. ... +static void cfag12864b_setbit ...
    (Linux-Kernel)