psykocrash
Messages postés240Date d'inscriptionvendredi 14 juin 2002StatutMembreDernière intervention17 mars 2009
-
16 avril 2008 à 21:03
psykocrash
Messages postés240Date d'inscriptionvendredi 14 juin 2002StatutMembreDernière intervention17 mars 2009
-
19 avril 2008 à 12:29
Salut,
Je bosse sur un petit programme et je butte sur un problème de pointeurs. Voici le code source qui pose problème :
--------------------
#include <stdio.h>
#include <string.h>
#include <malloc.h>
void make_arguments_table(int argc, char* argv[], char** new_argv)
{
int arguments_count = argc - 2;
int counter = 2;
int len = 0;
int i=0;
new_argv = (char**)malloc(arguments_count * sizeof(char*));
if (new_argv == NULL) printf("Lack of memory space.\n");
for(i=0;i
--------------------
Je fais deux printf() de test. Le premier dans make_arguments_table() qui affiche l'adresse de array[0]. Le deuxième, dans main(), affiche a sont tour cette valeur qui devrait être la même... mais qui ne l'est pas.
En C, faut soit passer par la valeur de retour, soit passer par un pointeur de ton pointeur, soit un (char***)
char** make_arguments_table(int argc, char* argv[])
ou
void make_arguments_table(int argc, char* argv[], char*** new_argv)
cs_jfrancois
Messages postés482Date d'inscriptionvendredi 26 août 2005StatutMembreDernière intervention 5 décembre 20092 17 avril 2008 à 12:24
Bonjour,
arguments est passé par valeur à la fonction make_arguments_table, ce qui fait que les allocations mémoire qui sont faits dans la fonction le sont sur une copie locale de arguments ! Il faut passé l'adresse de arguments pour que ce pointeur soit directement utilisé et modifié (ou passer sa référence, ce qui revient au même puisque le passage par référence n'est rien d'autre qu'un passage par adresse avec une syntaxe de passage par valeur !).
PS: char** x et char* x[] c'est la même chose. Eviter d'utiliser ces 2 syntaxes en même temps, ça donne l'impression que c'est compris comme étant différents.
void make_arguments_table(int argc,char** argv,char*** new_argv)
{
int arguments_count = argc - 2;
int counter = 2;
int len = 0;
int i=0;
*new_argv = (char**)malloc(arguments_count * sizeof(char*));
if (*new_argv == NULL)
{
printf("Lack of memory space (1)\n");
return;
}
printf("make_arguments_table:arguments = %X\n",*new_argv);
for (i=0 ; i
Ce qui donne (programme test) :
test a b c
make_arguments_table:arguments = 4107D0
make_arguments_table:arguments[0] = 4107C0
make_arguments_table:arguments[1] = 4107B0
main:arguments = 4107D0
main:arguments[0] = 4107C0
// Read the time
int h=0;
int m=0;
time_t time_since_1970;
struct tm* local_time;
// Hour and minute from chars to integers
h = (10 * (((int)argv[1][0]) - '0')) + (((int)argv[1][1]) - '0');
m = (10 * (((int)argv[1][3]) - '0')) + (((int)argv[1][4]) - '0');
if (h < 0 || h > 23 || m < 0 || m > 59)
{
display_help();
return (0);
}
// Wait for the right time and execute the commands
char** arguments;
make_arguments_table(argc,argv,arguments);
while (1)
{
time(&time_since_1970);
local_time = localtime(&time_since_1970);
printf("Scheduled: %02d:%02d\tLocal: %02d:%02d:%02d\n"
,h,m,local_time->tm_hour,local_time->tm_min,local_time->tm_sec);
if (local_time->tm_hour h && local_time->tm_min m)
{
printf("time reached\n");
int counter = 0;
while (counter < argc - 2)
{
printf("execute %s\n",arguments[counter]);
counter++;
}
return (0);
}
Sleep(5000); // wait 5 seconds before checking again
}
return (0);
}
void make_arguments_table(int argc,char** argv,char**& new_argv)
{
int arguments_count = argc - 2;
int counter = 2;
int len = 0;
int i = 0;
new_argv = (char**)malloc(arguments_count * sizeof(char*));
if (new_argv == NULL)
{
printf("Lack of memory space (1)\n");
return;
}
for(i=0 ; i
void display_help()
{
printf("usage: stask <hh:mm> <command_1> <command_2> ... <command_n>\n");
printf("Schedule a task.\n");
printf(" <hh:mm>\t: Specify when to execute the command.\n");
printf("\t\t hh = hour (00 <= hh <= 23).\n");
printf("\t\t mm = minute (00 <= mm <= 59).\n");
printf(" <command>\t: Command to execute (use quotes if it includes spaces).\n");
printf("\t\t You can put several commands.\n\n");
printf("Exemple:\n$ stask 07:00 "wxvlc /home/username/playlist.m3u" "echo OK > stask.log"\n\n");
}
psykocrash
Messages postés240Date d'inscriptionvendredi 14 juin 2002StatutMembreDernière intervention17 mars 2009 17 avril 2008 à 00:25
J'y arrive pas. Est ce que tu pourrais me donner un exemple concret stp ? (parce qu'avec seulement la déclaration de la fonction, j'ai du mal à comprendre quoi faire a l'interrieur de la fonction).
psykocrash
Messages postés240Date d'inscriptionvendredi 14 juin 2002StatutMembreDernière intervention17 mars 2009 18 avril 2008 à 15:46
J'ai une erreur lors du malloc sur le array[1] que je n'arrive pas à corriger. Voici le code complet, si vous pouvez me dire où ça cloche ce serait vraiment sympa.
// Read the time
int h=0;
int m=0;
time_t time_since_1970;
struct tm* local_time;
// Hour and minute from chars to integers
h = (10 * (((int)argv[1][0]) - '0')) + (((int)argv[1][1])-'0');
m = (10 * (((int)argv[1][3]) - '0')) + (((int)argv[1][4])-'0');
if (h < 0 || h > 23 || m < 0 || m > 59)
{
display_help();
return (0);
}
// Wait for the right time and execute the commands
int counter = 0;
char** arguments;
while (1)
{
time(&time_since_1970);
local_time = localtime(&time_since_1970);
if (1==1 || local_time->tm_hour == h && local_time->tm_min == m)
{
make_arguments_table(argc, argv, &arguments);
while (counter < (argc-2))
{
printf("debug:arguments[%d] = %s\n", counter, argv[counter]);
counter++;
}
return (0);
}
printf("Scheduled: %d:%d\tLocal: %d:%d\n", h, m, local_time->tm_hour, local_time->tm_min);
sleep(10); // wait 10 seconds before checking again
}
return (0);
}
void make_arguments_table(int argc, char** argv, char*** new_argv)
{
int arguments_count = argc - 2;
int counter = 2;
int len = 0;
int i=0;
*new_argv = (char**)malloc(arguments_count * sizeof(char*));
if (*new_argv == NULL)
{
printf("Lack of memory space (1)\n");
return;
}
for(i=0; i <command_1> <command_2> <command_n>\n");
printf("Schedule a task.\n");
printf(" <hh:mm>\t: Specify when to execute the command.\n");
printf("\t\t h=hour (00 <= h <= 23).\n");
printf("\t\t m=minute (00 <= h <= 59).\n");
printf(" <command>\t: Command to execute (use quotes if it include spaces).\n");
printf("\t\t You can put several commands.\n\n");
printf("Exemple:\n$ stask 07:00 "wxvlc /home/username/playlist.m3u" "echo OK > stask.log"\n\n");
}
psykocrash
Messages postés240Date d'inscriptionvendredi 14 juin 2002StatutMembreDernière intervention17 mars 2009 18 avril 2008 à 23:06
ça marche pas chez moi. gcc me renvoit deux erreurs à cause des char**&.
(j'ai enlevé le include <windows.h> parce que je suis sous linux, précision concernant le numéro des lignes...)
stask2.c:6: erreur: expected «;", «," or «)" before «&" token
stask2.c:84: erreur: expected «;", «," or «)" before «&" token