-3

Here I'm getting the output 'bcde' but since 'a' is a constant array pointer it should not have been incremented inside fun() right? Then why is it allowed here?

    void fun(char a[])
    {
     a++;
     printf("%s",a);
    }

    void main()
    {
     char a[]="abcde";
     fun(a);
    }
indiv
  • 17,306
  • 6
  • 61
  • 82
joel.wilson
  • 8,243
  • 5
  • 28
  • 48

4 Answers4

3

When you pass an array to a function in C, the array "decays" to a pointer. In other words, an equivalent function can be declared like this:

void fun(char *a)

Now the code inside the function makes perfect sense: the pointer is incremented, so when the result is passed to printf, the original string is printed starting with the second letter.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 2
    @user3801801 The pointer is not constant. Moreover, the memory the pointer points to is perfectly modifiable, because you wrote `char a[]="abcde"`, not `char *a="abcde"`. But even a pointer to constant can be modified (as opposed to a constant pointer, which is declared differently). – Sergey Kalinichenko Jul 17 '14 at 17:25
  • 1
    No, not a constant pointer. A pointer to constant data is not the same as a constant pointer. Also, what user3801801 said is correct. The data isn't even constant. – Mike Holt Jul 17 '14 at 17:25
  • okay...and if it was declared char *s="abcde" inside main den it still remains the same ryt? – joel.wilson Jul 17 '14 at 17:29
  • 1
    @user3801801 Yes, the code and its behavior would remain the same. – Sergey Kalinichenko Jul 17 '14 at 17:30
  • In C,if i declare char *s="abcde" then content of s(i.e.*s='o';) is not allowed ryt?den y is it allowed in C++? – joel.wilson Jul 17 '14 at 17:34
  • 1
    @user3801801 The assignment is not allowed in both C and in C++. The difference is that in C the code will compile, while in C++ the compiler is going to complain about the assignment of a string literal to a non-const pointer (C++ requires `const char *s = "abcd"`) – Sergey Kalinichenko Jul 17 '14 at 17:37
2

When you pass an array name to your function then you are passing pointer to its first element.

void fun(char a[])  // a is not an array of char

is equivalent to

void fun(char *a)   

You can modify a inside function because it is not an array name but a pointer to char.
You can't modify a in main as it is declared as array. Array names are non modifiable l-values.

haccks
  • 104,019
  • 25
  • 176
  • 264
1

When you are in function arguments, char a[] is treated exactly as if you wrote char *a.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
1

There is no any "constant array pointer"

In this statement

char a[]="abcde";

there is declared a non-const array. You may change it for example as

a[0] = 'A';

When you pass the array to the function as an argument then there is used conversion from the array to a pointer of type char * that points to the first element of the array.

This function declaration

void fun(char a[]);

is equivalent to

void fun(char *a);

The parameter is adjaced to the pointer.

So inside the function you can to change the pointer itself and the object it points to. For example

 a++;
 *a = 'B';

If you want that the pointer would not be changed in the function you could declare it as

void fun(char * const a);

In this case the compiler would issue an error for statement

 a++;

because a is indeed a const pointer.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335