1

I got a function (practice for college) that I'm supposed to define, goes like this:

void add_str(char* &a, char* b);

It's supposed to add the second string to the first one. Why would I pass a *& parameter to a function, and when is it useful?

Shaytan
  • 23
  • 6
  • Since you are referring to a C-string: The code in your example is likely useless or wrong (without knowing/passing the capacity of 'a') –  May 17 '14 at 19:02
  • 1
    @DieterLücking Hm, it may not be a beautiful way to do it, but why useless/wrong? – Joachim Isaksson May 17 '14 at 19:05
  • The strings need to be *null-terminated* for this to work. – Felix Glas May 17 '14 at 19:06
  • The question isn't _"when my function is useful"_, but rather _"when is passing an argument with *& useful"_. Since it's an academic example (as noted in the question), I think its _pretty_ safe to assume that there's enough space for `a` to be concatenated with `b` and that both are null-terminated.. – Paweł Stawarz May 17 '14 at 19:08
  • @DieterLücking as snps just said, the strings are null-terminated, so capacity of a is not necessary. – Shaytan May 17 '14 at 19:10
  • @PawełStawarz "a" is in fact just the right size to hold it's own string, but it doesn't matter, it can be redefined using the strlen() func on both strings and then setting "a" to point to a new string with a correct size – Shaytan May 17 '14 at 19:25
  • @Shaytan Dieters point was that - while of course `a` can hold its current contents - it may be unable to hold contents of `a+b`, because of - for example - lack of memory. Still - it's an academic example, so we can let that slide, since noone will use this in normal cases anyhow. – Paweł Stawarz May 17 '14 at 20:09
  • @PawełStawarz The `&` reference on the pointer makes it replaceable by the function, so even if `a` can't fit `a+b`, the function can replace `a` with a newly allocated memory block that does, and have the caller see the change. – Joachim Isaksson May 17 '14 at 21:08
  • @JoachimIsaksson only when the machine can allocate a large-enough memory block. If the memory is full, the function won't do a miracle :) – Paweł Stawarz May 17 '14 at 21:22

2 Answers2

2

Because you're going to need to create new space to hold the concatinated string and you need to pass that back somehow. This signature allows the function to overwrite the parameter passed in so that the pointer the caller gave will be moved to point at the new string.

This is a really, really stupid way to solve the problem but the teacher is probably trying to make some sort of point. Hopefully it's a good one. I'm not fond of broken examples but it can be hard to think of legitimate ones when what you want to teach is more usually found in something too complex to relate to newbs.

Edward Strange
  • 40,307
  • 7
  • 73
  • 125
1

It allows add_str to modify whatever is passed as a. That's what a non-const reference parameter is for; it only looks odd because the parameter is a pointer rather than a simple type.

Alan Stokes
  • 18,815
  • 3
  • 45
  • 64