'can you simply just reassign a new string to a pointer without any issues?

I refer to the sample code provided below from IBM's website regarding the use of Strtok, however the function of Strtok is not my main concern.

My concern is the reuse of char pointers or strings inside the loop, in particular, the string variable named "token" in the code below.

I have read many articles already, but none of them answer this question to me in "black and white".

I'd like to clarify, if the laws of C allow the reuse of char or string array pointer variables like this in order to avoid dangerous coding.

As you can see, the variable "token" is reused over and over again in the loop, and the contents that the pointer representing each time is changed and reassigned with different sized strings each time - this is of course the produced tokens.

Can you use pointers in this way?

#include <stdio.h>
#include <string.h>
 
int main(void)
{
   char *token, *string = "a string, of, ,tokens\0,after null terminator";
 
   /* the string pointed to by string is broken up into the tokens
      "a string", " of", " ", and "tokens" ; the null terminator (\0)
      is encountered and execution stops after the token "tokens"     */
   token = strtok(string, ",");
   do
   {
      printf("token: %s\n", token);
   }
   while (token = strtok(NULL, ","));
}
 
/*****************  Output should be similar to:  *****************
 
token: a string
token:  of
token:
token: tokens
*/

Although this is an example, my main project that I'm trying to write, requires a string variable of some kind, in order to store and handle strings which will be - incoming commands from a telecommunications modem. Commands like this will be of variable lengths each time, and the variable would be named something like "incoming_received_SMS_message", and this will be used in a loop so that the rest of the code determine the command and run the appropriate functions.

So instead of using malloc, memset or going to far with things as some suggest, can you simply just reassign a new string to a pointer without any issues? (By the way I am fully aware that a pointer only contains an address).


EDIT:

So I've tried this code in Arduino IDE to test the theory, but it still generates errors;

char * pointer;
void setup() {
Serial.begin(115200);
}

void loop() {

pointer = "sample text";
pointer = "even more sample text";

Serial.println(pointer);

}

But Then this generates the following errors;

warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings] pointer = "sample text";

warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings] pointer = "even more sample text";



Solution 1:[1]

For starters the provided code has undefined behavior because the function strtok tries to change a string literal

char *token, *string = "a string, of, ,tokens\0,after null terminator";
token = strtok(string, ",");

any attempt to change a string literal results in undefined behavior.

It seems you mean a character array initialized by a string literal instead of a pointer to the string literal itself

char *token, string[] = "a string, of, ,tokens\0,after null terminator";
token = strtok(string, ",");

A pointer may be reassigned as any non-constant variable. For example you may write

int x = 0;
x = 10;
x = 20;

The same way you may reassigned a pointer like for example

char *s = "Hello";

s = "World!";
s = "Bye!";

Or another example

char s[] = "Hello";
char *p;

for ( size_t i = 0; s[i] != '\0'; i++ )
{
    p = s + i;
    putchar( *p );
}
putchar( '\n' );

Pay attention to that if a pointer will point to an object that is not already alive then the pointer will be invalid and dereferencing such a pointer invokes undefined behavior. For example

char *p;

{
    char s[] = "Hello";
    p = s;
}

puts( p ); // undefined behavior!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1