Answer the question
In order to leave comments, you need to log in
How to copy the value of an environment variable through a pointer in SI?
Hello! The task is to display environment variables whose names are shorter than the specified number. To do this, I break each variable into [name, value] by the "=" symbol using strtok. However, strtok changes the value itself by adding NULL to the array. How can I copy the value of a variable in the "name = value" format to another variable, so that when working with the new one, the old one does not change. Tried with memcpy and didn't work.
void ShortNames(char **arr, int num)
{
printf("Значения переменных, у которых имена меньше заданного: \n");
for (char **env = arr; *env != 0; env++)
{
char **result = malloc(sizeof(char*) * 1000);
int size = sizeof(env);
char* thisEnv = *env;
//char* thisEnv = malloc(sizeof(char*) * size);
//memcpy(thisEnv, *env, sizeof(char*) * size);
split(result, thisEnv, "=");
if (strlen(result[0]) <= num)
{
printf("%s \n", *env);
}
}
}
Answer the question
In order to leave comments, you need to log in
How to copy value
#include <string.h>
void ShortNames(char **arr, int num)
{
int i;
for (i = 0; arr[i]; ++i) {
int len = strchr(arr[i], '=') - arr[i];
if (len <= num)
printf("%.*s\n", len, arr[i]);
}
}
Why the hell do you need strtok()? environment variable is just a string. What prevents you from finding the '=' sign through strchr(), counting one and taking strlen() from this point? So we got the length :)
(for universality of the code, you can check the character following the = on isspace(), if it matches, skip it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question