Creating a function that returns a character array

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 511 Views - Last Post: 22 November 2012 - 12:59 PM Rate Topic: -----

#1 Naz123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-November 12

Creating a function that returns a character array

Posted 19 November 2012 - 06:16 PM

hey guys, i have been trying to get this program to work but i have had no luck. I basically need to create a function which will insert a letter, that was entered by a user, into an array which is alp["BDJKMPWXZ"}. Then i want this function to return this array. this is the code i have so far. it has a bunch of errors that i don't understand. someone help. Thanks. Any help is appreciated.

#include <iostream>

using namespace std;
char adelle(char alp[], char, char alpnew[]);
int main(){
char letter;
char alp[10]={"BDJKMPWXZ"};
cout<<"Please enter a letter you would like to add to the array:"<<endl;
cin>>letter;
adelle(alp, letter, alp);
	return 0;}
char adelle(char alp[], char character, char alpnew[]){
	
	char alpnew[]={"BDJKMPWXZ"};
for (int i=0; i<17; i++){
	if (character=='a')
		alpnew[11]={"ABDJKMPWXZ"};}
return alpnew[11];}


This post has been edited by macosxnerd101: 19 November 2012 - 06:21 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Creating a function that returns a character array

#2 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Creating a function that returns a character array

Posted 19 November 2012 - 06:58 PM

C++ assumes that you will be making changes to any array you pass to it (barring any class matters), so just pass the name of the array to the function, make your changes to the array, and return. You do not want or need, to return the array - it's already in the calling function, waiting for you.
Was This Post Helpful? 1
  • +
  • -

#3 Naz123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-November 12

Re: Creating a function that returns a character array

Posted 19 November 2012 - 07:04 PM

thanks for the reply. that is where i am having a problem. In the function itself, i wrote a for loop which will check for which letter the user inputted. within that for loop, i put in an if statement which says that if the character was 'A', then the new array alpnew[]={"ABDJKMPWXZ"}. the compiler says that it expects an expression. Can you explain what i can do? thanks
Was This Post Helpful? 0
  • +
  • -

#4 Mrk  Icon User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 66
  • Joined: 03-December 09

Re: Creating a function that returns a character array

Posted 20 November 2012 - 12:50 AM

Here is your code reformatted more to my liking : )

#include<iostream>
using namespace std;

char adelle(char alp[], char, char alpnew[]);

int main()
   {
       char letter;
       char alp[10]={"BDJKMPWXZ"};
       cout<<"Please enter a letter you would like to add to the array:"<<endl;
       cin>>letter;
       adelle(alp, letter, alp);
	   return 0;
   }

char adelle(char alp[], char character, char alpnew[])
   {
       char alpnew[]={"BDJKMPWXZ"};
       for (int i=0; i<17; i++)
       {
	       if (character=='a')
	       alpnew[11]={"ABDJKMPWXZ"};
       }
       return alpnew[11];
   }


Now I think there are several things you need to understand first. If you pass an array to a function, that could end up being a lot of work. For example, what if you had this array: char alp[1000]. That would be an awful lot of elements to pass to the function, right? What if there were 10,000 elements instead of just 1000? Or more? It could take all day to pass all of that to a function! You don't think that the compiler is that dumb, do you? LOL.

So what happens then when you try to pass an array to a function is that secretly, just a pointer to the array gets passed instead to the function, and not the actual array itself. This makes things much simpler. So when you think you are manipulating the array which was supposedly passed to the function, you are actually manipulating the actual array back in main() via that pointer! So no need to return an array because you were manipulating the original array all along. So you are all set.

Now on line 12 above when you call your function, adelle(alp, letter, alp), you are passing a pointer that points to your alp array in main() twice! Therefore, in your function adelle anything you do to alpnew you are doing to alp back in main(). Likewise for alp, in your function adelle, should you decide to use it.

So in your function adelle, you have two "arrays" (actually pointers) that both only manipulate the alp array back in main().

BTW, the reason the compiler was expecting an expression on line 22 above, alpnew[11]={"ABDJKMPWXZ"}, is because you are specifying the eleventh element of the array, which only holds one character, so the compiler sees the braces and expects an expression that will evaluate to a single character. Also, since the alp array that you are manipulating with alpnew is only 10 characters wide (0 - 9) you are out of bounds by 2 places.

You may want to rewrite most or all of your program to take all of this "clearer" knowledge into account : )

This post has been edited by Mrk: 20 November 2012 - 12:52 AM

Was This Post Helpful? 2
  • +
  • -

#5 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Creating a function that returns a character array

