-1

For example,

double myArray[3] = {0.0};

myArray["SSN_1"] = 2.0;
myArray["SSN_2"] = 3.0;
myArray["SSN_3"] = 2.0;

for(... how? ...)
{
... Print ... but, how? ...
}

If you have any reference, please link.

2
  • 3
    No it is not.... Commented Apr 23, 2017 at 21:35
  • 2
    With 4k rep, I would have expected a basic search to have been your first port of call. Commented Apr 23, 2017 at 21:39

1 Answer 1

2

Not in it's not possible, only integers are allowed.

Associative containers can be built with a simple struct like

struct double_map_item {
    char *key;
    double value;
};

take a look at bsearch()'s manual page example for a simple method to find items through their key values.

Of course, this is a very simplistic possible implementation, for a more complete and robust implementation you should read about hash tables.

Regarding this comment, the type of a character constant in c is int and so

array['x'] = value;

is valid, but note that this is not really useful because,

  1. You can only use a single character.
  2. You will probably use an array with a size larger than needed.
  3. You only have a very limited set of possible indices.

NOTE: following your own comment, there is a chance that you saw c++ code. In c++ you can overload the [] operator so that it takes const char * as a parameter and then use a hash table or any other method to find the matching element for the given key.

But in c, it is not possible.

If one were to implement such a container in c++, the following example illustrate how to proceed, please note that this is just for illustration of the underlying concept, you should use std::map instead,

#include <iostream>
#include <map>

class Array {
public:
    Array();
    double operator[](const char *const key);
    void insert(const char *const key, double value);

private:
    std::map<const char *, double> m_items;
};

Array::Array()
{
}

double
Array::operator[](const char *const key)
{
    return m_items[key];
}

void
Array::insert(const char *const key, double value)
{
    m_items.insert(std::pair<const char *, double>(key, value));
}

int
main(void)
{
    Array items;

    items.insert("SSN_1", 2.0);
    items.insert("SSN_2", 3.0);
    items.insert("SSN_3", 2.0);

    std::cout << items["SSN_1"] << std::endl;
    return 0;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Characters are an integer type, so they could work as indices (albeit not in a very useful way in most circumstances).
I think I saw somewhere, probably in string search algorithm implementations.
But not c, c++ because you can overload the [] operator.
@IharobAlAsimi, oh, I get it. Can u give an example?
@anonymous Sorry but I am not that good with c++, and whenever I have tried to implement such operator overloading I always get stuck and have trouble, that's one of the reasons that I don't like c++, because it's impossible to really master it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.