Pointer error

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


my code is:

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
#include <iostream>
#include <algorithm>
#include <string>
#include <ctime>

using namespace 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...

Can anyone help with this?
The type of entireArray()'s parameter should be 'std::string *'. Also, you need to pass the length of the array. And yes, you do need to pass it.
what do you mean i need to pass it? you mean i should make length one of the parameters in the funtion?
Yes, that's what it means to pass something to a function.
also it is only showing the first slot of the array now, rather than the entire array :/
Nevermind it is showing the entire array. Could you please explain why i needed to pass the length rather than define it inside the function?
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.
Last edited on
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).
Last edited on
@ helios:
2. It's illegal to define variable-sized arrays. This is a non-standard GCC extension.

I've got a question on that one, you mean:
 
int array[var]; 


would not be accepted by any compiler? So instead using:
 
int* array = new int[var]


would be better?
All stl containers have iterators. All function in stl use this iterators. You can access those iterators with begin()/end()

So you can simplify your program as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <algorithm>
#include <string>
#include <ctime>

using namespace std;

int main()
{ 
	string myString;	
	cin >> myString;	// input your word
	
	sort(myString.begin(),myString.end());
	
	  do {
   
    cout << myString;
   
  } while ( next_permutation (myString.begin(),myString.end()) );
  
  return 0;
  
}
You can also transfer data from one container to another:
1
2
vector<string::value_type> v;
v.assign(myString.begin(),myString.end());
or
vector<string::value_type> v(myString.begin(),myString.end());
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.
Thanks helios! Then I usually did it right without knowing it...but still - keeping an eye on it won't hurt, will it?
Last edited on
in the line:
int* array = new int[var]

what does the pointer do after int?
Topic archived. No new replies allowed.