i'm trying a new assignment i've got: i need to copy the Environment variables from the shell into an array, then lowercase them and print them back. (i'm using Ubuntu 20.04.4 LTS, gcc for compiling, and writing my code in C language). i was adviced to use malloc to create the array but decided not to. everything goes good up to the point i'm trying, for my own reasons, to see what i've created. then, when running it via valgrind - i'm getting this error i'm not quite sure why and how to fix. i'd appreciate the help.
as for the code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
char PrintString(char **strings, int counter)
{
for(int j = 0; strings[j] != NULL; j )
{
printf("%s \n", strings[j]);
}
return (4);
}
/*----------------------------------------------*/
//function to lowercase the strings
char Lowercase(char **array, int seq_num) //seq_num is the size of the array
{
int i;
int j;
for (i = 0; i < seq_num; i ) //for every line in the env list
{
for(j = 0; j < strlen(array[i]); j ) //for every character in the env strings
{
array[i][j] = tolower(array[i][j]);
}
printf("%s\n", (array[i]));
}
return(**array);
}
/*----------------------------------------------*/
//in order to get the number of the env var - this is the block
int Conut_variables(char **envp)
{
int counter=0; //counter for the no. of variables
for(int i = 0; envp[i] != NULL ; i )
{
counter ;
}
return(counter);
}
/*----------------------------------------------*/
int main(int argc, char *argv[], char *envp[])
{
int counter = Conut_variables(envp);
int i,j;
char *new_buffer[counter];
for(i = 0; envp[i] != NULL; i )
{
new_buffer[i] = envp[i];
}
Lowercase(new_buffer, counter);
PrintString(new_buffer, counter);
return (0);
}
/*----------------------------------------------*/
as for the errors:
==20052== Conditional jump or move depends on uninitialised value(s)
==20052== at 0x10922E: PrintString (Ex4vol2.c:9)
==20052== by 0x1094CB: main (Ex4vol2.c:53)
==20052== Uninitialised value was created by a stack allocation
==20052== at 0x109364: main (Ex4vol2.c:44)
==20052==
==20052==
==20052== HEAP SUMMARY:
==20052== in use at exit: 0 bytes in 0 blocks
==20052== total heap usage: 1 allocs, 1 frees, 1,024 bytes allocated
==20052==
==20052== All heap blocks were freed -- no leaks are possible
==20052==
==20052== For lists of detected and suppressed errors, rerun with: -s
==20052== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
CodePudding user response:
In PrintString
you're not using the right loop control:
for(int j = 0; strings[j] != NULL; j )
You pass int counter
which tells you how many entries you have, but you don't use it. You instead look for a NULL
entry, but you never set a NULL
entry nor do you allocate space in the array for it.
So use counter
instead.
for(int j = 0; j<counter; j )