char & pointers

1
2
3
4
5
6
7
8
9
double get_number()
{
	char s[100];

	cin.getline(s, 99);
	if(strlen(s) == 0)
		return 0.0;
	return atof(s);
}


In the C++ book I'M reading chars are defined by char *variableName, but in the preceding code from the same book I see char s[100] without the * character? What have I missed?

In the following code I'M a little confused with the "convert" function. Is it working with the pointer s or the char s?

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
#include <iostream>
#include <string.h>
#include <ctype.h>
using namespace std;

void convert_to_upper(char *s);

int main()
{
	char s[100];

	cout<< "Enter string to convert and press ENTER: ";
	cin.getline(s, 99);

	convert_to_upper(s);
	cout<< "The converted string is:" << endl;
	cout<< s;

	system("pause");
	return 0;
}

void convert_to_upper(char *s)
{
	int i;
	int length = strlen(s);

	for(i = 0; i < length; i++)
		s[i] = toupper(s[i]);
}
Last edited on
The name of an array is converted to the pointer to its first element when it is used in expressions. So when you pass an array as an argument of a function it is converted to the pointer to its first element.
Topic archived. No new replies allowed.