simple array of char array problem

This is a discussion on simple array of char array problem within the C++ Programming forums, part of the General Programming Boards category; hi all, I have a global array of char array like so Code: char *myArray[100]; I then set initialise it ...

  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
    22,118
    You might try strncpy()
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    ZuK
    ZuK is offline
    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,048
    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
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    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, 02: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, 03:23 PM

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