0

When you assign a string literal such as "ABC" to char a[] ex.

char a[] = "ABC";

it has the effect of doing

char a[4] = {'A','B','C','0'};

does this same thing apply when you pass it to a function parameter

ex.

int f(char a[]);

vs.

int f(char *a);
1
  • "ABC" is a char [4] not a char [3] Commented Jan 1, 2013 at 23:27

2 Answers 2

5

does this same thing apply when you pass it to a function parameter

No; in general, in C you can't pass arrays directly by value; every array parameter to a function is actually interpreted by the compiler as a pointer parameter, i.e. when you write

int f(char a[]);

the compiler sees

int f(char *a);

(the same applies even if you specify the dimensions of the array)


By the way,

it has the effect of doing

char a[3] = {'A','B','C"};

Actually, it has the effect of doing:

char a[4] = {'A','B','C', 0};
Sign up to request clarification or add additional context in comments.

Comments

2

No, because both of those function declarations are identical. Both declare a function that takes a pointer to char, and in both cases the argument becomes initialised with a pointer to the first element of the string literal.

1 Comment

You mean of the string, not of the string literal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.