Thread: Saving text into char* vectors

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291

    Saving text into char* vectors

    Hello, I have a problem with char* vectors. Let me explai with an example of what I mean:
    Code:
    #include <conio.h>
    #include <stdio.h>
    #include <vector>
    int main()
    {
    vector<char*>vvv;
    char txt[260];
    sprintf(txt,"my");
    vvv.push_back(txt);
    sprintf(txt,"vector");
    vvv.push_back(txt);
    sprintf(txt,"test");
    vvv.push_back(txt);
    for(int q=0;q<vvv.size();q++)
        {
        printf("%d-. %s\n",q,vvv[q]);
        }
    getch();
    return 0;
    }
    At the end, it writes 'test' 3 times, so I think that what happens is that I save a poiter to the char txt[260], so every rename also renames the vector content.
    How can I save 'independent' text into char* vectors?
    Thank's in advance
    Niara

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    The easy solution is to just use std::string instead of char*.

    You save three pointers, but all three are pointing to the same memory block, that you modify. In the end, all pointers point to the block that finally holds "test".

    If you want a seperate chunk of memory for each of your pointers, you will have to use functions like malloc/calloc or new to create a new chunk of memory for each of your pointers. The drawback is that you have to remember what you created from scratch and free/delete it when you no longer need it.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    Hello nvoigt, thank's for your time and help. I find the way to rut it with std::string, but it seems a little confused. That's how it works:
    Code:
    #include <conio.h>
    #include <stdio.h>
    #include <vector>
    #include <string>
    
    int main()
    {
    std::string txt="my";
    std::vector<std::string>vvv;
    vvv.push_back(txt);
    txt = "vector";
    vvv.push_back(txt);
    txt = "test";
    vvv.push_back(txt);
    
    std::vector<std::string>::iterator dadesVector;
    for(dadesVector = vvv.begin(); dadesVector != vvv.end(); dadesVector++)
        {
        std::string strD = *(dadesVector);
        cout<<strD<<endl;
        }
    
    getch();
    return 0;
    }
    It works exactly as I expected. But there's something I still see clearer: the next piece of code gives an error:
    Code:
    for(int q=0;q<vvv.size();q++)
        {
        std::string vtxt = vvv.at(q);
        cout<<vtxt.c_str()<<endl;
        }
    The error is:
    Code:
    no matching function for call to `vector<basic_string<char,string_char_traits<char>,
    __default_alloc_template<false,0> >,allocator<basic_string<char,string_char_traits<char>,
    __default_alloc_template<false,0> > > >::at (int &)'
    I'm doing it in DevCpp with the MingW default compiler.
    Where can be the problem? (note: the iterator method to access to data works well, but I would like to know if that's problem of compiler, includes, I don't know whatever else...).

    Thank's.
    Niara

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Your code works fine with g++ 3.3.5
    Kurt

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> cout<<vtxt.c_str()<<endl

    You can just use std::cout<<vtxt<<std::endl without the .c_str(), but you should be including <iostream>.

    I'm not sure why that error would appear, the call to at is correct. Technically at() takes a vector<string>::size_type variable instead of an int, but it should be converted correctly (the size_type thing is usually just a typedef for an unsigned integral type). Try size_t instead of int for q.

    If that doesn't work, post the entire program with includes and everything and give the full error message and line number.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    Hey, thank's both Zuk and Daved for your replies.
    I haven't idea of what can it be, I have find lots of samples (maybe lots isn't the right word, but several yes) that works in that way (of course not in my compiler); also I have that problem when I declare a vector of 'int', and because of that I usually use the [position] to extract the values.
    Well, for the moment that not seems a problem since I can use the iteration from 'begin' to 'end' instead the from 0 to vector size, I think that be the same.
    Regards.
    Niara

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Determining the Date of the [nth] [Day] in [Month]
    By Dave_Sinkula in forum C Programming
    Replies: 5
    Last Post: 05-25-2007, 04:12 PM
  2. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Replies: 1
    Last Post: 07-13-2002, 05:45 PM
  5. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM