Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problem with Array of char*

Problem with Array of char*

Scheduled Pinned Locked Moved C / C++ / MFC
helpdata-structuresquestion
19 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    error1408
    wrote on last edited by
    #1

    Hi, I have a function in a class that has the following sig:

    int strtoken(char *str, char *separator, char *token[])

    for the *token I do this before:

    char *token[256];
    for(int i = 0; i < 256; i++)
    {
    token[i] = '\0';
    }

    I call the function but at this line it crashes:

    token[0] = strtok(str, separator);

    Error on writing etc. (German Error, so I don't know exactly what it's called in english) Can someone help me?

    S _ C 3 Replies Last reply
    0
    • E error1408

      Hi, I have a function in a class that has the following sig:

      int strtoken(char *str, char *separator, char *token[])

      for the *token I do this before:

      char *token[256];
      for(int i = 0; i < 256; i++)
      {
      token[i] = '\0';
      }

      I call the function but at this line it crashes:

      token[0] = strtok(str, separator);

      Error on writing etc. (German Error, so I don't know exactly what it's called in english) Can someone help me?

      S Offline
      S Offline
      Sarath C
      wrote on last edited by
      #2

      You can simply initialize the array as follows. char *token[256] = { 0 }; strtok return a char* (array of character) not a single char. See the example on using strtok taken from msdn. Read the documentation before using it.

      #include #include char string[] = "A string\tof ,,tokens\nand some more tokens";
      char seps[] = " ,\t\n";
      char *token;

      int main( void )
      {
      	printf( "Tokens:\\n" );
      
      	// Establish string and get the first token:
      	token = strtok( string, seps ); // C4996
      	// Note: strtok is deprecated; consider using strtok\_s instead
      	while( token != NULL )
      	{
      		// While there are tokens in "string"
      		printf( " %s\\n", token );
      
      		// Get next token: 
      		token = strtok( NULL, seps ); // C4996
      	}
      }
      

      -Sarath. "Great hopes make everything great possible" - Benjamin Franklin

      My blog - Sharing My Thoughts

      1 Reply Last reply
      0
      • E error1408

        Hi, I have a function in a class that has the following sig:

        int strtoken(char *str, char *separator, char *token[])

        for the *token I do this before:

        char *token[256];
        for(int i = 0; i < 256; i++)
        {
        token[i] = '\0';
        }

        I call the function but at this line it crashes:

        token[0] = strtok(str, separator);

        Error on writing etc. (German Error, so I don't know exactly what it's called in english) Can someone help me?

        _ Offline
        _ Offline
        _Superman_
        wrote on last edited by
        #3

        It's very difficult to guess without any error codes or messages. All I can think of is that you passed a constant string for the str parameter because strtok will modify the str parameter.

        «_Superman_»

        1 Reply Last reply
        0
        • E error1408

          Hi, I have a function in a class that has the following sig:

          int strtoken(char *str, char *separator, char *token[])

          for the *token I do this before:

          char *token[256];
          for(int i = 0; i < 256; i++)
          {
          token[i] = '\0';
          }

          I call the function but at this line it crashes:

          token[0] = strtok(str, separator);

          Error on writing etc. (German Error, so I don't know exactly what it's called in english) Can someone help me?

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          IMHO there is no apparent reason for the crash, could you please elaborate a bit (i.e. what are str and separator values? Could you please post

          strtoken

          code? :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          E 1 Reply Last reply
          0
          • C CPallini

            IMHO there is no apparent reason for the crash, could you please elaborate a bit (i.e. what are str and separator values? Could you please post

            strtoken

            code? :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            E Offline
            E Offline
            error1408
            wrote on last edited by
            #5

            Ok look, here is the full function (that I did NOT write myself) which should work correct, but I think >>I<< do something NOT correct. Its a tokenizer for char-strings

            int strtoken(char *str, char *separator, char *token[])
            {
            int i = 0;

            	token\[0\] = strtok(str, separator); //This line crashes
            
            	while ( token\[i\] ) 
            	{
            		i++;
            		token\[i\] = strtok(NULL, separator);
            	}
            	return ( i );
            }
            

            And I call it that way atm:

            		char \*token\[256\]; //I think that is an array of char\*, isn't it?
            		for(int i = 0; i < 256; i++)
            		{
            			token\[i\] = '\\0';
            		}
            		Helper::instance()->strtoken(name, ".", token);
            

            name is a char* e.g. "system.fullscreen"

            C A D 3 Replies Last reply
            0
            • E error1408

              Ok look, here is the full function (that I did NOT write myself) which should work correct, but I think >>I<< do something NOT correct. Its a tokenizer for char-strings

              int strtoken(char *str, char *separator, char *token[])
              {
              int i = 0;

              	token\[0\] = strtok(str, separator); //This line crashes
              
              	while ( token\[i\] ) 
              	{
              		i++;
              		token\[i\] = strtok(NULL, separator);
              	}
              	return ( i );
              }
              

              And I call it that way atm:

              		char \*token\[256\]; //I think that is an array of char\*, isn't it?
              		for(int i = 0; i < 256; i++)
              		{
              			token\[i\] = '\\0';
              		}
              		Helper::instance()->strtoken(name, ".", token);
              

              name is a char* e.g. "system.fullscreen"

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #6

              OK, as already suggested by «_Superman_», probably your code is crashing because you're passing a string literal to the function (strtok cannot access for writing a constant string). for instance

              char * name = "system.fullscreen";
              token[0] = strtok(name, "."); // exception here

              would crash the application, while

              char name[] = "system.fullscreen";
              token[0] = strtok(name, ".");

              should work fine. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              E 1 Reply Last reply
              0
              • E error1408

                Ok look, here is the full function (that I did NOT write myself) which should work correct, but I think >>I<< do something NOT correct. Its a tokenizer for char-strings

                int strtoken(char *str, char *separator, char *token[])
                {
                int i = 0;

                	token\[0\] = strtok(str, separator); //This line crashes
                
                	while ( token\[i\] ) 
                	{
                		i++;
                		token\[i\] = strtok(NULL, separator);
                	}
                	return ( i );
                }
                

                And I call it that way atm:

                		char \*token\[256\]; //I think that is an array of char\*, isn't it?
                		for(int i = 0; i < 256; i++)
                		{
                			token\[i\] = '\\0';
                		}
                		Helper::instance()->strtoken(name, ".", token);
                

                name is a char* e.g. "system.fullscreen"

                A Offline
                A Offline
                Arman S
                wrote on last edited by
                #7

                You missed to tell us how your 'name' variable is declared/defined :). Except for it, other stuff seems OK to me (in the sense that they should not provoke a crash).

                -- Arman

                1 Reply Last reply
                0
                • C CPallini

                  OK, as already suggested by «_Superman_», probably your code is crashing because you're passing a string literal to the function (strtok cannot access for writing a constant string). for instance

                  char * name = "system.fullscreen";
                  token[0] = strtok(name, "."); // exception here

                  would crash the application, while

                  char name[] = "system.fullscreen";
                  token[0] = strtok(name, ".");

                  should work fine. :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  E Offline
                  E Offline
                  error1408
                  wrote on last edited by
                  #8

                  Your right, I get a char* name as a parameter and use it to call the function. Thats the cause of the crash. But how do I solve this problem? I have this char *name and it has to be tokenized. How can I do that as strtok crashes with it? Btw. thanks for your help, I would have never thought of THAT ^^

                  C 1 Reply Last reply
                  0
                  • E error1408

                    Your right, I get a char* name as a parameter and use it to call the function. Thats the cause of the crash. But how do I solve this problem? I have this char *name and it has to be tokenized. How can I do that as strtok crashes with it? Btw. thanks for your help, I would have never thought of THAT ^^

                    C Offline
                    C Offline
                    CPallini
                    wrote on last edited by
                    #9

                    well, if you really need to use that fucntion, then you may assign the array the way I shown in my previous post, or do someting like:

                    char * name;
                    name = _strdup("system.fullscreen");
                    if ( name )
                    {
                    strtoken(name, ".", token);
                    free(name); // don't forget to free memory
                    }

                    :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    E 1 Reply Last reply
                    0
                    • C CPallini

                      well, if you really need to use that fucntion, then you may assign the array the way I shown in my previous post, or do someting like:

                      char * name;
                      name = _strdup("system.fullscreen");
                      if ( name )
                      {
                      strtoken(name, ".", token);
                      free(name); // don't forget to free memory
                      }

                      :)

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      E Offline
                      E Offline
                      error1408
                      wrote on last edited by
                      #10

                      But if I call it with that char* it crashes! I have a function

                      doSomething(char *name)
                      {
                      [...]
                      strtoken(name, ".", token); //crashes
                      }

                      That works but it does not help me:

                      doSomething(char *name)
                      {
                      [...]
                      char foo[] = "hello.world";
                      strtoken(foo, ".", token); //does NOT crash
                      }

                      But how can I use strtoken with char *name? Can I make a char foo[] out of char *name?

                      C 1 Reply Last reply
                      0
                      • E error1408

                        But if I call it with that char* it crashes! I have a function

                        doSomething(char *name)
                        {
                        [...]
                        strtoken(name, ".", token); //crashes
                        }

                        That works but it does not help me:

                        doSomething(char *name)
                        {
                        [...]
                        char foo[] = "hello.world";
                        strtoken(foo, ".", token); //does NOT crash
                        }

                        But how can I use strtoken with char *name? Can I make a char foo[] out of char *name?

                        C Offline
                        C Offline
                        CPallini
                        wrote on last edited by
                        #11

                        if you really need to pass a constant string to doSomething, the you may write (as already suggested)

                        doSomething(char * name)
                        {
                        //...
                        char * name_writable_copy = _strdup( name );
                        if ( name_writable_copy )
                        {
                        strtoken(name_writable_copy, ".", token);
                        free( name_writable_copy );
                        }
                        else
                        {
                        // handle (unusual) allocation error
                        }
                        }

                        The above code shouldn't crash. :)

                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                        [My articles]

                        E 1 Reply Last reply
                        0
                        • C CPallini

                          if you really need to pass a constant string to doSomething, the you may write (as already suggested)

                          doSomething(char * name)
                          {
                          //...
                          char * name_writable_copy = _strdup( name );
                          if ( name_writable_copy )
                          {
                          strtoken(name_writable_copy, ".", token);
                          free( name_writable_copy );
                          }
                          else
                          {
                          // handle (unusual) allocation error
                          }
                          }

                          The above code shouldn't crash. :)

                          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                          [My articles]

                          E Offline
                          E Offline
                          error1408
                          wrote on last edited by
                          #12

                          Ok thx. I will try it in the evening today and post my results.

                          C 1 Reply Last reply
                          0
                          • E error1408

                            Ok thx. I will try it in the evening today and post my results.

                            C Offline
                            C Offline
                            CPallini
                            wrote on last edited by
                            #13

                            Well, good luck! :)

                            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                            [My articles]

                            1 Reply Last reply
                            0
                            • E error1408

                              Ok look, here is the full function (that I did NOT write myself) which should work correct, but I think >>I<< do something NOT correct. Its a tokenizer for char-strings

                              int strtoken(char *str, char *separator, char *token[])
                              {
                              int i = 0;

                              	token\[0\] = strtok(str, separator); //This line crashes
                              
                              	while ( token\[i\] ) 
                              	{
                              		i++;
                              		token\[i\] = strtok(NULL, separator);
                              	}
                              	return ( i );
                              }
                              

                              And I call it that way atm:

                              		char \*token\[256\]; //I think that is an array of char\*, isn't it?
                              		for(int i = 0; i < 256; i++)
                              		{
                              			token\[i\] = '\\0';
                              		}
                              		Helper::instance()->strtoken(name, ".", token);
                              

                              name is a char* e.g. "system.fullscreen"

                              D Offline
                              D Offline
                              David Crow
                              wrote on last edited by
                              #14

                              error1408 wrote:

                              Helper::instance()->strtoken(name, ".", token);

                              How about something like:

                              char *temp = new char[strlen(name) + 1];
                              strcpy(temp, name);
                              Helper::instance()->strtoken(temp, ".", token);

                              "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                              E 1 Reply Last reply
                              0
                              • D David Crow

                                error1408 wrote:

                                Helper::instance()->strtoken(name, ".", token);

                                How about something like:

                                char *temp = new char[strlen(name) + 1];
                                strcpy(temp, name);
                                Helper::instance()->strtoken(temp, ".", token);

                                "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                                E Offline
                                E Offline
                                error1408
                                wrote on last edited by
                                #15

                                DavidCrow wrote:

                                How about something like: char *temp = new char[strlen(name) + 1]; strcpy(temp, name); Helper::instance()->strtoken(temp, ".", token);

                                Ok this works! But I don't understand why...could someone explain it to me? Why does the param char * not work but the char * thats created here? Btw. I have to DELETE the char *temp afterwards, right?

                                D 1 Reply Last reply
                                0
                                • E error1408

                                  DavidCrow wrote:

                                  How about something like: char *temp = new char[strlen(name) + 1]; strcpy(temp, name); Helper::instance()->strtoken(temp, ".", token);

                                  Ok this works! But I don't understand why...could someone explain it to me? Why does the param char * not work but the char * thats created here? Btw. I have to DELETE the char *temp afterwards, right?

                                  D Offline
                                  D Offline
                                  David Crow
                                  wrote on last edited by
                                  #16

                                  error1408 wrote:

                                  Why does the param char * not work but the char * thats created here?

                                  Work through this:

                                  void main( void )
                                  {
                                  char *abc = "First";
                                  char xyz[] = "Last";

                                  abc\[0\] = '1';
                                  xyz\[0\] = '2';
                                  

                                  }

                                  error1408 wrote:

                                  Btw. I have to DELETE the char *temp afterwards, right?

                                  Correct (since it points to heap memory).

                                  "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                                  E 1 Reply Last reply
                                  0
                                  • D David Crow

                                    error1408 wrote:

                                    Why does the param char * not work but the char * thats created here?

                                    Work through this:

                                    void main( void )
                                    {
                                    char *abc = "First";
                                    char xyz[] = "Last";

                                    abc\[0\] = '1';
                                    xyz\[0\] = '2';
                                    

                                    }

                                    error1408 wrote:

                                    Btw. I have to DELETE the char *temp afterwards, right?

                                    Correct (since it points to heap memory).

                                    "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                                    E Offline
                                    E Offline
                                    error1408
                                    wrote on last edited by
                                    #17

                                    Ok I think I got it. So is it right, that in my case I can not know if the char *name that I get is a literal or not so to be sure it's not I have to create a temp array?

                                    D I 2 Replies Last reply
                                    0
                                    • E error1408

                                      Ok I think I got it. So is it right, that in my case I can not know if the char *name that I get is a literal or not so to be sure it's not I have to create a temp array?

                                      D Offline
                                      D Offline
                                      David Crow
                                      wrote on last edited by
                                      #18

                                      error1408 wrote:

                                      ...I can not know if the char *name that I get is a literal or not so to be sure it's not I have to create a temp array?

                                      You'll notice in my example that both variables pointed to a string literal, yet only one of them could be changed. Therein lies the difference between a char* vs. char[]. See here for more.

                                      "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                                      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                                      1 Reply Last reply
                                      0
                                      • E error1408

                                        Ok I think I got it. So is it right, that in my case I can not know if the char *name that I get is a literal or not so to be sure it's not I have to create a temp array?

                                        I Offline
                                        I Offline
                                        ilostmyid2
                                        wrote on last edited by
                                        #19

                                        i agree with the pallini's solution and also david gave a good description. string literals r located in a const part of memory which is not writable. this is why arrays don't cause crash. they're located in heap or stack based on whether u allocate them or use local variables which r both writable parts of memory. there's an API function to determine whether a block of memory or a string is const named AfxIsValidAddress. it may also test whether the block is writable. but if i were u, i would use _strdup anyway, and would pass the arg as const for the caller to make sure that the original version of the passed string is not altered.

                                        1 Reply Last reply
                                        0
                                        Reply
                                        • Reply as topic
                                        Log in to reply
                                        • Oldest to Newest
                                        • Newest to Oldest
                                        • Most Votes


                                        • Login

                                        • Don't have an account? Register

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • World
                                        • Users
                                        • Groups