hi there, i have this problem: i need to know the size of the argument strings of the program to be able to dinamicaly create the arrays for a command line and knot been limited to a fixed number of using useless extra memory.
tryed with *argv, argv[1], *argv[1] and none gave me the correct result.
googled anyware but it seems noone did this before....
isn't there a way to do this without having to use more arrays and strcpy?
sizeof gives you the size in bytes of the type you give it. It does not give you the string length. It does not detect allocated buffer sizes. It just looks at the given type and says how big it is.
argv is a pointer. Therefore sizeof(argv) returns the size of a pointer (commonly 4)
master roshi has the right idea in his post.
Shadow Addict almost has the right idea, but it doesn't work in this situation. sizeof(a)/sizeof(*a) only works if 'a' is an array. The problem is that argv is not an array, but is a pointer.
thanks for the replies, the case was what roshi said. I didn't know of strlen() so thanks again :P
edit: thanks disch for the explanation, i know that it gives the size in bytes but as a char is 1 byte i thought that if "abc" are three characters, it would be 3 bytes long (and it is) but what i didn't know was that it didn't work with pointers. :P
When passed as a parameter to a function, yes. char* argv[] is the same as char** argv.
But that just means they're both pointers. Neither of them are arrays.
If argv is an array of pointers to chars, doesn't that mean it's also an array of char arrays?
No, it's neither of those things. It's a pointer to a pointer to a char.
It just so happens that it points to the first element in an array, so it's kind of like it's pointing to an array. However the compiler (and therefore sizeof()) has no way to know this. Nor does it have any way to know how large the array being pointed to is.
Essentially, sizeof() can only give you the size of an array if you give it an array. argv is not an array, it's a pointer.
just want to add additional question Disch, consider this int * p = newint[10];, i wonder how does the compiler figures out how long the array was if you delete it, delete[] p;
i wonder how does the compiler figures out how long the array was if you delete it
The compiler doesn't figure it out. The delete[] implementation does.
Typically, when you new[] something, it actually allocates a little more memory than you request, and the extra memory is used to record the size of the allocated array so that it can be deallocated later.
For example -- say you do this: int* p = newint[10]; It might actually allocate room for 11 ints (the first one keeps track of how many ints were allocated), and then it actually gives you a pointer to the 2nd integer in the array.
However the exact details of how it works can vary depending on the implementation, and it's not something you should try to manipulate in your code.
No, it's neither of those things. It's a pointer to a pointer to a char.
It just so happens that it points to the first element in an array, so it's kind of like it's pointing to an array. However the compiler (and therefore sizeof()) has no way to know this. Nor does it have any way to know how large the array being pointed to is.
Essentially, sizeof() can only give you the size of an array if you give it an array. argv is not an array, it's a pointer.