-1

I don't want to pointers, how can I write these 2 function without using pointers writing, and how to the call to the function will change?

void addSubString(char subString[], char help[], int *position, int until){
    int counter = 0;
    for (; counter< until; counter++, (*position)++){
        help[*position] = subString[counter];
    }
}

void addReplaceWith(char replaceWith[], char help[], int *position){
    int counter = 0;
    for (; replaceWith[counter] != '\0'; counter++, (*position)++){
        help[*position] = replaceWith[counter];
    }
}

calls:

addReplaceWith(replaceWith, help, &helpCounter)
addSubString(subString, help, &helpCounter, subStringHelp); 
Maroun
  • 94,125
  • 30
  • 188
  • 241
  • *Why* don't you want to do pointers? – ArjunShankar Dec 09 '14 at 16:05
  • First please clean up your code; INDENT: C does not care. Then change all of `(*position)` to `position`, except when you say `int *position`, it is already established that `position` is an `int *` after the function parameter is declared. – Gophyr Dec 09 '14 at 16:10
  • possible duplicate of [What are the differences between a pointer variable and a reference variable in C++?](http://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in) – Mmmh mmh Dec 09 '14 at 16:18

3 Answers3

1

How about:

int addSubString(char subString[], char help[], int position, int until){
    int counter = 0;
    for (; counter< until; counter++, position++){
        help[position] = subString[counter];
    }
    return position;
}

int addReplaceWith(char replaceWith[], char help[], int position){
    int counter = 0;
    for (; replaceWith[counter] != '\0'; counter++, position++){
        help[position] = replaceWith[counter];
    }
    return position;
}

calls:

helpCounter = addReplaceWith(replaceWith, help, helpCounter)
helpCounter = addSubString(subString, help, helpCounter);

I assume the "substringHelp" part was a typo?

swstephe
  • 1,840
  • 11
  • 17
  • Technically, the arrays are just a different way of writing pointers, too. – swstephe Dec 09 '14 at 16:06
  • This is not a direct translation, the position member will be different after the function is called, im not sure if this is important to the OP. You should make sure though. – Fantastic Mr Fox Dec 09 '14 at 16:08
  • I assume that "no pointers" means they want to call with regular parameters for some reason, perhaps so it wouldn't modify variables in place, and you could call it with integer arguments, for example. – swstephe Dec 09 '14 at 16:19
1
void addSubString(char subString[], char help[], int position, int until);

You can change your function prototype as shown above.

If the value of the position needs to be maintained between two function calls then you can make it as static or make position global and use it

Gopi
  • 19,784
  • 4
  • 24
  • 36
1

You could change int *position to int position in the parameters, and then use position++ instead of (*position)++ in the methods.

The problem with this approach is that, although you get functions that will perform the same operation as before, the variable helpCounter won't change its value with the invocation of any of the two. In this particular case, you could solve by returning the modified value of the counter:

int addSubString(char subString[], char help[], int position, int until) {
  int counter = 0;
  for (; counter < until; counter++, position++){
    help[position] = subString[counter];
  }
  return position;
}

Then you would call:

helpCounter = addSubString(subString, help, helpCounter, subStringHelp); 

However, I'd advise against this for two reasons:

  • when more than one changed value has to be returned, it is not possible to follow this approach
  • this is the typical situation in which pointers are meant to be used: their semantics will help indicate that your functions could modify the parameters passed as pointers, just by reading the declaration. A return value instead is much more cryptic if it doesn't adapt to the natural semantics of the function. Example: in int square(int) the meaning of the return value is evident, while in
    int addSubString(char[], char[], int, int) it certainly isn't.
0x5C91
  • 3,360
  • 3
  • 31
  • 46