Reputation:
I am extremely new to c++ and wrote this program to reverse a word. What I tried was to basically loop through an array and swap first letter with the last, second with the second last etc. However the result is some wired characters ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠Ω ⌠7p≈╗
. I don't want a work-around since there are plenty of examples online. I just want to know why what I am doing wont work.
#include <iostream>
using namespace std;
int main()
{
char word[10];
for (int i = 0; i < 10; i++)
{
word[i] = word[sizeof(word - i)];
}
cout << word << endl;
return 0;
}
It is also giving me this warning warning C6001: using uninitialized memory 'word'
. But I though I initialized the memory by doing char word[10]
.
Upvotes: 2
Views: 313
Reputation: 37997
Problem is what sizeof(word - i)
actually does.
Short answer: it returns pointer size (4 or 8).
More detailed answer:
word
has type char[10]
and size 10
word + 1
, the compiler will decay array to pointer to char to be able do this operation. So result type is char *
sizeof(char *)
is 4 or 8 depending on what platform you build it (32 or 64 bits)word[sizeof(word - i)]
always refers to the same cellThere is also a second problem. In C
, strings of text must be null terminated to indicate the size of it. Your word
contains apple
without zero character at the end, so garbage is printed after apple
(it may even crash program).
Also, your code is more C like, in C++ this can be done like this:
int main()
{
std::string word{"apple"};
std::string reversed{word.rbegin(), word.rend()};
std::cout << word << '\n';
std::cout << reversed<< '\n';
return 0;
}
Upvotes: 3
Reputation: 311048
There are at least three problems.
The first one is that the array is not initialized and has an indeterminate value.
char word[10];
The second one is that this loop
for (int i = 0; i < 10; i++)
in any case will not reverse the array.
Even after you updated the array like
char word[5] = {'a', 'p', 'p', 'l', 'e'};
nevertheless the loop does not make sense.
for (int i = 0; i < 10; i++)
And this expression
sizeof(word - i)
is incorrect. This expression word - i
has the type char *
due to the pointer arithmetic. At least you mean
sizeof(word ) - i
Instead you could write something like the following
#include <iostream>
#include <utility>
#include <cstring>
//...
char word[] = "Hello World!";
for ( size_t i = 0, n = std::strlen( word ); i < n / 2; i++ )
{
std::swap( word[i], word[n - i - 1] );
}
std::cout << word << std::endl;
Or you could write a separate function. Here you are.
#include <iostream>
#include <utility>
#include <cstring>
char * reverse( char *s )
{
for ( size_t i = 0, n = std::strlen( s ); i < n / 2; i++ )
{
std::swap( s[i], s[n - i - 1] );
}
return s;
}
int main()
{
char word[] = "Hello World!";
std::cout << word << '\n';
std::cout << reverse( word ) << '\n';
}
The program output is
Hello World!
!dlroW olleH
Upvotes: 2
Reputation: 628
sizeof(word - 1)
will be 4, or 8. Since it's a pointer.
What you need is strlen(word)
.
Also, word
is not initialized, do something like char word[10] = "blabla"
.
Note, that for the edit you did, where:
char word[5] = {'a', 'p', 'p', 'l', 'e'};
strlen
will not work, since you need a character to be null, or 0
, to know where the string ends.
Also, it seems you're writing in C++, for such things you have std::string
, you don't need a char array. and than you can call word.length()
for the length, and it can dynamically set the size it needs for the string and grow.
Upvotes: 0
Reputation: 3530
In your case word
is declared but not initialized. That warning means word array can contain un-initialized values.
char word[10];
To see sensible characters rather than garbage (uninitialized) value, either inputs value to word
or initialize it in code.
Why this behaviour? the stack contains trash until the program places specific values into variables defined on the stack.
Upvotes: 0
Reputation: 104
C/C++ doesn't have an auto initialization for declared variable. You must fill in each of the array with a value before referencing because C/C++ is using pointers.
Upvotes: 0