0

I'm checking my code currently for errors and found that I forgot to use '&' when I'm passing an array as a parameter to a function that I modify then.

I'm thinking that is because it's an array of chars, I'm already passing its address, even without the '&', but I couldn't validate it anywhere.

function prototype:

void removeWhiteChats(char *s);

Here's an example:

char p[] = "1 2 3     4 56";
removeWhiteChars(p);
print("%s,p);

Output: "123456";

and this works as well:

char p[] = "1 2 3     4 56";
removeWhiteChars(&p);
print("%s,p);

Output: "123456";
Sheilem
  • 59
  • 5
  • 2
    *"I'm passing an array as a parameter to a function"* No, you are passing a pointer, by value. – Bob__ May 04 '22 at 12:53
  • 2
    See also [How come an array's address is equal to its value in C?](https://stackoverflow.com/questions/2528318/how-come-an-arrays-address-is-equal-to-its-value-in-c): You're passing the same address in both cases. Your function signature is `char*`, forcing the correct indexing. – Luatic May 04 '22 at 12:54
  • C doesn't support pass by reference, what you are passing is a pointer variable. – Rinkesh P May 04 '22 at 12:55
  • `&p` is a pointer to an array. `p` becomes a pointer to the first element of the array. They have the same value but different types. – stark May 04 '22 at 13:13
  • @RinkeshP: In C, “pass by reference” means passing a reference manually, that is, providing a called function access to an object by passing a pointer to it. That is the terminology that was used in the past and that is still used, and the C standard says that a pointer provides a reference. In C++, a new feature was invented that provided a built-in automatic reference, and people discussing C++ use “reference” to mean that feature. That new use in the context of C++ does not change the meaning of “reference” when discussing C code. “Passing by reference” in C means passing a pointer. – Eric Postpischil May 04 '22 at 14:11

0 Answers0