-3
#include <stdio.h>

increment(char *c)
{
    c++;
    c++;
    printf("works 'n' %c \n", *c);
}

int main()
{

    char *p;
    char mon[10] = "Monday";
    p = mon;
    increment(p);
    printf("expecting 'n' %c \n", *p);

    return 0;
}

If character buffer pointer incremented in other function it will not reflect after coming out of the function.

Megharaj
  • 1,589
  • 2
  • 20
  • 32
  • 1
    Arguments in C are always passed by value (*assigned* into parameters). – Antti Haapala -- Слава Україні Mar 31 '17 at 11:03
  • When you call **increment(p)**, the argument is pushed on the stack and it will be a copy of the local pointer from main. Modifying it in the increment function will just change the copy and not the actual pointer from main (so, as the answer said, you need to create a pointer to the pointer). – Valy Mar 31 '17 at 11:17

1 Answers1

5

In increment(char *c) you create a new pointer c which is pointing to the same object p does, but they are not the same, they are just pointing to the same thing. If you increment one, the other one stays the same.

If you want to increment the pointer p you need a pointer to this pointer, pass it to your function and change p instead of a local copy.

izlin
  • 2,129
  • 24
  • 30