1

I'm getting a little bit overwhelmed with pointers.

Lets say I have an array of chars

char arrayOfChars[50];

and I want to pass it to some function that will manipulate it.

The way I pass it would be by reference:

someFunction(char &arrayToManipulate) {}

but I also often see this:

someFunction(char *arrayToManipulate) {}

What's the difference between the two,

And bonus question; is there any problem if in the parameter list of someFunction, I name the local variable by the same name? I.e.

someFunction(char &arrayOfChars) {}

Many thanks.

aName
  • 257
  • 2
  • 8
  • 2
    https://stackoverflow.com/questions/2094666/pointers-in-c-when-to-use-the-ampersand-and-the-asterisk This should help โ€“ Boomer Apr 19 '19 at 15:42
  • Lightning fast answers! @tkausl - I think it's mostly duplicate, yes. So summarize, requesting a pointer in the parameter list, gives me flexibility later in the function, if I should want it to point to something else. Whereas just pulling in the address of the variable doesn't give me that flexibility. - And the bonus question? โ€“ aName Apr 19 '19 at 15:48
  • 1
    `is there any problem if in the parameter list of someFunction, I name the local variable by the same name?ยด You can't. At least gcc and clang don't allow it, not sure whether it is actually specified in the standard or not. โ€“ tkausl Apr 19 '19 at 15:54

0 Answers0