pointer to string.

This is a discussion on pointer to string. within the C Programming forums, part of the General Programming Boards category; hi im trying to split up a string e.g: execute -b / run -r / shout -s into 2 different ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    61

    pointer to string.

    hi im trying to split up a string e.g: execute -b / run -r / shout -s into 2 different strings.

    *First array of strings will contain the following, with "/" taken out:
    1st string: buffer[0]=execute -b
    2nd string: buffer[1]=run -r
    3rd string: buffer[2]=shout -a

    *Second array of strings will contain a further tokenized string of each word before any flags:
    1st string: buffer2[0]=execute
    2nd string: buffer2[1]=run
    3rd string: buffer2[2]=shout

    i came out with this rough idea but im still uncertain at some parts and i hope someone would show me where there might be problems.

    Code:
    char* temp;
    char** buffer;       /* i do not have a definite size to malloc this, wat should specify for the 
    char** buffer2;      * size for malloc? */
    int i;                     
    
    i=0;
    temp = strtok(somevalue, "/");
    while (temp) {
      buffer[i] = temp;
      strtok(NULL, "/");
      i++;
    }
    
    /* loop through buffer */
    for( i = 0; i < sizeof(buffer); i++) {   /* can i actually loop all the contents of the string array
         temp = strtok(buffer[i], " ");         * like that? */
         
         buffer2[i] = temp;
         
    }
    Is there a more efficient or shorter way to produce the same result ?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    You can definetely use strtok for this. I think it is appropriate. If you don't want to damage your input in anyway, you can try my strtok.
    Remember that your code is illegal because you are using unallocated buffers. If you don't know the size, then you must guess. And if it just isn't big enough, allocate some more.
    But even so, you can query the size because strtok truncates the string array. Then you can allocate.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    61
    for the second loop, size of buffer will returns the size of memory i allocate for the buffer right? in dat case, if i loop thru the array using the following code, will it be a problem?
    Code:
    /* loop through buffer */
    for( i = 0; i < sizeof(buffer); i++) {   /* can i actually loop all the contents of the string array
         temp = strtok(buffer[i], " ");         * like that? */
         
         buffer2[i] = temp;
         
    }

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    I don't think you quite get how strtok works. It truncates your string and returns the original pointer. You need to allocate or have a big enough buffer to copy that.
    Then you call strtok again until it returns NULL I think.
    You can see my strtok. It has full source. It's also non-damaging.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    61
    oh.. what i actually meant was for

    for( i = 0; i < sizeof(buffer); i++)

    if i do a malloc of buffer=(char**)malloc(1000); for example.
    does sizeof retrieves 1000 or will it jus get the array size?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cstudent View Post
    oh.. what i actually meant was for

    for( i = 0; i < sizeof(buffer); i++)

    if i do a malloc of buffer=(char**)malloc(1000); for example.
    does sizeof retrieves 1000 or will it jus get the array size?
    Using sizeof() on a pointer will return 4 (or 8 on some machines), not the size of the allocation that the pointer points to. You need to "remember" the size of the the allocation yourself.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    61
    right thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to String and Pointer to Char
    By vb.bajpai in forum C Programming
    Replies: 3
    Last Post: 06-15-2007, 03:03 PM
  2. Pointer to array of string and Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-15-2007, 06:04 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  5. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM

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