Teach me how to use char array and pointers C++

I am trying to get the user to enter a sentence, then I change that sentence to say hi.
My error - arguement char not compataible with paramater char
If you see any other errors please let me know.
If you can show me how it should be done, I would appreciate it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
using namespace std;

void input(char*code1[])
{	
	*code1[100];
	cout <<"enter your sentence" << endl;
	cin >> *code1[100] ;
	cout << *code1[100] << endl;

}

int main()
{
	char output[100];
	output[0] = 'h';
	output[1] = '1';
	output[2] = 0;
	input(&output[2]);
	system("pause");
}
Last edited on
What is it *code1[100]; ?!
I was use Visual. char code1 needs to be initiated. Then I notice
cin >> *code1[100] ; does the same thing?
I asked you the question. What does mean statement *code1[100]; ?!
Last edited on
closed account (zb0S216C)
tripline wrote:
char*code1[]

That's an array of pointers to chars; implicitly equivalent to char**. Remove the asterisk.

Edit:

Since you seem to know the size of the array beforehand, I recommend that you pass the array by reference:

1
2
3
void input(char(&Code1)[100]) // A reference to an array of 100 characters. 
{
}

Wazzak
Last edited on
Vlad- 100 elements of *Code1 to initialize it?
Framework - Right. I forgot about parentheses. In this, how do I modify this input(&output[2]); ?
At this I thought input
 
(&(output[2]))

but it says cant use char with char?

If I removed the asterick for
 
char*code1[]

It will not be a pointer anymore though? Please bear with me as this is my first time and explain how the 2 pointers came about when I only used one *
Last edited on
closed account (zb0S216C)
tripline wrote:
"It will not be a pointer anymore though?"

It was never a pointer to an array anyway; it was an array of pointers.

By passing the array by reference, you don't need to explicitly specify the address. For example:

1
2
3
4
5
6
7
8
9
10
11
12
void Function(int(&ArrayOf100)[100])
{
    // ...
}

int main()
{
    int MyArray[100] = { 0 };
    
    // Pass the array to Function()
    Function(MyArray);
}

Note the absence of the address-of operator. References do not require you to pass it the address. Passing by pointer (or reference) requires you to pass the address. For example:

1
2
3
4
5
6
7
8
9
10
11
12
void Function(int(*ArrayOf100)[100])
{
    // ...
}

int main()
{
    int MyArray[100] = { 0 };
    
    // Pass the array to Function()
    Function(&MyArray);
}

Not the presence of the address-of operator.

Wazzak
Last edited on
As i understand correctly you want to get a user string prefixed with "h1'.

1
2
3
4
	char output[100];
	output[0] = 'h';
	output[1] = '1';
	output[2] = 0;


This is accomplished the following way

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
#include <iostream>
#include <cstdlib>

using namespace std;

void input( char *s )
{	
	cout <<"enter your sentence" << endl;
	cin >> s ;

}

int main()
{
	char output[100];

	output[0] = 'h';
	output[1] = '1';
	output[2] = 0;

	input( &output[2] );

	cout << output << endl;

	system("pause");
}
Last edited on
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
#include <iostream>
#include <string>
using namespace std;


void input(char *code)
{	
	
	cout <<"\n\n enter your sentence (this is the input fn)" << endl;
	cin >> code;
	

}



int main()
{
	char output[20];
	
	input(output);
cout<<"\n\n enter the sentence (this is the main fn)";
	cin>>output;

	cout << output << endl;
	system("pause");
}
Last edited on
Don't mess with all the character arrays. You have #included <string>, so use it:

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 <cctype>  // for tupper()
#include <iostream>
#include <string>
using namespace std;

int main()
  {
  // This is where we will keep the text that the user gives us:
  string s;

  // Ask the user for text and get it:
  cout << "Enter your sentence: ";
  getline( cin, s );

  // As this is an example, let's make the output SHOUTING:
  for (size_t n = 0; n < s.length(); n++)
    {
    s[ n ] = toupper( s[ n ] );
    }

  // Display the changes we made to the user's input:
  cout << s << endl;

  return 0;
  }

Hope this helps.
Topic archived. No new replies allowed.