Problems with a string arraay..

This is a discussion on Problems with a string arraay.. within the C Programming forums, part of the General Programming Boards category; What is thr proper way to have an array of strings w/o having to deal with malloc and such.. i've ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    2

    Problems with a string arraay..

    What is thr proper way to have an array of strings w/o having to deal with malloc and such.. i've tried several varients.. include these which all segfault..
    Code:
    char *randusername()
    {
            char *names[2][12] = { {"ltorvalds"}, {"foobar"} };
    
            return names[0][0];
    
    }
    
    char *randusername()
    {
            char *names[2][12] = { {"ltorvalds"}, {"foobar"} };
    
            return names[0];
    
    }
    char *randusername()
    {
            char *names[2][12] = { "ltorvalds", "foobar" };
    
            return names[0][0];
    
    }
    char *randusername()
    {
            char *names[2][12] = { "ltorvalds", "foobar" };
    
            return names[0];
    
    }
    what is the correct way to do this..



    Thanks..

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    they all segfault because you are trying to return a pointer to a destroyed object. remember the rules of scope. You make an array on the stack then return its address then leave function which destroys array. you could make it static which will solve the problem or make an array in a higher func and pass it into the pickrandname func. also u return the pointer.... return array[x]; not the char return array[x][y];
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    2
    i hate to sound stupid, but how should the array be declared... ie, which one of the options i used above.. because i've tried both of those whilst moving the array into the global scope of things.

    anway.. it still segfaults..

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you must return a pointer to them do one of two things:

    1) Make them global arrays (called tables) and just make your functions lookup function.

    2) Use the keyword static to prevent the memory from being destroyed:

    static char table[2][12] = .....


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 10:20 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 10:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM

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