Thread: Finding length of char **

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

    Finding length of char **

    Hello!

    I'm trying to create a stringSplit-function in C, and it is supposed to return a char**, containing the parts of the supplied string. But how do I find the length of the char**? I.e. how do I find how many char*'s there are?

    If I return a char * I can look for 0, but when I try this for char ** (i.e. "if array[x]==0") I get Segmentation fault (Linux). So what do I do?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You count them. More specifically, your stringSplit function has to count them and tell you how many there are. ETA: There is no way to tell, just given a char **, how much memory is allocated at that spot; so whoever malloc's has to pass that information.
    Last edited by tabstop; 04-23-2008 at 08:02 PM.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    40
    Thanks. I didn't know how to do it but I tried a few ways, and the following works (the function is apparently not implemented yet, but I get the number back in the int number in main and the char array works). I assumed there would be a way of getting the size of a char**, but maybe there isn't.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    char ** stringSplit(char * string, char delimiter, int* number)
    {
      int x;
      char** test=(char **)malloc(3*sizeof(char*));
      test[0]="Line 1";
      test[1]="Line 2";
      test[2]="Line 3";
      *number=3;
      return(test);
    
    }
    
    main()
    {
      int number;
      int x;
      char ** test=stringSplit("Test",'|',&number);
    
      for (x=0;x<number;x++)
      {
         printf("&#37;s\n",test[x]);
      }
      exit(0);
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, sizeof(char**) is almost always equal to 4, these days. But char ** is just a pointer to a memory location. If we're interpreting it as the start of an array, that tells me not at all how far that array goes.

  5. #5
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    I don't know what you're trying to do, but consider ending your char** with a null string to mark as a terminator. for example:

    Code:
    char ** stringSplit(char * string, char delimiter, int* number)
    {
      int x;
      char** test=(char **)malloc(4*sizeof(char*));
      test[0]="Line 1";
      test[1]="Line 2";
      test[2]="Line 3";
      test[3]=NULL;
      for(x = 0; *test; test++, x++);
      *number=x;
      return(test);
    }

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I am not sure how new to C you are, so forgive me if I offend you with info you are already aware of, but check out strtok() for tokenizing a string. You could perhaps implement something similar thus eliminating the need to use a char** at all.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you want to look into strtok, you can look into my own strtok function. It's much better than the original in that in won't damage the input.
    And also don't use implicit main: http://cpwiki.sourceforge.net/Implic...2a2eaeb5955006
    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.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Wow, you are sure plugging away at your strtok() there Elysia

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yeah, I hate the original strtok, so
    Plus mine is more convenient. No need to split the string yourself
    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.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yeah I took a look at it, good stuff. I love standard functions that come with warnings.

  11. #11
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    You dont return a "char**" instead pass a "char[][1024]" into one of the arguments.

    Code:
    #include <string.h>
    #include <stdio.h>
    
    int SplitString(const char* in, const char* tokens, char out[][1024], int outCount);
    
    int main(int argc, char* argv[])
    {
    
        char splitStrings[5][1024];
        //5 is the maximum number of strings the output array can hold
        int count = SplitString("Test|Hello|Blah", "|", splitStrings, 5);
    
        for(int i = 0; i < count; ++i)
            printf("&#37;s\n", splitStrings[i]);
        return 0;
    }
    
    
    int SplitString(const char* in, const char* tokens, char out[][1024], int outCount)
    {
        char tempChar[0xFFFF];
        int count = 0;
        strncpy(tempChar, in, sizeof(tempChar));
    
        char* tok = strtok(tempChar, tokens);
        while(count < outCount && tok)
        {
            strncpy(out[count++], tok, sizeof(*out));
            tok = strtok(NULL, tokens);
        }
    
        return count;
    }
    Last edited by 39ster; 04-24-2008 at 03:42 AM. Reason: changed strncpy(out[count++], tok, 1024); to strncpy(out[count++], tok, sizeof(*out));

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Avoid that evil strtok function. This is easier and safer:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdint.h>
    #include <assert.h>
    
    const char* strtokv2(const char* strToSearch, const char* strDelimiter);
    const char* strtokv3(const char* strToSearch, const char* strDelimiter, char* pBuffer, size_t nBufferSize);
    
    const char* strtokv2(const char* strToSearch, const char* strDelimiter)
    {
    	size_t nStrLength = strlen(strToSearch);
    	size_t nDelLength = strlen(strDelimiter);
    	const char* strEnd = strToSearch + nStrLength;
    	const char* p = strToSearch;
    	while (p < strEnd && *p == ' ') p++;
    	while (p++ < strEnd)
    	{
    		if (*p == *strDelimiter)
    		{
    			bool bFound = true;
    			if ((size_t)(strEnd - p) < nDelLength) // Length of remaining string - length of delimiter
    			for (size_t i = 1; i < nDelLength; i++)
    			{
    				if (p[i] != strDelimiter[i])
    				{
    					bFound = false;
    					break;
    				}
    			}
    			if (bFound)
    				return p;
    		}
    	}
    	return NULL;
    }
    
    const char* strtokv3(const char* strToSearch, const char* strDelimiter, char* pBuffer, size_t nBufferSize)
    {
    	uint32_t nLength;
    	char* pFound;
    	if (!strToSearch) return NULL;
    	pFound = (char*)strtokv2(strToSearch, strDelimiter);
    	if (!pFound)
    		nLength = strlen(strToSearch) + 1; // +1 for NULL
    	else
    		nLength = (pFound - strToSearch) + 1; // +1 for NULL
    	assert(nBufferSize >= nLength);
    	memcpy(pBuffer, strToSearch, nLength - 1);
    	pBuffer[nLength - 1] = 0;
    	if (pFound)
    		return pFound + 1;
    	else
    		return pFound;
    }
    
    int main (void)
    {
    	char str[] = "This is a test string";
    	char mytokenizedstr[50][20] = {0};
    	int index = 0;
    	const char* pSearchPos = str;
    
    	while ( (pSearchPos = strtokv3( pSearchPos, " ", mytokenizedstr[index], sizeof(mytokenizedstr[index]) )) != NULL ) index++;
    	for (int i = 0; i < 5; i++)
    		puts(mytokenizedstr[i]);
    	puts(str);
    	return 0;
    }
    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.

  13. #13
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Heres mine:
    Code:
    bool StrTok(const char* in, const char* tokens, int* index, char* out, size_t outLen)
    {
        const char* ptr = &in[*index];
        if(*ptr && outLen)
        {
            size_t len = strcspn(ptr, tokens);
            size_t outSize = len < outLen-1 ? len : outLen-1;
            strncpy(out, ptr, outSize);
            out[outSize] = 0;
            *index += len + (ptr[len] != 0);
            return true;
        }
        return false;
    }
    
    const char myString[] = "dasd|ds";
    int main(int argc, char* argv[])
    {
        char buffer[1024];
        int index = 0;
    
        while(StrTok(myString, "|", &index, buffer, sizeof(buffer)))
            printf("&#37;s\n", buffer);
        
        return 0;
    }
    Last edited by 39ster; 04-24-2008 at 08:59 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  2. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  3. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  4. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM