hoy to get the size of array all initialised to as

char pste_msg[64]= {1,2,4,5,6,8,6,5};
memset(pste_msg,0,64);
sizeof(pste_msg) is giving 64
any help????????
thanks.

Your getting 64 because thats the size, you have a couple of options :
1) Use string
2) Use std::vector<char>
3) Append a null character at the end
4) Create a length variable to keep tract

for example :

char msg = "12345"; //null character appended automatically
int length = strlen(msg);
char msg2 = {1,2,3,4,5,'\0'}; //append null character automatically 
int length2 = strlen(msg2);

But to make your like easier, if you can, just use std::string.

sizeof(pste_msg) is giving 64

Yes, because that's the size of the array. You're making a distinction between the memory capacity and the functional size. The capacity is how many items could possibly be stored in the array, while the functional size is how full the array is in terms of real values. The latter isn't directly supported by a magic keyword or function. You'll need to store the current size and update it accordingly if you want to continue using an array.

I'd suggest learning how to use vectors, since after doing so you'd have a more powerful array that, once learned, is easier to use.

With a vector, you could just write:

mArray.size();

And there ya go.

also use using namespace std; to get rid of the std:: whatever you had. it makes the code somewhat smaller and a little easier to use.

also use using namespace std; to get rid of the std:: whatever you had. it makes the code somewhat smaller and a little easier to use.

There are caveats to a using directive. While more verbose, the std:: prefix is always safe.

but doesnt using namespace std; clear his std:: whatever??? since it is an origional std definition??? or declaration???

but doesnt using namespace std; clear his std:: whatever???

It eliminates the need for a std:: prefix by importing all of the names in the std namespace to the current scope. That can be a bad thing because you're not likely to use all of the names that were imported, and you might be using other libraries that use the same names. Using directives (when placed at file scope) defeat the purpose of namespaces.

It eliminates the need for a std:: prefix by importing all of the names in the std namespace to the current scope. That can be a bad thing because you're not likely to use all of the names that were imported, and you might be using other libraries that use the same names. Using directives (when placed at file scope) defeat the purpose of namespaces.

To just add to this, you can also use something like:

#include <iostream>
#include <vector>

int main()
{
    using std::cout;
    using std::endl;
    using std::vector;

    vector myVec;
    cout << "Hello World!" << endl;
    return 0;
}

This way, you can choose which things you don't need to specify the scope for, so you don't completely ruin the point of having a namespace. (I think)

Edited 5 Years Ago by ravenous: changed code

To just add to this, you can also use something like:

#include <iostream>
#include <vector>

int main()
{
    using std::cout;
    using std::endl;
    using std::vector;

    vector myVec;
    cout << "Hello World!" << endl;
    return 0;
}

This way, you can choose which things you don't need to specify the scope for, so you don't completely ruin the point of having a namespace. (I think)

You can also have a using directive at a tighter scope:

int main()
{
    using namespace std;

    // ...
}

Which is why I said that there are caveats, but a std:: prefix always works without exception.

Yeah, because it is 64. This is a statically allocated 64 bytes of memory, you've initialized first 8 bytes, the rest of 56 bytes are either zero or undefined, depending on the compiler.

hoy to get the size of array all initialised to as

char pste_msg[64]= {1,2,4,5,6,8,6,5};
memset(pste_msg,0,64);
sizeof(pste_msg) is giving 64
any help????????
thanks.

This article has been dead for over six months. Start a new discussion instead.