New to C
here and I have found the following algorthim to concatenate strings whilst searching books online:
Algorithm: STRING_CONCAT (T, S)
[string S appends at the end of string T]
1. Set I = 0, J = 0
2. Repeat step 3 while T[I] ≠ Null do
3. I = I + 1
[End of loop]
4. Repeat step 5 to 7 while S[J] ≠ Null do
5. T[I] = S[J]
6. I = I + 1
7. J = J + 1
[End of loop]
8. Set T[I] = NULL
9. return
Essentially, I have tried to implement this with my current working knowledge with C
. However, I am unsure on how to get the char*
pointers to correctly point inside the function. For example,
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
const char* stringConcat(char* T, char* S){
int i = 0;
int j = 0;
char* Q;
while(*S[i] != NULL & *T[i] != NULL){
i += 1;
while(*S[j] != NULL){
*T[i] = *S[j];
i += 1;
j += 1;
}
}
*T[i] = NULL;
return *T
}
int main(void){
char* sentence = "some sentence";
char* anotherSentence = "another sentence";
const result;
result = stringConcat(sentence, anotherSentence);
return EXIT_SUCCESS;
}
I get a logged error output with the following:
exe_4.c:8:11: error: indirection requires pointer operand ('int' invalid)
while(*S[i] != NULL & *T[i] != NULL){
^~~~~
exe_4.c:8:27: error: indirection requires pointer operand ('int' invalid)
while(*S[i] != NULL & *T[i] != NULL){
...
...