开发者

Array of char *

I am having problems with array pointers. I've looked through Goog开发者_JS百科le and my attempts are futile so far.

What I would like to do is, I have a char name[256]. I will be 10 of those. Hence, I would need to keep track of each of them by pointers.

Trying to create a pointer to them.

int main()
{
    char superman[256] = "superman";
    char batman[256] = "batman";
    char catman[256] = "catman";
    char *names[10];
    names[0] = superman;
    names[1] = batman;
    system("pause");
    return 0;
}

How do I actually traverse an array of pointers?


names[0] is a char* to whatever you stored in names[0] (which in this case is a pointer to the first element in your superman array) thus your guess at e.g cout << names[0] << endl;is correct.

If you want to traverse that array, you need to know when to stop so you're not traversing pointers you havn't yet initialized- if you know you have initialized 2 of those pointers, you could do e.g.

for(int i = 0; i < 2 ; i++) {
  std::cout << names[i] << std::endl;
}

As an alternative, place a NULL pointer after the last element you have initialized(make sure there's room for that NULL pointer) e.g.

names[2] = NULL;
for(int i = 0; names[i] != NULL ; i++) {
  std::cout << names[i] << std::endl;
}


why not use strings and a Vector of strings to store the names? smpl:

#include <string>
#include <iostream>
#include <Vector>

//using namespace std;

int main(void) {
    std::string superman = "superman";
    std::string batman = "batman";
    std::vector<std::string> names;
    names.push_back(superman);
    names.push_back(batman);
    for (unsigned int i = 0; i < names.size(); ++i) {
        std::cout << names[i] << std::endl;
    }
    char c; std::cin >> c;
}


char *names[] = { "superman", "batman", "whatever", NULL };

...

for (int i = 0; names[i] != NULL; i++)
    printf("%s\n", names[i]);

He might not want to use a vector because he might be using C, not C++.

edit: I see he tagged it C++ though.


Using arbitrary fixed length arrays to manipulate strings is a complete no no. In my company, this code would be illegal, period. This practice is exactly the cause of most security breaches and it's what makes C/C++ (that uses this type of code) notoriously unsecure. I highly recommend the C++ solution from "Oops".


First try using std::string, this will relieve you of memory allocation and deallocation issues.
Second, use std::vector<string> which dynamically expands as needed.

If you must use char *, you will need an array of pointers to char *.
This is declared as:

char * array_of_C_strings[10]; // Define an array of 10 pointers to char *.

If the strings are fixed length:

char array_of_fixed_length_C_strings[10][256]; // Array of 10 C-Strings that are max. size 256.

Assignment:

char hello[32];
strcpy(hello, "Hello");
array_of_C_Strings[0] = hello;  // Note: only pointers are copied
strcpy(array_of_fixed_length_C_Strings[2], hello);  // Copy actual content of string.

With std::string and std::vector<std::string>:

std::string hello = "hello";
std::vector<std::string> string_container;
string_container.push_back(hello);
string_container.push_back("world!");
std::cout << string_container[0]
          << ' '
          << string_container[1]
          << "\n";

The example using std::string and std::vector looks simpler than an array of char *, but that is my opinion, YMMV.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