Reversing user input string with char array

Following is a code in which I am trying to reverse user input string using pointer to char. I know there are better ways to write than using this program, but I just want to know what happened in the program is that it reverses only first five letters. the count variable I used is initialized with sizeof. and sizeof always shows 4 count irrespective of string length.

In the following program input is santosh and output is otnas

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
  #include <iostream>

using namespace std;
char* call_this(char* t1);

int main()
{
    char* t {new char};
    call_this(t);

    return 0;
}

char* call_this(char* t1){
cout << "Please enter some text to reverse" << endl;
cin >> t1;
cout << "You entered " << t1 << endl;
cout << "Reverse is ";
int count {sizeof t1/sizeof t1[0]};
for(int i{count}; i>=0; --i ){
    cout << *(t1 + i);
}
return 0;
}
Last edited on
sizeof shows the size of the type, in bytes, you are using and not the lenght.. what you are looking for is lenght();
Last edited on
But sizeof is used in function argument to know the dimensions of an array. And pointers and array are same
I used to know the dimensions of pointer to char array.
Last edited on
That's the point sizeof doesnt give you the dimentions but the size of the array on the stack/heap in bytes. If you want to know how many elements you need to use lenght or size
you have created a pointer to a single char.

When your user enters text, you are overwriting memory you don't own.

sizeof t1 is like saying sizeof (char *) you're getting back the size of a pointer on your platform.

Also remember that a pointer is not an array - you may think they're the same, but they're not.

C++ provides a string class for what you're trying to do.

If you really want to do this C-style, then you must pass the length of your array, or determine it if you specify that you're using a C-style null terminated string.

@Edward01, that is misleading and incorrect. How would lenght() length() help in this case? which length() is that?

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
#include <iostream>
#include <cstring>

const size_t MY_LEN {256};
const size_t MY_LEN2 {16};

using namespace std;

void call_this(char t1[]);
void call_this(char t1[], const size_t len);

int main()
{
	char t[MY_LEN + 1] = {0};
    call_this(t);
	cout << endl;
	char t2[MY_LEN2 + 1] = {0};
    call_this(t2, MY_LEN2);
	cout << endl;
    return 0;
}

void call_this(char *t1){
cout << "Please enter some text to reverse" << endl;
cin >> t1; // this has to be limited to MY_LEN characters
t1[MY_LEN] = 0; // make sure we're within bounds
cout << "You entered " << t1 << endl;
cout << "Reverse is ";

int count = strlen(t1) - 1;

for(int i{count}; i>=0; --i ){
    cout << *(t1 + i);
}
}

void call_this(char *t1, const size_t len){ 
cout << "Please enter some text to reverse" << endl;
cin >> t1; // this has to be limited to len characters
t1[len] = 0; // make sure we're within bounds
cout << "You entered " << t1 << endl;
cout << "Reverse is ";

for(size_t i{strlen(t1)}; i>0; --i ){
	
    cout << *(t1 + i);
}
cout << *t1;
}
Thanks @Tipaye. Now I know my mistakes.
@OP

Arrays and pointers are not the same. An array's name is automatically converted to a pointer to it's first element. That doesn't make them the same.

Think about what happens when you try and do pointer arithmetic. When you know that your pointer contains the address of an array, you can compare pointers to elements of the same array and jump about within the array using pointer arithmetic:
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
void func(int *ptr);

int main()
{
	int arr[4] = {0};
	
	// arr++; can't do this, array is not a pointer
	func(arr); // automatically &arr[0] is used

	int *ptr2 = arr; // automatically &arr[0] is used
	++ptr2;			 // now ptr2 == &arr[1]
	func(ptr2);
	
	int *ptr3 = &arr[0]; // explicitly assigned to &arr[0]
	++ptr3;				 // now ptr3 == &arr[1]
	func(ptr3);
	
	ptr2 = arr;
	ptr3 = arr;
	ptr2 += 2;// now ptr2 == &arr[2]
	
	if (ptr2 > ptr3)// same array, that's okayish
	{
		
		*ptr2 += *ptr3; // arr[2] += arr[0]
	}
	
	int var = 0;
	int *pvar = &var;
	
	if (pvar < ptr2) // legal but makes no sense
	{
	}
}

void func(int *ptr)
{
	ptr++; // can do this, but are you sure ptr points to an array?
		   // do you own the next address
}



Considering your second point, sizeof can't really tell us the size of just any array. What you're talking about is useful in a different situation, where we have an array of known size (known to the compiler) e.g. size in bytes, but we need to be sure about the number of elements, i.e. size or length, in number of elements, because the size of each element is not 1 unit, i.e. sizeof element != 1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
	// imagine that sizeof int == 4
	
	int arr[4] = {0}; // sizeof arr == 16
	
	for (size_t i = 0; i < sizeof arr; ++i) 
	{
		// we will go from 0 to 15 and therefore out of bounds
	}
	
	for (size_t i = 0; i < sizeof arr / sizeof arr[0]; ++i) 
	{
		// sizeof arr / sizeof arr[0] = 16 / 4 = 4
		// we will go from 0 to 3 and are therefore safe
		// notice that arr is a fixed size array, and we already know it's size
		// we use the size of arr and size of it's elements type to determine number 
		// of elements in arr
	}
	
	int *parr = new int[4];	// Have to manually keep track of how many elements 
		                                // there are in parr[]
}
Topic archived. No new replies allowed.