help me understand this char array please....

Discussion in 'C Programming' started by s.subbarayan, Feb 23, 2005.

  1. s.subbarayan

    s.subbarayan Guest

    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
    s.subbarayan, Feb 23, 2005
    #1
    1. Advertising

  2. s.subbarayan

    Mac Guest

    On Tue, 22 Feb 2005 21:26:52 -0800, s.subbarayan wrote:

    > Dear all,
    > I encountered the following piece of program:
    > #include <stdio.h>
    > void strprint(char *str);


    This is a bad name for a function, since it starts with str. Call it
    my_strprint, or something.

    > void main()


    How about:
    int main(int argc, char** argv)

    main always returns an int.
    > {
    > char *spr="hello";
    > strprint(spr);
    >

    return 0;
    > }
    > void strprint(char *str)
    > {
    > char *ptr="hai";
    > str=ptr;
    > printf("%s",str);
    > }
    >


    This function makes no sense. The value of the variable str is completely
    ignored by the function and doesn't influence the functions behavior at
    all.

    > This program when compiled with MSVC++ compiler prints the output as
    > "hai".


    The compiler is generally irrelevant in this newsgroup, as long as it is a
    C compiler.
    >
    > I have a few doubts regarding this program:
    >
    > 1)Whats the statement str=ptr meaning?
    >

    It means assign the value of ptr to str. What did you think it meant?

    > 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?


    The assignment operator (=) does not do what you think. It just takes the
    value of ptr, and stores it in str. That value happens to be a pointer to
    a char which happens to be the first character in a c string.

    >
    > 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].


    No. You can say that 'a' is the same as '&(a[0])', but it is NOT the same
    as a[0]. The type of 'a' is "array of 10 chars" and the type of a[0] is
    "char".

    > Having this inference,I suppose
    > this statement str=ptr is supposed to copy only the first character.
    >


    No. I've already explained this above.

    > 4)But I am not getting how it has copied entire array into another
    > array with this statement?
    >


    No array was copied. The variables str and ptr are pointer types, which
    means they hold addresses. When you say str=ptr, you are throwing away the
    address currently stored in str and replacing it with the address stored
    in ptr.

    > 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.
    >


    As I said above, 'a' has type "array of 10 chars," and 'a[0]' has type
    "char".

    > 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


    Your knowledge of C is too basic for this group. You should read at least
    one C book before posting questions here.

    Please do not post any questions until you have a rudimentary
    understanding of the basics of the language, or at least have carefully
    read over the FAQ list for this newsgroup:

    http://docs.mandragor.org/files/Programming_languages/C/clc_faq_en/C-faq/top.html

    --Mac
    Mac, Feb 23, 2005
    #2
    1. Advertising

  3. s.subbarayan

    infobahn Guest

    Mac wrote:
    >

    <snip Yet Another Clueless Program, + Mac's response>

    > Your knowledge of C is too basic for this group.


    I think I have to disagree with this.

    > You should read at least
    > one C book before posting questions here.


    But this is certainly wise advice. He would do better to learn by
    other people's mistakes than to make all the same mistakes for
    himself.
    infobahn, Feb 23, 2005
    #3
  4. On 22 Feb 2005 21:26:52 -0800, in comp.lang.c ,
    (s.subbarayan) wrote:

    > char *ptr="hai";


    this line initialises ptr to point to "hai".

    > str=ptr;


    this line throws away the value of str (a pointer to "hello") and replaces
    it with the value of ptr (a pointer to "hai"). Note its does NOT copy
    anything. It assigns the value of ptr to str. Not what it points to, its
    value.

    > printf("%s",str);


    so this line prints out "hai".

    This is a very stupid programme, presumably intended to teach you
    something. I've no idea what - possibly that the = operator doesn't copy
    things, possibly that C's char pointers are not the same as C++ strings.
    Possibly nothing at all.

    >4)But I am not getting how it has copied entire array into another
    >array with this statement?


    in the above code, you do not have any arrays.

    char* str = "hello";
    creates a pointer to a string literal. To create an array you would use
    char str[6] = "hello";


    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    Mark McIntyre, Feb 23, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KK
    Replies:
    2
    Views:
    517
    Big Brian
    Oct 14, 2003
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,007
    Ian Collins
    May 9, 2006
  3. Replies:
    3
    Views:
    720
  4. davidb
    Replies:
    0
    Views:
    752
    davidb
    Sep 1, 2006
  5. davidb
    Replies:
    6
    Views:
    1,533
    Default User
    Sep 1, 2006
Loading...

Share This Page