Start of array in string?

This is a discussion on Start of array in string? within the C++ Programming forums, part of the General Programming Boards category; How can I get the adress to the first element in the array in a string or a vector? I ...

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Start of array in string?

    How can I get the adress to the first element in the array in a string or a vector?

    I only know this way: &MyString[0]
    Last edited by TriKri; 01-01-2007 at 03:26 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    I don't recommend using addresses of vectors' or strings' elements - there are other ways to do that. check http://www.sgi.com/tech/stl/Iterators.html
    Programming is a form of art.

  3. #3
    Registered User rynoon's Avatar
    Join Date
    Dec 2006
    Location
    London, ON
    Posts
    26
    Quote Originally Posted by hardi
    I don't recommend using addresses of vectors' or strings' elements - there are other ways to do that. check http://www.sgi.com/tech/stl/Iterators.html
    Seconded. If you absolutely have to then I believe you would use something like...

    (void*)&strVal[0]

  4. #4
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Well, I am making a SDL project, and I am setting the caption of the window with this line:

    Code:
    SDL_WM_SetCaption(&Caption[0], NULL);
    Where Caption is a string. The function SDL_WM_SetCaption does not wan't a string, but a const char*, so I thought I may convert it. Caption is set to "Wavetest" when I send "Wavetest" as an argument to another function I have.

  5. #5
    Registered User rynoon's Avatar
    Join Date
    Dec 2006
    Location
    London, ON
    Posts
    26
    Quote Originally Posted by TriKri
    Well, I am making a SDL project, and I am setting the caption of the window with this line:

    Code:
    SDL_WM_SetCaption(&Caption[0], NULL);
    Where Caption is a string. The function SDL_WM_SetCaption does not wan't a string, but a const char*, so I thought I may convert it. Caption is set to "Wavetest" when I send "Wavetest" as an argument to another function I have.
    I see. Have you tried Caption.c_str()?

  6. #6
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Thank you! That was probably what I was looking for.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    It is actually somewhat common to get the address of an element in a vector, because a vector's memory is guaranteed to be contiguous. This allows the vector to be used with API's expecting a C style array. &myVec[0] is the correct way to do it.

    Of course, it is better to use the vector's built-in interface if possible. Also, you must be aware that inserting or erasing from the vector could cause the address to be invalidated.

    A string is not guaranteed to be contiguous, so it is not recommended to do the same thing with a string, and c_str() is of course the correct way to get the const char*.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Next Question...
    By Azmeos in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2003, 02:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21