-1

I'm trying to assign a (char*)0 pointer to a string in C, but it doesn't work... Here is my code: `

char* token = strtok(command, " ");
        int counter = 0;
        while (token) {
            if(counter == 0){
                strcpy(req.command, token);
            }
            
            printf("Token: %s\n", token);
            strcpy(req.arguments[counter], token);
            counter++;

            token = strtok(NULL, " ");
        }

        // strcpy(req.arguments[counter], "\0");
        printf("Ok\n");
        req.arguments_size = counter;
        req.arguments[counter] = (char)* 0;

The req structure is this:

typedef struct {
    char command[LENGTH];
    char *const arguments[LENGTH];
    int arguments_size;
} Request;

`

I'm doing this because I want to use execv() function on a server and I need that there is a (char*) 0 after the command arguments in the array. Thank you for your help!

I tried to assign the pointer to the array element in different ways, but it doesn't work and i can't use strcpy because arguments must be not null!

Here it is what the compiler says to me: client-1.c:55:32: error: assignment of read-only location

client-1.c:55:32: error: assignment of read-only location ‘req.arguments[counter]’ 55 | req.arguments[counter] = zero; | ^

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 3
    Never describe a problem merely as “it doesn't work.” State what output or behavior was observed from the program or compiler, and what output or behavior was desired instead. Edit the question to provide a [mre], including source code readers can compile without any changes or additions to reproduce the problem. Provide an exact copy of any input needed to reproduce the problem. – Eric Postpischil Nov 20 '22 at 20:50
  • 1
    Most likely, the compiler is complaining about attempting to assign a value to a `const` object, because `char *const arguments[LENGTH];` declares `arguments` to be an array of pointers that are `const`-qualified. You should have included the compiler error message in your post. Who defined this structure, you or somebody else? Does it need to be that way? Why does it use `const` with `arguments`? Did you assign values to any of the other elements in the array? How? Show the code for that. – Eric Postpischil Nov 20 '22 at 20:51

2 Answers2

2

You declared an array of constant pointers

char *const arguments[LENGTH];

First of all the array shall be initialized when an object of the structure type is defined and then you may not change elements of the array.

Maybe instead you mean

const char * arguments[LENGTH];

Secondly you need to allocate dynamically memory for stored strings that will be pointed to by elements of the array if you want to copy strings. Otherwise this statement

strcpy(req.arguments[counter], token);

will invoke undefined behavior. For example

req.arguments[counter] = malloc( strlen( token ) + 1 );
strcpy(req.arguments[counter], token);

Also instead of this statement

req.arguments[counter] = (char *) 0;

it will be simpler and clear to write

req.arguments[counter] = NULL;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

The right-hand side of this assignment:

req.arguments[counter] = (char)* 0;

contains a syntax error. You want either (char *)0 or just NULL.

In addition you need to remove the top-level const qualifier from the arguments field, ie const char * arguments[LENGTH]; not char * const arguments[LENGTH];

Dan Bonachea
  • 2,408
  • 5
  • 16
  • 31