A problem about char pointer ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • comp.lang.c++

    A problem about char pointer ?

    this is a sample example about this question
    #include<stdio. h>
    void chg(char* t)
    {
    char *s=t;
    char p=*t;
    while(*t++=*++s );
    *--t=p;
    }
    int main()
    {
    //char * t="abcde";
    char t[]="abcde";
    chg(t);
    printf(t);
    printf("\n");
    return 0;
    }
    I want know why the function chg() cann't work when define t with
    char*, and what's the different
    between char* and char[]?
    thanks?
  • Victor Bazarov

    #2
    Re: A problem about char pointer ?

    comp.lang.c++ wrote:
    this is a sample example about this question
    #include<stdio. h>
    void chg(char* t)
    {
    char *s=t;
    char p=*t;
    while(*t++=*++s );
    *--t=p;
    }
    int main()
    {
    //char * t="abcde";
    char t[]="abcde";
    chg(t);
    printf(t);
    printf("\n");
    return 0;
    }
    I want know why the function chg() cann't work when define t with
    char*, and what's the different
    between char* and char[]?
    The difference is relatively obscure, unfortunately. When you declare
    't' to be an array (char t[]), then each element of 't' is allowed to
    be modified, IOW it's OK to modify it. When you declare 't' as a mere
    pointer, and initialise it with a literal, then 't' points to the
    non-modifiable memory, and any attempt to change the value of elements
    of 't' has _undefined_beha viour_.
    thanks?
    Yes, please.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Jim Langston

      #3
      Re: A problem about char pointer ?

      Victor Bazarov wrote:
      comp.lang.c++ wrote:
      >this is a sample example about this question
      >#include<stdio .h>
      >void chg(char* t)
      >{
      > char *s=t;
      > char p=*t;
      > while(*t++=*++s );
      > *--t=p;
      >}
      >int main()
      >{
      > //char * t="abcde";
      > char t[]="abcde";
      > chg(t);
      > printf(t);
      > printf("\n");
      > return 0;
      >}
      >I want know why the function chg() cann't work when define t with
      >char*, and what's the different
      >between char* and char[]?
      >
      The difference is relatively obscure, unfortunately. When you declare
      't' to be an array (char t[]), then each element of 't' is allowed to
      be modified, IOW it's OK to modify it. When you declare 't' as a mere
      pointer, and initialise it with a literal, then 't' points to the
      non-modifiable memory, and any attempt to change the value of elements
      of 't' has _undefined_beha viour_.
      >
      >thanks?
      >
      Yes, please.
      Just to reiterate, the problem isn't, per se, that t is defined as a char*,
      but where t is pointing to.

      For example,

      char x[] = "abcde";
      char* t = x;
      chg(t);

      with your code would work. Because t is now not pointing to constant
      memory.

      --
      Jim Langston
      tazmaster@rocke tmail.com


      Comment

      • Paul Brettschneider

        #4
        Re: A problem about char pointer ?

        Victor Bazarov wrote:
        comp.lang.c++ wrote:
        >this is a sample example about this question
        >#include<stdio .h>
        >void chg(char* t)
        >{
        > char *s=t;
        > char p=*t;
        > while(*t++=*++s );
        > *--t=p;
        >}
        >int main()
        >{
        > //char * t="abcde";
        > char t[]="abcde";
        > chg(t);
        > printf(t);
        > printf("\n");
        > return 0;
        >}
        >I want know why the function chg() cann't work when define t with
        >char*, and what's the different
        >between char* and char[]?
        >
        The difference is relatively obscure, unfortunately. When you declare
        't' to be an array (char t[]), then each element of 't' is allowed to
        be modified, IOW it's OK to modify it. When you declare 't' as a mere
        pointer, and initialise it with a literal, then 't' points to the
        non-modifiable memory, and any attempt to change the value of elements
        of 't' has _undefined_beha viour_.
        Reminds me of my first paid coding job, which consisted of making a large
        code base containing myriads of goodies like
        char *s;
        [...]
        return strncpy(" ", s, 5);
        and lots of global variables reentrant, because it was to be used in a
        shared library.

        I don't miss those times. ;)

        Comment

        • comp.lang.c++

          #5
          Re: A problem about char pointer ?

          On Mar 11, 5:06 am, "Jim Langston" <tazmas...@rock etmail.comwrote :
          Victor Bazarov wrote:
          comp.lang.c++ wrote:
          this is a sample example about this question
          #include<stdio. h>
          void chg(char* t)
          {
          char *s=t;
          char p=*t;
          while(*t++=*++s );
          *--t=p;
          }
          int main()
          {
          //char * t="abcde";
          char t[]="abcde";
          chg(t);
          printf(t);
          printf("\n");
          return 0;
          }
          I want know why the function chg() cann't work when define t with
          char*, and what's the different
          between char* and char[]?
          >
          The difference is relatively obscure, unfortunately. When you declare
          't' to be an array (char t[]), then each element of 't' is allowed to
          be modified, IOW it's OK to modify it. When you declare 't' as a mere
          pointer, and initialise it with a literal, then 't' points to the
          non-modifiable memory, and any attempt to change the value of elements
          of 't' has _undefined_beha viour_.
          >
          thanks?
          >
          Yes, please.
          >
          Just to reiterate, the problem isn't, per se, that t is defined as a char*,
          but where t is pointing to.
          >
          For example,
          >
          char x[] = "abcde";
          char* t = x;
          chg(t);
          >
          with your code would work. Because t is now not pointing to constant
          memory.
          >
          --
          Jim Langston
          tazmas...@rocke tmail.com
          thanks ,I think I konw .Because "abcde" is a constant ,t just point to
          it.and so t should be a constant pointer in fact.and char t[]="abcde"
          is declare a array and give it's elements vuales by "abcde" , and t
          is point to the first element's address.and I want to know how to
          judge if the pointer is point to a constant in my function ? thanks!

          Comment

          • Triple-DES

            #6
            Re: A problem about char pointer ?

            On 11 Mar, 02:27, "comp.lang. c++" <cnhenu...@gmai l.comwrote:
            thanks ,I think I konw .Because "abcde" is a constant ,t just point to
            it.and so t should be a constant pointer in fact.and char t[]="abcde"
            is declare a array and give it's  elements vuales by "abcde" , and t
            is point to the first element's address.and I want to know how to
            judge if the pointer is point to a constant in my function ? thanks!
            Since your function takes a pointer to char, it should not be pointing
            to a constant. Just make sure to use const char pointers when dealing
            with literals. Or preferably, std::string

            DP

            Comment

            • Jim Langston

              #7
              Re: A problem about char pointer ?

              comp.lang.c++ wrote:
              On Mar 11, 5:06 am, "Jim Langston" <tazmas...@rock etmail.comwrote :
              >Victor Bazarov wrote:
              >>comp.lang.c ++ wrote:
              >>>this is a sample example about this question
              >>>#include<std io.h>
              >>>void chg(char* t)
              >>>{
              >>> char *s=t;
              >>> char p=*t;
              >>> while(*t++=*++s );
              >>> *--t=p;
              >>>}
              >>>int main()
              >>>{
              >>> //char * t="abcde";
              >>> char t[]="abcde";
              >>> chg(t);
              >>> printf(t);
              >>> printf("\n");
              >>> return 0;
              >>>}
              >>>I want know why the function chg() cann't work when define t with
              >>>char*, and what's the different
              >>>between char* and char[]?
              >>
              >>The difference is relatively obscure, unfortunately. When you
              >>declare 't' to be an array (char t[]), then each element of 't' is
              >>allowed to be modified, IOW it's OK to modify it. When you declare
              >>'t' as a mere pointer, and initialise it with a literal, then 't'
              >>points to the non-modifiable memory, and any attempt to change the
              >>value of elements of 't' has _undefined_beha viour_.
              >>
              >>>thanks?
              >>
              >>Yes, please.
              >>
              >Just to reiterate, the problem isn't, per se, that t is defined as a
              >char*, but where t is pointing to.
              >>
              >For example,
              >>
              >char x[] = "abcde";
              >char* t = x;
              >chg(t);
              >>
              >with your code would work. Because t is now not pointing to constant
              >memory.
              >>
              >--
              >Jim Langston
              >tazmas...@rock etmail.com
              thanks ,I think I konw .Because "abcde" is a constant ,t just point to
              it.and so t should be a constant pointer in fact.and char t[]="abcde"
              is declare a array and give it's elements vuales by "abcde" , and t
              is point to the first element's address.and I want to know how to
              judge if the pointer is point to a constant in my function ? thanks!
              Use constant correctness in your code. If anything is constant, declare it
              as such. The following will not compile with your code:

              int main()
              {
              const char* t="abcde";
              chg(t);
              }

              The error that MSVC++ .net 2003 gives me is:
              error C2664: 'chg' : cannot convert parameter 1 from 'const char *' to 'char
              *'

              The reason you are allowed to have a char pointer point to non constant
              memory in the first place is because it was allowed in C. For backwards
              compatability. As such the programmer has to be a bit more careful with
              what they are doing.

              Use the const keyword wherever you can.
              --
              Jim Langston
              tazmaster@rocke tmail.com


              Comment

              Working...