Find the size of a char array

How do you find the size of a character array such as this.

char message [] // function parameter
You can't. You have to pass the size as a parameter.
Perhaps like this:

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

void testMessage(char message[], int sizeOfMessage)
{
     cout << "Message: " << message << endl;
     cout << "Size of message: " << sizeOfMessage << endl;
}

int main()
{
    char m[] = "This is a test message.";
    testMessage(m, sizeof(m));
    
    return 0;   
}

You can't. You have to pass the size as a parameter.


If it isn't dynamic (which in his example, it isn't), I believe you can.

npath's code works, but only for chars. This would work for any array.

1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;
int main() {
   char myArray[10];
   int sizeOfArray = sizeof(myArray) / sizeof(myArray[0]);
   cout << sizeOfArray << endl;
   return 0;
}
Last edited on
Well I have a program like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//timed message function
int timed_message(char message [], bool use_seconds, int time)
{
	for(int x=0; x<=/*SIZE OF MESSAGE ARRAY*/;++x)
        {
                cout << message[x];
        }
}

int main()
{

	timed_message("This message will appear at a steady rate.", false, 100);
	//keep the console open and exit
	cin.get();
	return EXIT_SUCCESS;
}


I need that for loop to only run while there are still characters in the char array. Otherwise it will print some weird crap and freak the program out.

How would I work Tevsky's solution into my function as a parameter?
Last edited on
Ah I'm a fool, I didn't read the "function parameter" part in your first post, and then didn't put two and two together when Helios mentioned "pass as a parameter". Helios is correct, you can't, you have to pass the size as a parameter. My solution only works when you have direct access to the array. You could always get rid of the function, load that message into a char array, and then put the loop in main and use the thing I suggested.

Or simply, use a string. The two of these things are only if you must have access to the individual chars though.
Last edited on
I have two questions:
1. Why didn't you say you were passing a C string (a char array may or may not be a C string)?
2. What's wrong with std::cout <<message;?
Last edited on
Helios -

1. I've had a headache all day, not really thinking clearly and forgot the name. :P
2. Well there's much more to the timed_message function, what it does is show the characters in the array, err.. C String, one by one, and plays a keyboard typing sound, as if someone is typing the letters onto the screen.

It's only a function because I plan to use this many times in the game I am developing. (It's a text based RPG.)



So why not use std::string?
You can print the letters one by one with a string?
Yes.

You can print the letters one by one with a string?


You can, exactly the same way as with a char array. Here's your code with a string, all I did was change the parameter from char array to a string. And then I put in the method message.size() as the loop condition. This function returns how many characters are in the string.

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

using namespace std;

int timed_message(const string& message, bool use_seconds, int time)
{
	for(int x=0; x<=message.size();++x)
        {
                cout << message[x];
        }
}

int main()
{

	timed_message("This message will appear at a steady rate.", false, 100);
	//keep the console open and exit
	cin.get();
	return EXIT_SUCCESS;
}
How about this (Windows only):

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

int timed_message(const string &message, bool use_seconds, int time)
{
    int x;

    for (x = 0; x <= message.size(); ++x)
    {
        cout << message[x];
        if (!use_seconds)
        {
            Sleep(time);
        }
        else
        {
            Sleep(time * 1000);
        }

    }
}

int main()
{
	timed_message("This message will appear at a steady rate.", false, 100);

	cin.get();
	return EXIT_SUCCESS;
}
Topic archived. No new replies allowed.