So my code breaks down at buffer[i] = envp[i].
I want to create a char**buffer of the environment variables, and make them lower case (didn't add the putc(tolower()) yet loop). But when I just try to make buffer[i] = envp[i], the compiler returns me this error:
error: assignment makes integer from pointer without a cast [-Wint-conversion] buffer[i] = envp[i]; ^
warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=] printf("%s\n", buffer[i]); ~^ ~~~~~~~~~ %d
int main(int argc, char *argv[], char * envp[])
{
int i = 0;
char *buffer = NULL;
while(envp[i])
{
i++;
}
buffer = (char *)malloc(i * 8);
for (i = 0; envp[i] != 0 ; i++)
{
buffer[i] = envp[i];
printf("%s\n", envp[i]);
printf("%s\n", buffer[i]);
}
return 0;
}
Please help, I've been breaking my head here :( . Thanks so much!!!
char *buffer
, what do you thinkbuffer[i]
is? – Andrew Henlebuffer
is changed to be a sequence ofchar *
rather thanchar
as it is now, nothing is being copied except the pointers fromevp[]
. In short, it's shallow copying. Where originally you had one set of pointers pointing to those strings (inevp[]
), now you would have two (inevp[]
andbuffer[]
). I somewhat doubt that accomplishes much for you. – WhozCraigbuffer[]
still relies onenvp[]
to know how many are valid inbuffer[]
. I'd expectbuffer[]
to be 1 element larger for aNULL
. – chux - Reinstate Monicai
or enumeratingenvp
again. – WhozCraig