passing arrays (character array)

Hello, I am currently learning how to pass pointers and arrays to functions. I am currently going over passing a character array to a function and I am a bit confused at why it works differently than a normal array.

The code i have put below is for the character array (I have been told that strings are no longer normally done like this but I wish to learn both ways). So where I am confused is, when i pass the character array and print it out in the "cout << "String : " << string << "\n\n"; " line, why is it the entire string that prints, and not just the, firstly the address, since its not being dereferenced, and secondly, why is it not just the very first character of the array?

The book I am using says that when the character array is passed it is only the pointer to the first element that is passed, so I do not understand why I do not need to treat it like I do any other array. (To show what I mean by this, I have posted the code i made to practice the passing arrays below too)
When I pass the array of numbers and print that I only get the first number, I dont get the full array, I need to print that a way myself. Why does a character array work differently?

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
  
void printString(char* str);

int main()
{
	char str[80] = "This is a test";
	char* pointer;

	cout << "Passing without pointer :";
	printString(str);

	cout << "\n\nPassing as pointer : ";
	pointer = str;
	printString(pointer);

	return 0;
}

void printString(char* string)
{
	cout << "String : " << string << "\n\n";
}





Code for array passing
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
void printArr(int a[10]);
void printArr2(int a[]);
void printArr3(int* a);

int main()
{
	int arr[10];

	for (int i = 0; i < 10; i++)
		arr[i] = i+8;

	//cout << "Address of a : " << arr << "\n\n";

	printArr(arr); 
	printArr2(arr);
	printArr3(arr);
}

void printArr(int a[10])
{
	cout << "Value : " << *(a + 3) << "\n\n";	
}

void printArr2(int a[])
{
	cout << "Value : " << *a << "\n\n";
}

void printArr3(int* a)
{
	cout << "Value : " << *a << "\n\n";
}
why is it the entire string that prints, and not just the, firstly the address, since its not being dereferenced

C++ inherited C's "string" concepts and char * is printer as if it were a string when you use cout because cout is coded that way, to allow easy use of C's string type. C++ replaced char* / char array strings with an object, but it retains C support.

It isnt passed any differently: cout is just doing something "special" with it.
you can force it to print the address with a cast: cout << (int)(thecharpointer)


Do you realize that all three of the following function signatures are the same?

1
2
3
void printArr(int a[10]);
void printArr(int a[]);
void printArr(int* a);


Do you realize that all three of the following function signatures are the same?


Yes, the book has taught these 3 different ways of passing the array, and I just wanted to practice them and see how they worked.

thank you for explaining jonnin
Topic archived. No new replies allowed.