Hi all,

Quite a basic question for most of you; I am trying to obtain length of a string < char name[MAX_ENTRIES][MAX_NAME_SIZE] > for use in a for loop that will perform isalpha.

for (int j=0; j < &name.length(); j++)

I am getting error 2228: left of '.length' must have class/struct/union type upon compile.

Can anyone please offer me some guidance?

This is a simple win32 console application. See attached code.

Dani AI

Generated

Short answer: the compiler error happens because char name[MAX_ENTRIES][MAX_NAME_SIZE] is a plain C-style array, and raw arrays do not have a .length() member. That method exists on C++ classes like std::string or containers. ’s pointer to the C string-length routine is the right idea for C-style strings, and ’s GetLength() example is C#-specific (not applicable to C/C++).

A clean, modern approach is to use std::string (or a container of strings) so you can call .size() / .length() safely and avoid buffer/termination issues. Example pattern (C++):

#include <string>
#include <cctype>

std::string names[MAX_ENTRIES]; // or std::vector<std::string>

for (size_t i = 0; i < MAX_ENTRIES; ++i) {
    for (size_t j = 0; j < names[i].size(); ++j) {
        char ch = names[i][j];
        if (std::isalpha(static_cast<unsigned char>(ch))) {
            // handle alphabetic character
        }
    }
}

Notes and pitfalls to watch for when staying with char[][]:

  • The number of rows is MAX_ENTRIES (or sizeof(name)/sizeof(name[0]) only if the array is in the same scope; that trick fails once the array decays to a pointer).
  • To get the length of the string in row i, use the C string-length routine (as suggested) or convert the row to std::string. Never call a C routine that walks memory unless the buffer is guaranteed to be NUL-terminated; prefer bounded helpers when available.
  • isalpha takes an int that must be representable as unsigned char or EOF; cast to unsigned char first to avoid undefined behavior for negative char values.

Best practice: prefer std::string / std::vector<std::string> for safety and clarity, and only use raw char[][] when constrained by legacy APIs or embedded limits.

How exactly do you want to get length?!!
The only way I know of is using <string.h> There is a strlen(char *s) function which return the strings length :D

rodkay, actually I can help you in c#. In c# you can use GetLength() to get multi dimensional array. Use like this:

 for (int j = 0; j < myjaggedarraymutidim[i].GetLength(0); j++)
                {
                    for (int k = 0; k < myjaggedarraymutidim[i].GetLength(1); k++)
                    {
                        Console.Write(" "+myjaggedarraymutidim[i][j, k]+" ");
                    }

                    Console.WriteLine();
                }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.