I need some basic C++ help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Psykarrd

    I need some basic C++ help

    I am trying to declare a string variable as an array of char's. the
    code looks like this.

    char name[80];

    then when i try to use the variable it dosn't work, however i am not
    sure you can use it the way i am trying to. Could some one please tell
    me what i am doing wrong, or another way of doing the same thing. i am
    trying to use the variable like this.

    name = "modem";


    thanks for any help you can give me.
  • Dave

    #2
    Re: I need some basic C++ help


    "Psykarrd" <psykarrd@hotma il.com> wrote in message
    news:19b61fcf.0 311191601.523ee 811@posting.goo gle.com...[color=blue]
    > I am trying to declare a string variable as an array of char's. the
    > code looks like this.
    >
    > char name[80];
    >
    > then when i try to use the variable it dosn't work, however i am not
    > sure you can use it the way i am trying to. Could some one please tell
    > me what i am doing wrong, or another way of doing the same thing. i am
    > trying to use the variable like this.
    >
    > name = "modem";
    >
    >
    > thanks for any help you can give me.[/color]

    strcpy(name, "modem");

    An expression such as "modem" is viewed by the compiler as a const char *
    (which points to an address in memory where the string literal "modem" is
    stored).

    So, the expression you gave was trying to assign an address to array. As
    such an operation does not make sense to do, your compiler yelped!


    Comment

    • Andrey Tarasevich

      #3
      Re: I need some basic C++ help

      Psykarrd wrote:
      [color=blue]
      > I am trying to declare a string variable as an array of char's. the
      > code looks like this.
      >
      > char name[80];
      >
      > then when i try to use the variable it dosn't work, however i am not
      > sure you can use it the way i am trying to. Could some one please tell
      > me what i am doing wrong, or another way of doing the same thing. i am
      > trying to use the variable like this.
      >
      > name = "modem";
      > ...[/color]

      Both 'name' and "modems" are _arrays_. Arrays in C++ are not assignable.
      In order to copy one array to another in general case you have to copy
      them "manually" - element by element. The good news is that in most
      cases there's a library function that can do it for you.

      In this particular case you can use 'strcpy':

      strcpy(name, modem);

      --
      Best regards,
      Andrey Tarasevich

      Comment

      • Dave

        #4
        Re: I need some basic C++ help


        "Dave" <better_cs_now@ yahoo.com> wrote in message
        news:vro2b2p7j1 e1d6@news.super news.com...[color=blue]
        >
        > "Psykarrd" <psykarrd@hotma il.com> wrote in message
        > news:19b61fcf.0 311191601.523ee 811@posting.goo gle.com...[color=green]
        > > I am trying to declare a string variable as an array of char's. the
        > > code looks like this.
        > >
        > > char name[80];
        > >
        > > then when i try to use the variable it dosn't work, however i am not
        > > sure you can use it the way i am trying to. Could some one please tell
        > > me what i am doing wrong, or another way of doing the same thing. i am
        > > trying to use the variable like this.
        > >
        > > name = "modem";
        > >
        > >
        > > thanks for any help you can give me.[/color]
        >
        > strcpy(name, "modem");
        >
        > An expression such as "modem" is viewed by the compiler as a const char *
        > (which points to an address in memory where the string literal "modem" is
        > stored).
        >
        > So, the expression you gave was trying to assign an address to array. As
        > such an operation does not make sense to do, your compiler yelped!
        >
        >[/color]

        Oops, sorry, I should have said "array of const char" instead of "const char
        *". So, it's really an array, and *then* that array undergoes a conversion
        to a pointer when the compiler encounters "modem". Finally then, as I said
        before, a pointer cannot be assigned to an array...


        Comment

        • Mike Wahler

          #5
          Re: I need some basic C++ help


          "Psykarrd" <psykarrd@hotma il.com> wrote in message
          news:19b61fcf.0 311191601.523ee 811@posting.goo gle.com...[color=blue]
          > I am trying to declare a string variable as an array of char's. the
          > code looks like this.
          >
          > char name[80];
          >
          > then when i try to use the variable it dosn't work, however i am not
          > sure you can use it the way i am trying to. Could some one please tell
          > me what i am doing wrong, or another way of doing the same thing. i am
          > trying to use the variable like this.
          >
          > name = "modem";[/color]

          Arrays are not assignable.

          strcpy(name, "modem"); /* 'strcpy()' is declared by <cstring>
          or <string.h>

          Better yet, use the std::string type, which *is* assignable,
          and also has the advantage of not being restricted to a fixed
          size like an array:

          std::string name; /* 'std::string' declared by <string> */
          name = "modem";

          Or if "modem" is to be the inital value:

          std::string name("modem");

          -Mike


          Comment

          • John Carson

            #6
            Re: I need some basic C++ help

            "Dave" <better_cs_now@ yahoo.com> wrote in message
            news:vro33uncnb 3kc8@news.super news.com[color=blue]
            >
            > Oops, sorry, I should have said "array of const char" instead of
            > "const char
            > *". So, it's really an array, and *then* that array undergoes a
            > conversion to a pointer when the compiler encounters "modem".
            > Finally then, as I said before, a pointer cannot be assigned to an
            > array...[/color]

            It is not the chars that are const --- if they were you couldn't change the
            char values. Rather, it is the location in memory of the chars that cannot
            be changed. In pointer terms, it is like a const pointer to char, i.e.,
            char * const


            --
            John Carson
            1. To reply to email address, remove donald
            2. Don't reply to email address (post here instead)

            Comment

            • Dave

              #7
              Re: I need some basic C++ help


              "John Carson" <donaldquixote@ datafast.net.au > wrote in message
              news:3fbc1cc9@u senet.per.parad ox.net.au...[color=blue]
              > "Dave" <better_cs_now@ yahoo.com> wrote in message
              > news:vro33uncnb 3kc8@news.super news.com[color=green]
              > >
              > > Oops, sorry, I should have said "array of const char" instead of
              > > "const char
              > > *". So, it's really an array, and *then* that array undergoes a
              > > conversion to a pointer when the compiler encounters "modem".
              > > Finally then, as I said before, a pointer cannot be assigned to an
              > > array...[/color]
              >
              > It is not the chars that are const --- if they were you couldn't change[/color]
              the[color=blue]
              > char values. Rather, it is the location in memory of the chars that cannot
              > be changed. In pointer terms, it is like a const pointer to char, i.e.,
              > char * const
              >
              >
              > --
              > John Carson
              > 1. To reply to email address, remove donald
              > 2. Don't reply to email address (post here instead)
              >[/color]

              2.13.4-1: An ordinary string literal has type "array of n const char"

              And indeed, you may not change the characters of a string literal.


              Comment

              • jbruno4000

                #8
                Re: I need some basic C++ help

                Looking at the way you're wanting to use the variable, perhaps you'll be better
                off using a string variable instead of char. i.e.
                #include <string>. You'll them be able to do what you seem to have in mind.

                string name;

                name = "modem";

                and you can still access the individual components of name:

                for(int count = 0; count < strlen(name); count++)
                cout << name[count] << "," ;
                cout << endl;

                outputs: m, o, d, e, m,

                Comment

                • rajanikanth

                  #9
                  Re: I need some basic C++ help

                  psykarrd@hotmai l.com (Psykarrd) wrote in message news:<19b61fcf. 0311191601.523e e811@posting.go ogle.com>...[color=blue]
                  > I am trying to declare a string variable as an array of char's. the
                  > code looks like this.
                  >
                  > char name[80];
                  >
                  > then when i try to use the variable it dosn't work, however i am not
                  > sure you can use it the way i am trying to. Could some one please tell
                  > me what i am doing wrong, or another way of doing the same thing. i am
                  > trying to use the variable like this.
                  >
                  > name = "modem";
                  >
                  >
                  > thanks for any help you can give me.[/color]

                  hi!

                  It is better to use the string class

                  #include<string >

                  string s = "modem";
                  cout << s << "\n";

                  Best,

                  Raj

                  Comment

                  • Andrey Tarasevich

                    #10
                    Re: I need some basic C++ help

                    John Carson wrote:[color=blue]
                    > "Dave" <better_cs_now@ yahoo.com> wrote in message
                    > news:vro33uncnb 3kc8@news.super news.com[color=green]
                    >>
                    >> Oops, sorry, I should have said "array of const char" instead of
                    >> "const char
                    >> *". So, it's really an array, and *then* that array undergoes a
                    >> conversion to a pointer when the compiler encounters "modem".
                    >> Finally then, as I said before, a pointer cannot be assigned to an
                    >> array...[/color]
                    >
                    > It is not the chars that are const --- if they were you couldn't change the
                    > char values. Rather, it is the location in memory of the chars that cannot
                    > be changed. In pointer terms, it is like a const pointer to char, i.e.,
                    > char * const[/color]

                    I think Dave was talking about the literal. String literal is an "array
                    of const char" and its 'char' values cannot be changed.

                    --
                    Best regards,
                    Andrey Tarasevich

                    Comment

                    • John Carson

                      #11
                      Re: I need some basic C++ help

                      "Andrey Tarasevich" <andreytarasevi ch@hotmail.com> wrote in message
                      news:vrobpatfvp l799@news.super news.com[color=blue]
                      > John Carson wrote:[color=green]
                      > > "Dave" <better_cs_now@ yahoo.com> wrote in message
                      > > news:vro33uncnb 3kc8@news.super news.com[color=darkred]
                      > > >
                      > > > Oops, sorry, I should have said "array of const char" instead of
                      > > > "const char
                      > > > *". So, it's really an array, and *then* that array undergoes a
                      > > > conversion to a pointer when the compiler encounters "modem".
                      > > > Finally then, as I said before, a pointer cannot be assigned to an
                      > > > array...[/color]
                      > >
                      > > It is not the chars that are const --- if they were you couldn't
                      > > change the char values. Rather, it is the location in memory of the
                      > > chars that cannot be changed. In pointer terms, it is like a const
                      > > pointer to char, i.e., char * const[/color]
                      >
                      > I think Dave was talking about the literal. String literal is an
                      > "array
                      > of const char" and its 'char' values cannot be changed.
                      >[/color]


                      You are right. I didn't read the post carefully enough.


                      --
                      John Carson
                      1. To reply to email address, remove donald
                      2. Don't reply to email address (post here instead)

                      Comment

                      • John Carson

                        #12
                        Re: I need some basic C++ help

                        "Dave" <better_cs_now@ yahoo.com> wrote in message
                        news:vro7qiktrn s332@news.super news.com[color=blue]
                        > "John Carson" <donaldquixote@ datafast.net.au > wrote in message
                        > news:3fbc1cc9@u senet.per.parad ox.net.au...[color=green]
                        > > "Dave" <better_cs_now@ yahoo.com> wrote in message
                        > > news:vro33uncnb 3kc8@news.super news.com[color=darkred]
                        > > >
                        > > > Oops, sorry, I should have said "array of const char" instead of
                        > > > "const char
                        > > > *". So, it's really an array, and *then* that array undergoes a
                        > > > conversion to a pointer when the compiler encounters "modem".
                        > > > Finally then, as I said before, a pointer cannot be assigned to an
                        > > > array...[/color]
                        > >
                        > > It is not the chars that are const --- if they were you couldn't
                        > > change the char values. Rather, it is the location in memory of the
                        > > chars that cannot be changed. In pointer terms, it is like a const
                        > > pointer to char, i.e., char * const
                        > >
                        > >
                        > > --
                        > > John Carson
                        > > 1. To reply to email address, remove donald
                        > > 2. Don't reply to email address (post here instead)
                        > >[/color]
                        >
                        > 2.13.4-1: An ordinary string literal has type "array of n const char"
                        >
                        > And indeed, you may not change the characters of a string literal.[/color]


                        My mistake. I misread your post as referring to the array

                        char name[80];

                        rather than to the string literal.


                        --
                        John Carson
                        1. To reply to email address, remove donald
                        2. Don't reply to email address (post here instead)

                        Comment

                        • Dave

                          #13
                          Re: I need some basic C++ help


                          "John Carson" <donaldquixote@ datafast.net.au > wrote in message
                          news:3fbc3379$1 @usenet.per.par adox.net.au...[color=blue]
                          > "Andrey Tarasevich" <andreytarasevi ch@hotmail.com> wrote in message
                          > news:vrobpatfvp l799@news.super news.com[color=green]
                          > > John Carson wrote:[color=darkred]
                          > > > "Dave" <better_cs_now@ yahoo.com> wrote in message
                          > > > news:vro33uncnb 3kc8@news.super news.com
                          > > > >
                          > > > > Oops, sorry, I should have said "array of const char" instead of
                          > > > > "const char
                          > > > > *". So, it's really an array, and *then* that array undergoes a
                          > > > > conversion to a pointer when the compiler encounters "modem".
                          > > > > Finally then, as I said before, a pointer cannot be assigned to an
                          > > > > array...
                          > > >
                          > > > It is not the chars that are const --- if they were you couldn't
                          > > > change the char values. Rather, it is the location in memory of the
                          > > > chars that cannot be changed. In pointer terms, it is like a const
                          > > > pointer to char, i.e., char * const[/color]
                          > >
                          > > I think Dave was talking about the literal. String literal is an
                          > > "array
                          > > of const char" and its 'char' values cannot be changed.
                          > >[/color]
                          >
                          >
                          > You are right. I didn't read the post carefully enough.[/color]

                          Although I think we've long since answered the OPs question, I'm now
                          starting to wonder about the accuracy of another part of my post.
                          Specifically, I'm wondering about where I stated the array is converted to a
                          pointer. Though such a conversion is one of the implicit conversions
                          described by the Standard, *does* the conversion happen here? Would there
                          be any reason for it to? After all, it's not as if we're trying to match
                          the array to a pointer parameter of a function, or some other such case
                          where a conversion is *needed*. Here, the assignment of an array (a string
                          literal) to another array is illegal to start with, so why would a
                          conversion happen only to come up with another construct (the assignment of
                          a pointer to an array) that is still just as illegal? So, I may have been
                          incorrect in that portion of my post. Can anybody confirm or deny this
                          authoritatively ? I'd like to know for sure now that I've thought about it a
                          little longer...


                          Comment

                          • Ken Stauffer

                            #14
                            Re: I need some basic C++ help

                            main returns an 'int', not void



                            "Psykarrd" <psykarrd@hotma il.com> wrote in message news:19b61fcf.0 311191601.523ee 811@posting.goo gle.com...[color=blue]
                            > I am trying to declare a string variable as an array of char's. the
                            > code looks like this.
                            >
                            > char name[80];
                            >
                            > then when i try to use the variable it dosn't work, however i am not
                            > sure you can use it the way i am trying to. Could some one please tell
                            > me what i am doing wrong, or another way of doing the same thing. i am
                            > trying to use the variable like this.
                            >
                            > name = "modem";
                            >
                            >
                            > thanks for any help you can give me.[/color]


                            Comment

                            • apple

                              #15
                              Re: I need some basic C++ help

                              strcpy(name, "modem");

                              Comment

                              Working...