Results 1 to 5 of 5
  1. #1
    258456's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    ghjghj
    Posts
    1,222
    Reputation
    18
    Thanks
    300
    My Mood
    Relaxed

    MULTI-DIMESIONAL C-Style Strings

    I tried to use multi-dimensional c-style strings but i keep getting into corners in which i can't get out, so if somebody can explain them for me i would appreciate it.


    I have been using it like this:

    Code:
    char szString[3][40];
    also i have read that this is the same:

    Code:
    char **szString = 0;
    I have no idea how to put the strings in it. Thanks in advance.

  2. #2
    Jason's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    /dev/null
    Posts
    5,704
    Reputation
    918
    Thanks
    7,677
    My Mood
    Mellow
    If you're using the 2nd option you need to dynamically allocate the memory for the char**.

    i.e, say you're going to store....20 strings at this stage:

    Code:
    char **strArray = (char**)calloc(20, sizeof(char*));
    So now the top level has been allocated, but there has been no room actually allocated for strings themselves, just the pointers to the strings. To produce a valid pointer you can do it several ways, the easiest being strdup, it will duplicate an existing string into heap memory and give you a pointer to that string, you can then add that pointer to your array:

    For example:

    Code:
    char **strArray = (char**)calloc(20, sizeof(char*));
    char *derp = "literal static string";
    //now, i don't want to risk the "derp" variable being collected or modified somewhere during runtime, so I'll create my own copy of the string (not the pointer to the string, the string itself.
    strArray[0] = strdup(derp);
    It might be easier to envision what this is doing when you consider:

    Code:
    char** == *char[]
    So essentially you have an array of pointers-to-string.

    As always, pointers can be a mindfuck.

    Quote Originally Posted by Jeremy S. Anderson
    There are only two things to come out of Berkley, Unix and LSD,
    and I don’t think this is a coincidence
    You can win the rat race,
    But you're still nothing but a fucking RAT.


    ++Latest Projects++
    [Open Source] Injection Library
    Simple PE Cipher
    FilthyHooker - Simple Hooking Class
    CLR Injector - Inject .NET dlls with ease
    Simple Injection - An in-depth look
    MPGH's .NET SDK
    eJect - Simple Injector
    Basic PE Explorer (BETA)

  3. The Following 3 Users Say Thank You to Jason For This Useful Post:

    258456 (03-29-2012),Hassan (03-29-2012),t7ancients (03-29-2012)

  4. #3
    258456's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    ghjghj
    Posts
    1,222
    Reputation
    18
    Thanks
    300
    My Mood
    Relaxed
    Sorry for late reply. Thanks Jason that makes sense. And ya, as usual pointers are bein annoying, lol.


    The only thing annoying me now is that this line is giving me an Assertion error:

    Code:
    sprintf(szServerRequestArray[i - 2], "somestring %i some string %i some string", WhichOne, i);
    Last edited by 258456; 03-29-2012 at 09:56 PM.

  5. #4
    Jason's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    /dev/null
    Posts
    5,704
    Reputation
    918
    Thanks
    7,677
    My Mood
    Mellow
    Quote Originally Posted by 258456 View Post
    Sorry for late reply. Thanks Jason that makes sense. And ya, as usual pointers are bein annoying, lol.


    The only thing annoying me now is that this line is giving me an Assertion error:

    Code:
    sprintf(szServerRequestArray[i - 2], "somestring %i some string %i some string", WhichOne, i);
    Im guessing you never allocated any memory for szServerRequestArray[i - 2] to point to. Personally I'd use a buffer and then duplicate.

    Code:
    char buffer[256];
    //other code
    sprintf(buffer, "somestring %i some string %i some string", WhichOne, i);
    szServerRequestArray[i - 2] = strdup(buffer);

    Quote Originally Posted by Jeremy S. Anderson
    There are only two things to come out of Berkley, Unix and LSD,
    and I don’t think this is a coincidence
    You can win the rat race,
    But you're still nothing but a fucking RAT.


    ++Latest Projects++
    [Open Source] Injection Library
    Simple PE Cipher
    FilthyHooker - Simple Hooking Class
    CLR Injector - Inject .NET dlls with ease
    Simple Injection - An in-depth look
    MPGH's .NET SDK
    eJect - Simple Injector
    Basic PE Explorer (BETA)

  6. The Following User Says Thank You to Jason For This Useful Post:

    258456 (03-30-2012)

  7. #5
    258456's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    ghjghj
    Posts
    1,222
    Reputation
    18
    Thanks
    300
    My Mood
    Relaxed
    Quote Originally Posted by Jason View Post


    Im guessing you never allocated any memory for szServerRequestArray[i - 2] to point to. Personally I'd use a buffer and then duplicate.

    Code:
    char buffer[256];
    //other code
    sprintf(buffer, "somestring %i some string %i some string", WhichOne, i);
    szServerRequestArray[i - 2] = strdup(buffer);
    LOl, this is just a pain, i am just gonna use the function to return the string, the only bad thing is i will need to call the function many more times, but it isn't that bad performance wise. Thanks guys.

Similar Threads

  1. [Done] Siggy Request.. Warrock style
    By Zcar in forum Help & Requests
    Replies: 7
    Last Post: 06-15-2006, 08:55 AM
  2. New Style
    By Chronologix in forum Art & Graphic Design
    Replies: 3
    Last Post: 04-25-2006, 06:14 PM
  3. Can you Multi gunz still?
    By rzoin in forum Gunz General
    Replies: 2
    Last Post: 04-10-2006, 08:58 PM
  4. New Style - Pop Out
    By Bull3t in forum Art & Graphic Design
    Replies: 9
    Last Post: 02-02-2006, 10:32 PM
  5. Playing Warrock In Multi Windows Need Help
    By Shadowguild in forum WarRock - International Hacks
    Replies: 27
    Last Post: 01-04-2006, 10:32 PM