Thread: simple array of char array problem

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    100

    simple array of char array problem

    hi all,

    I have a global array of char array like so
    Code:
    char *myArray[100];
    I then set initialise it with memset

    Code:
    memset(myArray, '\0', sizeof(myArray));
    Now the problem is that I have a function which is called multiple times which assigns strings the myArray like so (obviously this is a simplified version)

    Code:
    void myArrayAssignment(int i){
    char buf[] = "hello world";
    myArray[i] = buf;
    }
    Now the problem is that when I leave the myArrayAssignment function the variable "buf" becomes out of scope so the indexs of myArray all point to junk.

    I will need to change myArray from

    Code:
    char *myArray[100];
    to the form of something like this:

    Code:
    char myArray[100][256];
    But i dont know how to assign to the array because C++ doesnt allow for array to array assignments.

    Is there away to do this strcpy()?
    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You might try strncpy()
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    100
    Thanks :-)

    I tried this but with no luck
    Code:
    char myArray[100][256]
    
    void myArrayAssignment(int i){
    char buf[] = "hello world";
    strncpy(myArray[i], buf, strlen(buf));
    }
    Didnt work as i got the following error:
    cannot convert ‘char**’ to ‘char*’ for argument ‘1’

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Are you are asking for this ?
    Code:
    #include <iostream>
    using namespace std;
    
    void assign_array( char arr [][256], int i ) {
        strcpy( arr[i], "Hello world" );
    }
    
    int main() {
       char myArray[100][256];
       int i = 77;
       assign_array( myArray, i );
       cout << myArray[i] << endl;
    }
    Kurt

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    No, but he may be asking for this
    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    void assign_array( char arr [][256], int i ) {
        strcpy( arr[i], "Hello world" );
    }
    
    int main() {
       char myArray[100][256];
       int i = 77;
       assign_array( myArray, i );
       cout << myArray[i] << endl;
    }
    strcpy() is in <cstring>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Why make an array of char * instead of a vector of string?
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Char array problem.
    By kbro3 in forum C++ Programming
    Replies: 16
    Last Post: 08-21-2008, 01:31 PM
  2. simple char array question
    By yahn in forum C++ Programming
    Replies: 4
    Last Post: 02-08-2006, 09:18 PM
  3. Problem Copying char array
    By NullStyle in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2004, 08:06 AM
  4. help with array of char and char **
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 04-20-2002, 02:23 PM