Char Pointer

Hi, everyone.

I'm new with C++, especially with pointers and I'm a bit confused.
My task is to write a program, that has a recursive int array_search(char* text1, char* text2) function and must compare the inputs. Maximal length is 20.
If text2 is NOT in text1, then return -1, in other case return the start position of text2 in text1.

Can someone help me with it?
Thanks :)
You first have to understand that recursion is nothing more than a fancy for loop.

So as a stepping stone to recursion, try and write it as a simple loop, stepping through text1 and comparing.
Something like this ?
But it doesn't work correctly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

#include <iostream>

using namespace std;

const unsigned short int MAX = 20;

int zeichenkette_suchen_rekursiv(
	char*, 
	char*, 
	unsigned int, unsigned int, unsigned int);

int main() {
	char *text = new char[MAX];
	char *zkette = new char[MAX];

	cout << "Enter text1: ";
	cin.getline(text, MAX);

	cout << "Enter text2: ";
	cin.getline(zkette, MAX);

	int rueckgabe = zeichenkette_suchen_rekursiv(text, zkette, 0,0,0);

	if (rueckgabe == -1)
		cout << "Die Zeichenkette '" << zkette << "' ist NICHT in dem Text '" << text << "' enthalten." << endl;
	else {
		cout << "Die Zeichenkette '" << zkette << "' ist in dem Text '" << zkette << "' enthalten." << endl;
		cout << "Sie startet ab Zeichen " << rueckgabe << " (bei Zaehlung ab 0)." << endl;
	}

	system("pause");
	return 0;
}


int zeichenkette_suchen_rekursiv(
	char *text,
	char *zkette,
	unsigned int text_pos,
	unsigned int text_such_pos,
	unsigned int zkette_such_pos) {
	

	if (text[text_pos] == zkette[zkette_such_pos]) {
		text_such_pos++;
		zkette_such_pos++;
	}
	else {
		text_pos++;
		text_such_pos = text_pos;
		zkette_such_pos = 0;
	}

	if (text_such_pos == sizeof(text)) {

		if (zkette_such_pos != sizeof(zkette)) {
			return -1;
		}

		return text_pos;
	}

	else if (zkette_such_pos == sizeof(zkette)) {
		return text_pos;
	}
	else
		return zeichenkette_suchen_rekursiv(
			text,
			zkette,
			text_pos,
			text_such_pos,
			zkette_such_pos);
	}


Last edited on
> if (text_such_pos == sizeof(text))
There is a big difference between sizeof() and strlen() when all you have is a char* pointer.

Also, you can save yourself a lot of code by just making use of strncmp().

I forgot to mention, that i can't use strings and C-string functions.

What should i use instead of sizeof() to get the length of a pointer?
1
2
3
4
5
6
int ptrlen (char* ptr) {
	int len=0;
	for(;*ptr != '\0';ptr++)
		len++;
	return len;
}


Note that pointer must have a '\0' character.

Basically you need to initialize the pointer to string, and if you're initializing explicitly with {} you must mention '\0' at the end if you are going to use that block of code with the pointer.

(edit: Just know that if you used {} you should leave enough space for a '\0' at the end)

Otherwise you will get an infinite loop or most likely unexpected behavior.

Last edited on
Pointers do not have length.

How does strlen() count characters? Where is the end of a C-string?

C-string is an array of char that contains a null character at end.

1
2
3
4
5
6
7
8
9
10
11
12
char snafu [5] { 'H', 'i' };
// assert:
// snafu[0] == 'H'
// snafu[1] == 'i'
// snafu[2] == 0
// snafu[3] == 0
// snafu[4] == 0

char * fubar = snafu + 1;
// assert:
// fubar[0] == 'i'
// fubar[1] == 0 

The snafu is a "large" array that contains C-string "Hi". (Two characters plus null as third char.)
The fubar points to C-string "i". (One character plus null as second char.)
Brace initializing like so:
snafu[5] = {'H', 'i'};
Initializes all 5 indexes??

On VS2017, for both release and build, unspecified indexes seem to be initialized with 0, is that just a coincidence?
@Grime
To quote C99:

The initialization shall occur in initializer list order, each initializer provided for a
particular subobject overriding any previously listed initializer for the same subobject; all
subobjects that are not initialized explicitly shall be initialized implicitly the same as
objects that have static storage duration.

And static storage duration just means '\0', 0, 0.0, NULL as appropriate to the type of object being implicitly initialised.


@IvoSil
> I forgot to mention, that i can't use strings and C-string functions.
Writing your own myStrlen() and myStrncmp() are common exercises for beginners.

You start with strlen() and strncmp() to begin with. When you know the code works, you can substitute your own.





Last edited on
misread that.
yea write your own. just maintain the zero end of string character, the rest is pretty much what you expect but you MUST track and maintain that or all is lost.
Last edited on
Topic archived. No new replies allowed.