Array?

What is the size of the character array s1?

char s1[MAX] = "susan";
eleventeen.
closed account (Lv0f92yv)
Try using "strlen(s1)", after declaring the variable as
char* s1;
then
s1 = "susan";

I would expect this to return 5. (remember you cannot access s1[5], since array indices start at 0). It would look like this:

s1[0] = s
s1[1] = u
s1[2] = s
s1[3] = a
s1[4] = n
Last edited on
yes, it is 5, trust me.
What is the size of the character array s1?

char s1[MAX] = "susan";


The size of the array is MAX .

The string length is 5 - which is NOT the same thing as the size of the array.
Last edited on
Now what is its size???

char s1[] = "susan";

no, it is not 5, trust me.
closed account (Lv0f92yv)
The string length is 5 - which is NOT the same thing as the size of the array.

Yes - I didn't read the OP's post carefully. strlen(array) returns the number of characters (assuming array is a character array) are in the array.

Edit:

A simple program that might help:

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

int main()
{
	const int MAX = 10; //must be constant value, so compiler knows it will not change.
	char s1[MAX] = "susan";

	cout << s1 << "\n";
	cout << sizeof(s1) << "\n"; //is the size of the array s1[], which is defined as MAX.
	cout << strlen(s1); //the number of characters in 'susan' - 5.

	getchar();
	getchar();
	return 0;
}
Last edited on
I see that the first 3 replies are totally wrong, to correct those and defend the post below those three (guestgulkan):

The size of the array in elements is MAX, in other words, the array contains MAX number of elements.
A char can hold 256 different values, so you may say that the size is MAX * 8 bits = MAX bytes.
If you want to know how much space in memory s1 will hold for your elements, you can use the following snippet:
 
int size = sizeof(char) * MAX;

Where size will hold the value that represents the number of bytes.
Do not think a char as a value that holds 1 byte of memory, it may be 4 bytes on an other compiler/os.
what about the null character at the end '\0' ??
Topic archived. No new replies allowed.