Posted 20 November 2012 - 04:13 PM

Mrk, you're mostly right, though you missed the shadowing issue.
In adelle():
char alpnew[]={"BDJKMPWXZ"};

New local array named alpnew shadows identically named function parameter. From that moment onwards, alpnew now refers NOT to the alpnew in main() scope, but the new, local alpnew shadowing it*.

OP - your compiler should warn you about it, and other issues. If it doesn't, you should set warning levels higher. For GCC that means using these flags, for starters: -ansi -pedantic -Wall -Wextra --std=VERSION where VERSION can be c++03 or for features found in new C++ standard (g++ support) c++11

*) this actually should result in a compilation error if function parameter is hidden by another declaration in the outermost scope of a function. See my post below with standard quotations.

This post has been edited by Xupicor: 21 November 2012 - 05:46 AM

Was This Post Helpful? 1
  • +
  • -

#6 Naz123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-November 12

Re: Creating a function that returns a character array

Posted 20 November 2012 - 05:41 PM

thanks to all who replied. i am closer to getting this done than i was before.
Was This Post Helpful? 0
  • +
  • -

#7 Naz123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-November 12

Re: Creating a function that returns a character array

Posted 20 November 2012 - 05:58 PM

what does "redefinition of formal parameter" mean? i am getting this error and im not sure what to do.
Was This Post Helpful? 0
  • +
  • -

#8 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Creating a function that returns a character array

Posted 20 November 2012 - 06:08 PM

Posting complete error message is usually better than just the fragment of it. Also you could post a line referenced by the message and few lines up too (since sometimes actual mistakes are a line up from the referenced error).

If I'd had to guess it's because you create a local variable with the same name as function parameter, and by doing that - you shadow it, making the parameter unreachable in that scope.
edit: See my below post for quotations of the standard, this should actually always result in error, and not in a warning as I first thought.

void foo(int a) {
    int a; // bam, a shadows parameter a, this should yield an error 
    //...
}

This post has been edited by Xupicor: 21 November 2012 - 05:27 AM

Was This Post Helpful? 1
  • +
  • -

#9 Naz123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-November 12

Re: Creating a function that returns a character array

Posted 20 November 2012 - 06:14 PM

"error C2082:redefinition of formal parameter 'alpnew'" this is the error. i am doing what my professor said. she gave us the prototype of the function which is what i have. so basically what you are saying is that i need to create a new array within the function that has a different name. correct? sorry I'm getting confused with all this. thanks for your help though.
Was This Post Helpful? 0
  • +
  • -

#10 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1302
  • View blog
  • Posts: 4,485
  • Joined: 19-February 09

Re: Creating a function that returns a character array

Posted 20 November 2012 - 06:33 PM

View PostNaz123, on 21 November 2012 - 03:14 AM, said:

"error C2082:redefinition of formal parameter 'alpnew'" this is the error. i am doing what my professor said. she gave us the prototype of the function which is what i have. so basically what you are saying is that i need to create a new array within the function that has a different name. correct? sorry I'm getting confused with all this. thanks for your help though.


Yes, the error means that you are trying to create a variable with the same name as a parameter.

You don't really need another array in the function. The parameters means you have access to two arrays the original and the new.

Perhaps you should try a simpler goal first such as copying the original array to a new array.
Was This Post Helpful? 1
  • +
  • -

#11 Naz123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-November 12

Re: Creating a function that returns a character array

Posted 20 November 2012 - 06:54 PM

okay, i took your advice and i got rid of the unwanted array. i have no errors as of now. Now i have a new problem, i can only get the function to assign a character to an index of the array. this only works for 'A' since it's index is 0. how do i get it to insert the letter in the correct index and then return it? thanks for your help. I greatly appreciate it

this is the code that sort of works.

#include <iostream>
#include <string>
using namespace std;
char adelle(char alp[], char);
int main(){

char letter;
char alp[20]={"BDJKMPWXZ"};
cout<<"Please enter a letter you would like to add to the array:"<<endl;
cin>>letter;

alp[0]=adelle(alp, letter );

cout<<"The new array is: "<<alp<<endl;
	return 0;}
