map in C works?

Asked

Viewed 2,209 times

0

I would like to know if it is possible to use map in C. I know it works in C++, but in C it always gives build error. If someone has already used map in C, it could show me how to use?

#include <map>
#include <stdio.h>

int main()
{
    map<int, char[10]> m;

    m[1] = "Um";
    m[2] = "Dois";
    m[4] = "quatro";

   printf(m[4]); // não sei se precisa de mascara de dados
}
  • The map type does not exist in C, only in C++. What you could do is create your own type map.

  • The way you are I don’t think even in C++ will work, you need to write for example using namespace std;, and the form of insertion is wrong, should be using the insert().

  • Vlw Galera, already suspected that it did not work in C. I think map works only in object-oriented languages, I used in java but had never tested in C. Thanks for answering.

  • I don’t think you need to reinvent the wheel. Similar solutions already exist in C. See: http://stackoverflow.com/questions/647054/porting-stdmap-to-c

  • These stackoverflow people in the English version invent each complex implementation rs, using struct, pointer, memory allocation, function receiving pointer as parameter. But it’s worth it I didn’t know you could create a map this way in C.

  • Paul, for examples like the one you included in the question, a array in C (which is a very particular case of map ), works perfectly...

  • @Jjoao, I took a look at the documentation of C. And array only works in C++.

Show 2 more comments

3 answers

3

Does not work in the C language. Map is a C language resource++ (details here), since it uses features that are not found in C and are characteristic of object orientation.

What you can do is find some library that performs a similar function in C.

  • vlw by the answer (y)

  • Quiet, @Paulo !

0

Some maps solve perfectly with an array! Regarding your example,

#include <stdio.h>
    int main(){
    char *  m[20];

    m[1] = "Um";
    m[2] = "Dois";
    m[4] = "quatro";

   printf("%s\n", m[4]);
}

works perfectly.

If your indexes are more complex (example strings, etc) you can use Map broken with:

  • hash tables (see man hsearch)
  • binary search trees (see man tsearch)
  • array of pairs (see man lsearch)

go to richer libraries like

  • glib (see google glib)
  • lib db (for map in file,)

or even go to C++ (boost, etc)

  • Vlw @Jjoao for the help, and for the available options you gave me. Best forum there is always someone available to answer

  • Missed to allocate the m, otherwise does not work.

  • @Marcelouchimura, the m is a vector m[20] of pointers: therefore the m has its own space. Each of the pointers will need to point to a "defined" space. If we use m[5] as something that is being changed, you need to allocate space for this string (ex m[i]=malloc(50)) if we just have a constant value C puts these constants in a data area (a when compiling) and this case we don’t need mallocs.

  • m is char**, one dimension is defined (20); the other dimension has no defined size and therefore needs to be allocated.

  • You don’t have to, in case he fills in statements like m[2] = "Dois";

0

As already suggested the best is to implement its map in that case. I can recommend the use of hash table and the use of that lib to C.

Take a look too in that article which explains about hash table.

  • thanks for the help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.