Im making a really simple TODOlist in C.
My add function takes a char *
as its parameter. When I add it to my char **
list of reminders, it adds the memory address of my buffer instead of the value of the string.
The problem becomes apparent when I run the given source below.
If you try to [a]dd a string, say "Test", and then issue command [p]rint, a "p" will be printed.
I understand that this is because my list[0]
is holding a pointer to my buffer, so that when the value of buffer changes, so does the value in my list.
My C is rusty and I know that strcpy()
might solve this issue? But is this the usual way to handle this?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int size = 0;
int capacity = 50;
char **list;
void initList() {
list = malloc( capacity * sizeof( char * ) );
for (int i = 0; i < capacity; i++ ) {
list[i] = malloc( 256 * sizeof( char ) );
}
}
void saveReminders() {
}
void add( char *reminder ) {
list[size++] = reminder;
}
void delete( int index ) {
}
void print() {
for ( int i = 0; i < size; i++ )
printf( "%s\n", list[i] );
}
void quit() {
saveReminders();
exit( 0 );
}
void readReminders() {
}
void helpMenu() {
printf( "Enter a command:\n" );
printf( "[a]dd a reminder\n" );
printf( "[d]elete a reminder\n" );
printf( "[p]rint all reminders\n" );
printf( "[q]uit\n" );
}
void menu() {
helpMenu();
while ( 1 ) {
char buffer[64];
int index = 0;
printf( "> " );
scanf( "%s", buffer );
if ( strcmp( buffer, "a" ) == 0 ) {
printf( "Enter a reminder:\n> " );
scanf( "%s", buffer );
add( buffer );
helpMenu();
}else if ( strcmp( buffer, "d" ) == 0 ) {
printf("Remove which index?\n> " );
scanf( "%d", &index );
delete( index );
helpMenu();
}else if ( strcmp( buffer, "p" ) == 0 ) {
print();
helpMenu();
}else if ( strcmp( buffer, "q" ) == 0 ) {
quit();
}
}
}
int main( int argc, char* argv[] ) {
initList();
menu();
}