Return line problems -- char to *char

Hi,

I'm writing pretty much my first code with individual functions ever, and I've run into a couple of issues when I try to compile it. There are 6 or so functions, but the error is the exact same for all of them, so I've only included one example here. [Also, I've tested the functions individually, but then I had the return statement as "return 0", unlike now.]

Here's the code:
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
char key(char mystring[100])
{
  char alphabet[81]="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!?0123456789.,:;/\()@#$%^&*_-";
  char alpha[100];
  alpha[0]=mystring[0];
  int count=1; int i;int j;int k;
  int length = strlen(mystring);
  int alphalength;
  int alphabetlength =strlen(alphabet);
  cout << length<<endl;
  for(i=1;i<=length;i++)
    {
      k=0;
      alphalength = strlen(alpha);
      for(j=0;j<=alphalength;j++)
	{
	  if(alpha[j]==mystring[i])
	      k=k+1;
	}
      if(k==0)
	{
	  alpha[count]=mystring[i];
	  count=count+1;
	}
      else
	  count=count;
    }

for(i=0;i<=alphabetlength;i++)
    {
      k=0;
      alphalength = strlen(alpha);
      for(j=0;j<=alphalength;j++)
	{
	  if(alpha[j]==alphabet[i])
	      k=k+1;
	}
      if(k==0)
	{
	  alpha[count]=alphabet[i];
	  count=count+1;
	}
      else
	  count=count;
    }

  return alpha;
}


The goal is that it returns an array of characters which will be used by other functions eventually. In my separate test, I made it print this array, and that worked just fine, but this argument thing doesn't.

Here is the error I get when I try to compile (among a couple others) [Note, the line marking is not right here, the error hits in at the return line, not what it says in the error message below.]:

1
2
3
4
g++ message.cpp
message.cpp: In function ‘char key(char*)’:
message.cpp:110:10: error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
message.cpp:67:8: warning: address of local variable ‘alpha’ returned [enabled by default]


Also, I don't know how big a deal the last warning line is, any input is much appreciated!

Thanks!
Also, I don't know how big a deal the last warning line is

It's a very big deal. The local array is destroyed when the function exits and the pointer becomes invalid.

As for the other problem... there's no line 110 in the code snippet you posted.
I imagine it could be because you're trying to return an array in a function that expects a single character to be returned.

Also, count=count; is a bit superfluous.
The return type of your function is declared as char

char key(char mystring[100])

but you are trying to return the array alpha

return alpha;

which is implicitly converted to a pointer to its first element. So the function return type and the type of the returned expression are not compatible.
Using std::string would make it much easier:

http://www.cplusplus.com/reference/string/string/

No need to know the size (you can ask whenever necessary) and you can return it without a problem.

with char array you have to do something like this:
1
2
3
4
5
6
void key(char alpha[100], char mystring[100])
{
...
  char alpha[100];
...
}

You cannot return a local defined array.
Thanks for all the replies!

@Athar: line 110 is not there because I didn't paste in all the other functions, and the one I posted happened to start a few lines into the main code. What the error refers to as line 110 is the return line of the function I posted (line 47 here).

@vlad & iHutch105 & coder777: so is the problem that I have to define the array in the main function and not in this one? And also that the form of the return doesn't match what it expects? But everything so far has been defined as char, hasn't it?

I changed the argument line as coder777 suggested, and that solves the warning problem :) Thanks!
The 'invalid conversion' remains, though. Is that because I've confused the function so that it expects something else than what I'm trying to return?
Arrays are implicitly passed by reference. Just pass an array from main into the function and assign the values within the function. No need to return.
@iHutch105
Arrays are implicitly passed by reference


It si a wrong statement. Arrays are implicitly converted to a pointer to the first element. Passing by reference is another idiom.
Another excellent piece of pointless pedantry there, vlad, you certainly have the knack of that.

My point, so as to not deviate from the OP's issue, is better expressed in code form:
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;

void Assign(int arr[5])
{
	for(int i=0; i<5; i++)
	{
		arr[i] = i+1;
	}
}

int main(int argc, char* argv[])
{
	int my_array[5];

	Assign(my_array);

	for(int i=0; i<5; i++)
	{
		cout << "Element " << i << ": " << my_array[i] << endl;
	}

	return 0;
}


Note: Excuse any odd indentation, copied from VS and it tends to do that (I must turn tabs off at some point).
Last edited on
@iHutch105: Ah! I see! So I don't actually have to return anything at all? I just define the various char's and int's in my main function, and then run them through the other functions as the arguments? That explains a lot of things, actually! Thanks! :)
As long as they're arrays, yes.

If they're single ints or chars then they'll need to be passed by reference or returned.
Topic archived. No new replies allowed.