How to pass type char in function with parameters

Hello everyone!

I'm trying to do somethig like this

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
#include "stdafx.h"
#include <iostream>

class Numbers
{
public:
	int First(int);
private:
	void Second();
};


int main()
{
	int a;

	Numbers Num;

	a = 5;

	Num.First(a);
	system("pause");
	return 0;
}

int Numbers::First(int a)
{
	std::cout << "In Function First\n";
	std::cout << "a:\t" << a << std::endl;

	Second();

	return 0;
}

void Numbers::Second()
{
	std::cout << "In Function Second";
}


As we can see, I pass from main to function 'First' a parameter type int.
Now lets pretend I want to pass from function 'First' to function 'Second' a type char, how I do this?

This example is working great but I want instead of pass a int, pass a char.

For the char I want to read from a note pad using fread, then allocate the text readed in a type char then pass it as parameter.

Thanks!

btw this is my first post (:
and Congratulations about the tutorials, excellent tutorials in this page.

And one question, If I would like to share my codes here it is permitted here in this forum? So people can use it if they need.

Or here is just for answer questions?

Sry Im new :P ! & Thanks!
It's the same as how you did it with an 'int'; just replace 'int' with 'char'.

Sure, you can share your code. If you want it reviewed you could put it in one of the applicable programming sections or the lounge.
Thanks cause the answer Zhuge,

I just tried as u said replace the int with char and result like this:

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
#include "stdafx.h"
#include <iostream>

class Numbers
{
public:
	void First(char);
private:
	void Second();
};


int main()
{
	char a[20] = "hola";

	Numbers Num;

	Num.First(*a);
	system("pause");
	return 0;
}

void Numbers::First(char a)
{
	std::cout << "In Function First\n";
	std::cout << "a:\t" << a << std::endl;

	Second();

//	return 0;
}

void Numbers::Second()
{
	std::cout << "In Function Second\n";
}


and the program output is:
In Function First
a:      h
In Function Second


It means that only the first letter was passed by the parameter, how can I fix this? what I need to use? pointers? if the answer is pointers, how I use pointers in parameters? Thanks!
When you do *a that is the same as a[0], or just passing the first character in the array. If you want to pass the whole array change the parameter of the function to char[] a or char* a and simply use the array name alone when calling the function.
Thanks! Now it works

The result is this:

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
#include "stdafx.h"
#include <iostream>

class Numbers
{
public:
	void First(char*);
private:
	void Second();
};


int main()
{
	char a[20] = "hola";

	Numbers Num;

	Num.First(a);
	system("pause");
	return 0;
}

void Numbers::First(char*a)
{
	std::cout << "In Function First\n";
	std::cout << "a:\t" << a << std::endl;

	Second();

//	return 0;
}

void Numbers::Second()
{
	std::cout << "In Function Second\n";
}


and wow, I'm amazed because the fast answers,
thanks! I hope I can help people too just as u do!
This question is already solved, but I dont exactly understand how it works,
when we use the next code, that are pointers or we are just using arrays?
1
2
3
4
5
class Numbers
{
public:
	void First(char*);
};


1
2
3
4
void Numbers::First(char*a)
{
...
}
A pointer and an array are (almost) exactly the same in terms of the language. When you pass an array to a function like you did, it automatically decays into a pointer. This is why you can pass an array of X it to a function that expects a pointer to X.

Just note that a pointer to does not *have* to point to an array; it can also just point to a singular object. If it helps you can sort of think of it as an array with only 1 element.
I recommend you read on Pointers and arrays here http://cplusplus.com/doc/tutorial/pointers/

But essentially an array "is a" pointer, in fact it's a constant pointer.
Last edited on
ok, thanks about the advice!
Last edited on
Topic archived. No new replies allowed.