char adelle(char alp[], char character){

for (int i=0; i<17; i++){
	if (character=='a'|| character =='A')
			alp="ABDJKMPWXZ";
		else if (character=='c'||character=='C')
			alp="BCDJKMPWXZ";
		else if (character=='e'|| character=='E')
			alp="BDEJKMPWXZ";
		else if (character=='f'||character =='F')
			alp="BDFJKMPWXZ";
		else if (character=='g'|| character=='G')
			alp="BDGJKMPWXZ";
		else if (character=='h'|| character=='H')
			alp="BDHJKMPWXZ";
		else if (character=='i' || character =='I')
			alp="BDIJKMPWXZ";
		else if (character=='l' || character == 'L')
			alp="BDJKLMPWXZ";
		else if (character=='n' ||character=='N')
			alp="BDJKMNPWXZ";
		else if (character== 'o'|| character== 'O')
			alp="BDJKMOPWXZ";
		else if (character=='q' || character=='Q')
			alp="BDJKMPQWXZ";
		else if (character=='r' || character =='R')
			alp="BDJKMPRWXZ";
		else if (character=='s'||character=='S')
			alp="BDJKMPSWXZ";
		else if (character=='t' || character=='T')
			alp="BDJKMPTWXZ";
		else if (character == 'u' || character== 'U')
			alp="BDJKMPUWXZ";
		else if (character== 'v' || character== 'V')
			alp="BDJKMPVWXZ";
		else if (character== 'y' || character== 'Y')
			alp="BDJKMPWXYZ";
		else 
			cout<<"Please iput a valid character"<<endl;}

return alp[0];}

Was This Post Helpful? 0
  • +
  • -

#12 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1302
  • View blog
  • Posts: 4,485
  • Joined: 19-February 09

Re: Creating a function that returns a character array

Posted 20 November 2012 - 07:15 PM

View PostNaz123, on 21 November 2012 - 03:54 AM, said:

okay, i took your advice and i got rid of the unwanted array. i have no errors as of now. Now i have a new problem,



I said you didn't need another array, but I didn't suggest removing the parameter alpnew.


Here, generally the function wouldn't be aware of the data that the input array contains. Also this is unlikely to work.

20	            alp="ABDJKMPWXZ";




Instead of trying to insert a character straight away, you could try copying from the input array to the output array. Have two different arrays in main and print out the result.

In the function you can use strlen to find the length of the string and use a for loop to copy each character from the input array to the output array.
Was This Post Helpful? 1
  • +
  • -

#13 Naz123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-November 12

Re: Creating a function that returns a character array

Posted 20 November 2012 - 07:40 PM

thank you for your help.
Was This Post Helpful? 0
  • +
  • -

#14 Mrk  Icon User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 66
  • Joined: 03-December 09

Re: Creating a function that returns a character array

Posted 20 November 2012 - 11:58 PM

View PostXupicor, on 20 November 2012 - 05:13 PM, said:

Mrk, you're mostly right, though you missed the shadowing issue.
In adelle():
char alpnew[]={"BDJKMPWXZ"};

New local array named alpnew shadows identically named function parameter. From that moment onwards, alpnew now refers NOT to the alpnew in main() scope, but the new, local alpnew shadowing it.


Xupicor, here is what I don't understand. When I compile code like this:
char adelle(char alp[], char character, char alpnew[])
   {
       char alpnew[]={"BDJKMPWXZ"};


my compiler gives me a "redefinition of alpnew" error, which is what I would expect. I'm using MSVS 2010 express, but apparently other compilers allow the shadowing thing. Is this something that is undefined behavior or is the OP operating under an earlier C++ standard? Or is it something else?
Was This Post Helpful? 0
  • +
  • -

#15 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Creating a function that returns a character array

Posted 21 November 2012 - 04:12 AM

Well, g++ 4.7.2 also throws an error at me, so what other compilers are you talking about?

Still... I can't put my finger on it, but I think there was either a version of g++ or some other compiler that would only warn about it, and not throw an error... I remember having this discussion at some point. Hm.

Well, doesn't matter much, since n3376 under 3.3.3 Block Scope says:

Quote

A name declared in a block (6.3) is local to that block; it has block scope. Its potential scope begins at its point of declaration (3.3.2) and ends at the end of its block. A variable declared at block scope is a local
variable.
The potential scope of a function parameter name (including one appearing in a lambda-declarator) or of a function-local predefined variable in a function definition (8.4) begins at its point of declaration. If the function has a function-try-block the potential scope of a parameter or of a function-local predefined variable ends at the end of the last associated handler, otherwise it ends at the end of the outermost block of the function definition. A parameter name shall not be redeclared in the outermost block of the function definition nor in the outermost block of any handler associated with a function-try-block.

So that's that for C++11. ;)
In C++03 under 3.3.2 Local Scope we get to see the same:

Quote

A parameter name shall not be redeclared in the outermost block of the function definition nor in the outermost block of any handler associated with a function-try-block.

That pretty much settles it, right? ;)
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2