Hi,
Im currently in the process of learning about pointers and dont know all that much about them yet. However on a code i was making i got an error that says:
anagram.cpp:36:22: error: conversion from ‘std::string*’ to non-scalar type ‘std::string’ requested
#include <iostream>
#include <algorithm>
#include <string>
#include <ctime>
usingnamespace std;
void entireArray(string array);
void entireArray(string array)
{
int elements = sizeof(array) / sizeof(array[0]);
for (int i = 0; i < elements; ++i)
std::cout << array[i];
}
int main()
{
srand ( time(NULL) );
string myString;
cin >> myString; // input your word
int l = myString.length();
string array[l];
for (int i=0; i < l; i++)
array[i] = myString[i];
sort(array,array+l);
do {
entireArray (array);
} while ( next_permutation (array,array+3) );
return 0;
}
I know from previous experience if you try to output an array by just saying "cout << arrayName" that it will give you the address and that has something to do with pointers...
Now that I take a closer look at your code, you're doing a few things wrong.
1. You don't need array (defined on line 27) at all. An std::string is already an array of characters (or something more or less isomorphic to that).
2. It's illegal to define variable-sized arrays. This is a non-standard GCC extension.
3. If it's working without explicitly passing the size of the array, it's by pure luck.
4. You also don't need entireArray(). std::cout <<a_string will print an std::string.
Hey,
I already knew that you werent supposed to have a variable sized array, i was just leaving it like that for now for simplicities sake.
Also, as for 4 ive tried that before and it just shows the location of the array not the content...
and i guess it makes sense that a string is already an array of characters :P should i try using next permutation with that instead?
(also i did add in passing the length, thats why it started showing the entire array)
Also, as for 4 ive tried that before and it just shows the location of the array not the content...
That's because you weren't printing an std::string. You were printing an array of std::strings. Try std::cout <<myString instead.
(also i did add in passing the length, thats why it started showing the entire array)
Oh. In that case, to answer your previous question: imagine you have a length of rope, and you need to cut a specific section of it. If I just tell you "start cutting 15 cm from your end", you don't have enough information to figure out exactly where to make the next cut. A pointer is exactly that: (where you need to start reading) - (the start of the memory space). When you pass an array to a function, what you're doing in fact is passing a pointer to that array (the reality is a little more complicated because C's treating of stack arrays is rather obtuse; this is a good enough abstraction), and a pointer can point to a single object or to an array a million elements long.
More to the point, sizeof(array) / sizeof(array[0]) will work when used in the function that allocated array precisely because of C's treating of stack arrays. It won't work if you try to pass the array because 'array' in that context is a pointer to the actual array, and pointers have a fixed size (usually).
ZED0815: A few compilers will accept your first snippet, most won't. All compilers will accept your second snippet unless you're working in a special environment, such as an embedded system, or kernel space.