1

Say we have the following function prototypes:

void function1(char str[]);
void function2(char *str);

Now say we have a string char name[] = "John"; that we wish to pass through these functions. What is the difference between the two? What are their uses and limitations? Are there circumstances in which one is preferred over the other? Would it make a difference if instead the string were initialized as char *name = "John"?

I understand the difference between using char str[] and char *str within a function, but I don't know their behavior as function parameters or arguments.

Marcus McLean
  • 1,306
  • 2
  • 13
  • 24
  • If you bothered to look around, you'd surely find the answer. This has been asked and answered many times over. – Tony The Lion Apr 27 '13 at 23:51
  • If you copy/paste your entire title into the Google search box, (as I just did, for a laugh), you will get loads of hits. – Martin James Apr 27 '13 at 23:54
  • 1
    possible duplicate of [Pointer vs Array in function definition: what is the difference between void fct1(int \*p) and void fct1(int p\[\])?](http://stackoverflow.com/questions/15261509/pointer-vs-array-in-function-definition-what-is-the-difference-between-void-fct) – fredoverflow Apr 27 '13 at 23:57
  • You've your answer here http://stackoverflow.com/questions/16144535/difference-between-passing-array-fixed-sized-array-and-base-address-of-array-as/16144620#16144620 – Rüppell's Vulture Apr 28 '13 at 01:08

3 Answers3

4

There is no difference. Inside a parameter list, parameters of the form T[] and T[n] are silently re-written as T* by the compiler. This means that you cannot pass arrays by value.

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
4

There is absolutely no difference in C between

void function1(char str[]);
void function2(char *str);

because char str[] simply reduces to char * when passed as argument to a function.And for the record, even char str[20] is exactly the same thing as the function sees it as char *str.

As for whether it would make a difference if the string were initialized as

char *name = "John";

yes,it does!Here address of that string John is being assigned to pointer name, and another addresses can be reaassigned to name later.

char *name="John";
name="Mary";  //Works in C

But in

char name[]="John";

you are initializing a character array object name to John.The difference here is that you just can't reassign another string to name after initialization.The following is wrong in C:

char name[]="John";
name="Mary";// Wrong

While posting questions,search the forum for a minute to see if the question has already been answered.The first part of your question has been asked and answered very well multiple times.Since you seemed genuinely confused about the second part,I've answered that here.

Rüppell's Vulture
  • 3,583
  • 7
  • 35
  • 49
1

There is no difference from a technical point of view. However, if you use [] then you are documenting for the person reading your code that you expect an array.

user2327642
  • 465
  • 1
  • 5
  • 8