C pointers with char array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 333471
    New Member
    • Jun 2014
    • 4

    C pointers with char array

    Code:
    void func(char *s)
    {
    s+=1;
    }
    void main()
    {
    char s[]="neymar messi";
    func(s);
    printf("%s",s);
    }
    Above program shud have gvn me eymar messi ryt?
    but its showing 's' as it is...y?

    But in this case the output is nekmar messi....
    Code:
    void func(char *s)
    {
    s[2]='k';
    }
    void main()
    {
    char s[]="nekmar messi";
    func(s);
    printf("%s",s);
    }
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    C uses a Call-by-Value programming model.

    You reuse variables name s inside functions func and main, and that makes the explanation more confusing. I will edit your first snippet to eliminate the confusion.
    Code:
    void func(char *t)
    {
    t+=1;
    }
    void main()
    {
    char s[]="neymar messi";
    func(s);
    printf("%s",s);
    }
    main passes s to func, which has the effect of copying the value of main's s into func's t. func then increments t, but that has no effect on main's s, so line 9 prints the original string.

    Now let's look at the second snippet...
    Code:
    void func(char *t)
    {
    t[2]='k';
    }
    void main()
    {
    char s[]="nekmar messi";
    func(s);
    printf("%s",s);
    }
    main passes s to func, which has the effect of copying the value of main's s into func's t. func then writes 'k' into the third character of the string pointed to by t. t has the same value as s, so it points at the same string that s points at; thus 'k' is written to the third character of the string pointed to by s, but that character is already 'k', so this write has no effect.

    By the way, in the second snippet you are overwriting a string literal. The C Standard says doing so has unpredictable results. I can't remember if this is formally labeled undefined behavior or implementation defined behavior, but either way it is ill-advised.

    By the way, main should always return int.

    By the way, in general, a printf format string should end with newline. That is, line 9 of both snippets should be:
    Code:
    printf("%s\n",s);

    Comment

    • kudos
      Recognized Expert New Member
      • Jul 2006
      • 127

      #3
      Here you probably should have a peak at something I wrote about pointers (http://bytes.com/topic/c/answers/956...16-bit-pointer).

      You start by sending a pointer s into the method func. Try to write printf("%d\n",s ); What is the output? Probably a weird value, because that is the address to the pointer (made by the compiler). Now, what you do, it to add 1 to the pointer value, i.e., you look at what string starts at the same memory location +1.

      Let me illustrate:

      Code:
      char c[] = "hello, how are you";
      char *d = c;
      printf("%d\n",d); // where the pointer d points (in memory)
      printf("%s\n",d); // the content of the string placed at that address
      d+=1; // add 1 to where it points in memory
      printf("%d\n",d); // what is the address now?
      printf("%s\n",d); // and what is the content now???

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Use the "%p" format specifier to print the value of a pointer; otherwise you are relying on undefined behavior.

        Comment

        • 333471
          New Member
          • Jun 2014
          • 4

          #5
          im still confused...in both cases it was call by value...so den it shud nt reflect the changes...but in the second case its reflecting it(the 's' was neymar messi and not 'nekmar messi')

          Comment

          Working...