I have been teaching myself c++ for a little while now and came across something I couldn't figure out. I am trying to figure out how many char*s there are in a char**. In the current program I am using *argv[], so I can access it through argc, but is there a way to find that number without a second variable in case I do not have access to something like argc in another variable? Second, can you overload the assignment operator for the string class? I want to overload = for a string* so that it can be assigned a char**. Thank you for any help you may offer.

#include <string>
#include <stdlib.h>
using namespace std;
/* operator here??
string string::operator= (char*);*/



int main(int argc,char *argv[])
{
    string * test1 = new string[argc];
//replace next 2 lines with single statement using = operator
    for (int a = 0; a < argc; a++)
        test1[a] = argv[a];
    for (int b = 0; b < argc; b++)
        printf("argv[%d] : %s\nstring[%d] : %s\n",b,argv[b],b,test1[b].c_str());
}

In the current program I am using *argv[], so I can access it through argc, but is there a way to find that number without a second variable in case I do not have access to something like argc in another variable?

Maybe yes, and maybe no. It all depends on how the pointer was written. If the array of pointers ends with a NULL pointer, then just iterate until NULL is reached. But there is no guarentee that any given array of pointers will work like that. In the case of argv the only way is to use argc. Example:

char *ay[3];
ay[0] = "Hello";
ay[1] = "World";
ay[2] = NULL;

int i;
// now print all the strings in the array
for(i = 0; ay[i] != NULL; i++)
   cout << ay[i] << "\n";

Another method that may be sometimes useful is to use sizeof operator to calculate the number of pointers in the array

char* ay[] = {"Hello", "World",NULL};
int aySize = sizeof(ay) / sizeof(ay[0]);

But that does NOT work like this

void foo(char *ay[])
{
int aySize = sizeof(ay) / sizeof(ay[0]); <<< wrong
}

Alright, thank you for your help. What I was trying to do was access the number of elements in the array to iterate the pieces of the char* array into the string* in order to set up the overloaded = operator. I know how to overload operators, but how do you access the operators in the string class in order to overload it or is that possible? Thanks for your help.

I tried using sizeof() and all occurrences returned 4. sizeof(argv), sizeof(argv[0]) and sizeof(argv[1]) all returned four resulting in a size of 1 even though argc was equal to 5.

int main(int argc,char *argv[])
{
    string * test1 = new string[argc];
    for (int a = 0; a < argc; a++)
        test1[a] = argv[a];
    for (int b = 0; b < argc; b++)
        printf("argv[%d] : %s\nstring[%d] : %s\n",b,argv[b],b,test1[b].c_str());
    int test = (sizeof(argv) / sizeof(argv[0]));
    cout << sizeof(argv) << endl << sizeof(argv[0]) << endl << sizeof(argv[1]) << endl;
    if (test == argc)
        printf("Success, argc equals test");
    else
        printf("Failure, not equal\nargc : %d\ntest : %d",argc,test);
}

I ran that with arguments Test 1 2 3 and it returned

argv[0] : /home/files/Documents/c++/scripts/test/bin/Debug/test
string[0] : /home/files/Documents/c++/scripts/test/bin/Debug/test
argv[1] : Test
string[1] : Test
argv[2] : 1
string[2] : 1
argv[3] : 2
string[3] : 2
argv[4] : 3
string[4] : 3
4
4
4
Failure, not equal
argc : 5
test : 1

I tried using sizeof() and all occurrences returned 4. sizeof(argv), sizeof(argv[0]) and sizeof(argv[1]) all returned four resulting in a size of 1 even though argc was equal to 5.

Re-read my previous post carefully about that sizeof operator -- I even said you can not use it for pointer arrays that are parameters to functions, such as argv. Why? Because sizeof(any pointer) = 4. All that is doing is taking the size of a pointer, not the size of an array of pointers.

Instead of an array of strings why don't you make a vector of strings. With vectors you don't have to know how many strings there are in advance.

int main(int argc, char* argv[])
{
    vector<string> ay;
    for(int i = 0; i < argc; i++)
        ay.push_back(argv[i]);
}

I know how to overload operators, but how do you access the operators in the string class in order to overload it or is that possible? Thanks for your help.

I suppose here is one way to overload the string's = operator. I'm certain others can give you even better examples.

template <class T>
class MyString : public std::string
{
public:
    MyString& operator=(MyString& ms)
    {
        cout << "Hello World\n";
        return *this;
    }
};

int main()
{
    MyString<string> s1;
    MyString<string> s2;

    s1 = s2;

}
Comments
Hooray for vectors
This article has been dead for over six months. Start a new discussion instead.